Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my ASP.NET LinkButton not firing the OnClick="AddToCart_Click" event?

I have been banging my head against the wall on this for a full day now. I'm working out of Apress's "Beginning ASP.NET E-Commerce in C#", in the case someone is familiar with the project. In chapter 10, we are working with the PayPal AddToCart and GoToCart functionality. This is the event that isn't firing:

    //Why is this not working?
protected void AddToCartButton_Click1(object sender, EventArgs e)
{
    string productID = Request.QueryString["ProductID"];
    ProductDetails pd = CatalogAccess.GetProductDetails(productId);
    string options = "";
    foreach (Control cnt in attrPlaceHolder.Controls)
    {
        if (cnt is Label)
        {
            Label attrLabel = (Label)cnt;
            options += attrLabel.Text;
        }
        if (cnt is DropDownList)
        {
            DropDownList attrDropDown = (DropDownList)cnt;
            options += attrDropDown.Items[attrDropDown.SelectedIndex] + "; ";
        }
    string productUrl = Link.ToProduct(pd.ProductID.ToString());
    string destination = Link.ToPayPalAddItem(productUrl, pd.Name, pd.Price, options);
    Response.Redirect(destination);
    }

Here is the LinkButton's code:

    <p>
    <asp:LinkButton ID="AddToCartButton" runat="server" CausesValidation="False" OnClick="AddToCartButton_Click1">Add to Shopping Cart</asp:LinkButton>
</p>

I have tried setting a breakpoint but the event is never reached. The LinkButton also causes a postback, but never fires the OnClick event.

Any help would be much appreciated!

Here's url: http://www.northarktest.net/edwards/balloonshop

It seems the click event is firing on the server, but while locally debugging.

like image 742
Bazinga Avatar asked Mar 31 '12 15:03

Bazinga


1 Answers

In case it helps anyone I had a similar problem in that my LinkButton Click events (assigned in the code behind) were never firing. Turns out that since my LinkButtons were being dynamically created, I had to move them out of my Page_Load() event and into a Page_Init() event. After doing that the LinkButton Click events started working ... something to do with the page life cycle and all that fun stuff.

like image 122
Christopher Avatar answered Oct 05 '22 09:10

Christopher