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

时间:2023-01-19 01:11:59

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:

我现在一直在撞墙挡住整整一天。我正在研究Apress的“用C#开始ASP.NET电子商务”,如果有人熟悉这个项目的话。在第10章中,我们正在使用PayPal AddToCart和GoToCart功能。这是未触发的事件:

    //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事件在服务器上触发,但在本地调试时。

7 个解决方案

#1


1  

I guess LinkButton does fire the OnClick event. Maybe the method AddToCartButton_Click1() redirects to the wrong URL, please recheck this line:

我猜LinkBut​​ton会触发OnClick事件。也许方法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

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。

#2


3  

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.

如果它有助于任何人我遇到类似的问题,我的LinkBut​​ton Click事件(在后面的代码中分配)永远不会被触发。事实证明,由于我的LinkBut​​tons是动态创建的,我不得不将它们移出我的Page_Load()事件并进入Page_Init()事件。完成后,LinkBut​​ton Click事件开始工作......与页面生命周期和所有有趣的东西有关。

#3


1  

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.

    尝试将onclick方法从:OnClick =“AddToCartButton_Click1”更改为OnClick =“AddToCartButton_Click”。只需删除数字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. Then add your code existing code from your old button event (AddToCartButton_Click1) to the new one.

    如果这不起作用,请通过Visual Studio设计视图在页面中拖动一个新按钮,然后双击Button以生成事件处理程序。然后将旧按钮事件(AddToCartButton_Click1)中的代码现有代码添加到新代码中。

#4


1  

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.

我有这个问题。就我而言,问题在于我为其中的字段添加了一些带有验证器的modalpopup。如果要进行验证并且此验证失败(即使您无法看到,如我的情况),除非您将CausesValidation属性声明为false,否则任何按钮都不会引发事件。

#5


0  

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!

希望能帮助到你!

#6


0  

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问题。

#7


-1  

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. I removed the attribute and the linkbuttons worked!

问题是我在同一页面上有一个PayPal按钮,它有一个name =“submit”的属性,它以某种方式干扰了回发。我删除了属性,链接按钮工作!

#1


1  

I guess LinkButton does fire the OnClick event. Maybe the method AddToCartButton_Click1() redirects to the wrong URL, please recheck this line:

我猜LinkBut​​ton会触发OnClick事件。也许方法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

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。

#2


3  

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.

如果它有助于任何人我遇到类似的问题,我的LinkBut​​ton Click事件(在后面的代码中分配)永远不会被触发。事实证明,由于我的LinkBut​​tons是动态创建的,我不得不将它们移出我的Page_Load()事件并进入Page_Init()事件。完成后,LinkBut​​ton Click事件开始工作......与页面生命周期和所有有趣的东西有关。

#3


1  

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.

    尝试将onclick方法从:OnClick =“AddToCartButton_Click1”更改为OnClick =“AddToCartButton_Click”。只需删除数字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. Then add your code existing code from your old button event (AddToCartButton_Click1) to the new one.

    如果这不起作用,请通过Visual Studio设计视图在页面中拖动一个新按钮,然后双击Button以生成事件处理程序。然后将旧按钮事件(AddToCartButton_Click1)中的代码现有代码添加到新代码中。

#4


1  

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.

我有这个问题。就我而言,问题在于我为其中的字段添加了一些带有验证器的modalpopup。如果要进行验证并且此验证失败(即使您无法看到,如我的情况),除非您将CausesValidation属性声明为false,否则任何按钮都不会引发事件。

#5


0  

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!

希望能帮助到你!

#6


0  

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问题。

#7


-1  

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. I removed the attribute and the linkbuttons worked!

问题是我在同一页面上有一个PayPal按钮,它有一个name =“submit”的属性,它以某种方式干扰了回发。我删除了属性,链接按钮工作!