C# 为什么我的 ASP.NET LinkButton 没有触发 OnClick="AddToCart_Click" 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9957049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my ASP.NET LinkButton not firing the OnClick="AddToCart_Click" event?
提问by Bazinga
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:
这是 LinkButton 的代码:
<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.
我曾尝试设置断点,但从未达到该事件。LinkButton 也会导致回发,但不会触发 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.
似乎单击事件在服务器上触发,但在本地调试时。
采纳答案by Ashwini Verma
I guess LinkButtondoes fire the OnClickevent. Maybe the method AddToCartButton_Click1()redirects to the wrong URL, please recheck this line:
我想LinkButton确实会触发该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
为什么?点击添加到购物车链接按钮后,我得到了这个 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.
现在,如果您注意到 URL 中缺少页面,它应该包含类似:abc.aspx?ProductId=22。
回答by sbhomra
From looking at your code, I can't see any problem. You can try the following:
通过查看您的代码,我看不出任何问题。您可以尝试以下操作:
Try changing your
onclickmethod from:OnClick="AddToCartButton_Click1"toOnClick="AddToCartButton_Click". Just remove the number 1. Do the same for your code-behind method as well.Rebuild your project.
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.
尝试将您的
onclick方法从:更改OnClick="AddToCartButton_Click1"为OnClick="AddToCartButton_Click"。只需删除数字 1。对您的代码隐藏方法也执行相同操作。重建你的项目。
如果这不起作用,请通过 Visual Studio 设计视图在页面中拖动一个新按钮,然后双击该按钮以生成事件处理程序。然后将旧按钮事件 (
AddToCartButton_Click1) 中的代码现有代码添加到新按钮事件中。
回答by trebor faulken
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:
在 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..
注意:我跳过了 PayPal 购物车,并在本书后面尝试将产品添加到 BalloonShop 购物车时遇到了这个问题。
Hope it helps!
希望能帮助到你!
回答by adripanico
I have had this problem. In my case, the problem was that I had added some modalpopupwith 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 CausesValidationproperty to false.
我遇到过这个问题。就我而言,问题在于我modalpopup为其中的字段添加了一些验证器。如果要进行验证并且此验证失败(即使您看不到,就像我的情况一样),除非您将该CausesValidation属性声明为 false,否则任何按钮都不会引发该事件。
回答by user3620136
I had this problem It worked if I changed the LinkButton to a Button though
我遇到了这个问题 如果我将 LinkButton 更改为 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" 属性,它以某种方式干扰了回发。我删除了该属性并且链接按钮起作用了!
回答by Faron
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 问题。
回答by Chris D. Emerson
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.
如果它可以帮助我遇到类似问题的任何人,我的 LinkButton Click 事件(在后面的代码中分配)从未触发。事实证明,由于我的 LinkButton 是动态创建的,我不得不将它们从我的 Page_Load() 事件中移到 Page_Init() 事件中。这样做之后,LinkButton Click 事件开始工作……与页面生命周期和所有有趣的东西有关。

