Why doesn't my ASP.NET LinkButton fire the OnClick = "AddToCart_Click" event?

I already banged my head on the wall for the whole day. I am working on Apress "Getting ASP.NET E-Commerce in C #", in case someone is familiar with the project. In Chapter 10, we work with the PayPal AddToCart and GoToCart features. This is an event that does not fire:

//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 code:

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

I tried to set a breakpoint, but the event was never reached. LinkButton also raises a postback, but never fires an OnClick event.

Any help would be greatly appreciated!

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

It seems that the click event is fired on the server, but with local debugging.

+7
source share
7 answers

I think LinkButton fires an OnClick event. Perhaps the AddToCartButton_Click1() method redirects the wrong URL, please double-check this line:

 string productUrl = Link.ToProduct(pd.ProductID.ToString()); string destination = Link.ToPayPalAddItem(productUrl, pd.Name, pd.Price, options); Response.Redirect(destination); 

Why? After clicking on the Add to LinkButton Cart link, I received this URL: http://www.northarktest.net/edwards/balloonshop/Im-Younger-Than-You-p22/?ProductId=22

Now, if you notice that there is no page in the URL that should have something like: abc.aspx?ProductId=22 .

+1
source

In case this helps someone, I had a similar problem because my LinkButton Click events (assigned in the code behind) never fired. It turns out that since my LinkButtons were dynamically created, I had to move them from my Page_Load () object and to the Page_Init () event . After this LinkButton Click event began to work ... something in common with the page life cycle and all this interesting stuff.

+3
source

From looking at your code, I do not see any problems. You can try the following:

  • Try changing the onclick method: from OnClick="AddToCartButton_Click1" to OnClick="AddToCartButton_Click" . Just delete number 1. Do the same for your code method.

  • Restore your project.

  • If this does not work, drag a new button on your page using Visual Studio and double-click the button to generate an event handler. Then add the code of the existing code from the old button event ( AddToCartButton_Click1 ) to the new one.

+1
source

I had this problem. In my case, the problem was that I added several modalpopup with validators for the fields inside it. If the check is performed, and this check is not performed (even if you cannot see it, as in my case), any button will not raise an event if you do not declare the CausesValidation property false.

+1
source

Sorry for another post ...

I have an answer kindly provided by one of the authors of the book ... thanks, Mr. Andrei Rinet ...

At the top of the Page_load event in the product.aspx add file:

AddToCartButton.PostBackUrl = Request.Url.AbsoluteUri;

NOTE. I skipped the PayPal shopping cart and ran into this problem later in the book, trying to add products to the BalloonShop shopping cart.

Hope this helps!

0
source

I know this is an old question, but if you add:

 AddToCartButton.PostBackUrl = Request.RawUrl; 

Url problem will be fixed in Page_Load.

0
source

I had this problem. It worked if I changed LinkButton to a button, though

The problem was that I had a PayPal button on the same page, and it had a name = "submit" attribute, which somehow interfered with the postback. I removed the attribute and the link buttons worked.

-one
source

All Articles