简体   繁体   English

为什么我的ASP.NET LinkBut​​ton没有触发OnClick =“AddToCart_Click”事件?

[英]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. 我正在研究Apress的“在C#中开始使用ASP.NET电子商务”,如果有人熟悉该项目的话。 In chapter 10, we are working with the PayPal AddToCart and GoToCart functionality. 在第10章中,我们正在使用PayPal AddToCart和GoToCart功能。 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: 这是LinkBut​​ton的代码:

    <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. LinkBut​​ton也会导致回发,但永远不会触发OnClick事件。

Any help would be much appreciated! 任何帮助将非常感激!

Here's url: http://www.northarktest.net/edwards/balloonshop 这是网址: http//www.northarktest.net/edwards/balloonshop

It seems the click event is firing on the server, but while locally debugging. 似乎click事件在服务器上触发,但在本地调试时。

In case it helps anyone I had a similar problem in that my LinkButton Click events (assigned in the code behind) were never firing. 如果它帮助了我有类似问题的人,我的LinkBut​​ton Click事件(在后面的代码中分配)永远不会被触发。 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 . 事实证明,由于我的LinkBut​​tons是动态创建的, 我不得不将它们移出我的Page_Load()事件并进入Page_Init()事件 After doing that the LinkButton Click events started working ... something to do with the page life cycle and all that fun stuff. 完成后,LinkBut​​ton Click事件开始工作......与页面生命周期和所有有趣的东西有关。

I have had this problem. 我有这个问题。 In my case, the problem was that I had added some modalpopup with validators for the fields inside it. 就我而言,问题是我为其中的字段添加了一些带有验证器的modalpopup 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. 如果要进行验证并且此验证失败(即使您无法看到,如我的情况),除非您将CausesValidation属性声明为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" . 尝试将onclick方法从: OnClick="AddToCartButton_Click1"更改为OnClick="AddToCartButton_Click" Just remove the number 1. Do the same for your code-behind method as well. 只需删除数字1.对代码隐藏方法也一样。

  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. 如果这不起作用,请通过Visual Studio设计视图在页面中拖动一个新按钮,然后双击Button以生成事件处理程序。 Then add your code existing code from your old button event ( AddToCartButton_Click1 ) to the new one. 然后将旧按钮事件( AddToCartButton_Click1 )中的代码现有代码添加到新代码中。

I guess LinkButton does fire the OnClick event. 我猜LinkButton会触发OnClick事件。 Maybe the method AddToCartButton_Click1() redirects to the wrong URL, please recheck this line: 也许方法AddToCartButton_Click1()重定向到错误的URL,请重新检查此行:

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 点击Add to Shopping Cart LinkBut​​ton后,我得到了这个网址: http://www.northarktest.net/edwards/balloonshop/Im-Younger-Than-You-p22/?ProductId=22 ://www.northarktest.net/edwards/balloonshop/Im-Younger-Than-You-p22/?ProductId 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 . 现在,如果您注意到URL中缺少页面,则应该包含以下内容: 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... 我得到了答案,礼貌的书作者之一...谢谢Andrei Rinea先生......

At the top of the Page_load event in product.aspx add: 在product.aspx中的Page_load事件的顶部添加:

AddToCartButton.PostBackUrl = Request.Url.AbsoluteUri; 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.. 注意:当我尝试将产品添加到BalloonShop购物车时,我跳过了PayPal购物车并在本书后面遇到了此问题。

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. 对于Page_Load,它将更正Url问题。

I had this problem It worked if I changed the LinkButton to a Button though 我遇到了这个问题如果我将LinkBut​​ton更改为Button,它会有效

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. 问题是我在同一页面上有一个PayPal按钮,它有一个name =“submit”的属性,它以某种方式干扰了回发。 I removed the attribute and the linkbuttons worked! 我删除了属性,链接按钮工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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