简体   繁体   中英

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.

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.

I have had this problem. In my case, the problem was that I had added some modalpopup with validators for the fields inside it. If there is a validation to be done and this validation fails (even if you cannot see that, as in my case), any button would not raise the event unless you declare the CausesValidation property to false.

From looking at your code, I can't see any problem. You can try the following:

  1. Try changing your onclick method from: OnClick="AddToCartButton_Click1" to OnClick="AddToCartButton_Click" . Just remove the number 1. Do the same for your code-behind method as well.

  2. Rebuild your project.

  3. If that doesn't work, drag a new button in your page via Visual Studio design view and double click on the Button to generate the event handler. Then add your code existing code from your old button event ( AddToCartButton_Click1 ) to the new one.

I guess LinkButton does fire the OnClick event. Maybe the method AddToCartButton_Click1() redirects to the wrong URL, please recheck 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 Shopping Cart LinkButton I got this URL: http://www.northarktest.net/edwards/balloonshop/Im-Younger-Than-You-p22/?ProductId=22

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

Sorry about the other post...

I have got the answer, courtesy of one of the book authors... thank you Mr. Andrei Rinea...

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

AddToCartButton.PostBackUrl = Request.Url.AbsoluteUri;

NOTE: I skipped over the PayPal shopping cart and encountered this problem later in the book when trying to add products to the BalloonShop shopping cart..

Hope it helps!

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

AddToCartButton.PostBackUrl = Request.RawUrl;

To the Page_Load it will correct the Url issues.

I had this problem It worked if I changed the LinkButton to a Button though

The problem was I had a PayPal button on the same page and it had a attribute of name="submit" which was interfering with the postback somehow. I removed the attribute and the linkbuttons worked!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM