簡體   English   中英

為什么我的ASP.NET LinkBut​​ton沒有觸發OnClick =“AddToCart_Click”事件?

[英]Why is my ASP.NET LinkButton not firing the OnClick=“AddToCart_Click” event?

我現在一直在撞牆擋住整整一天。 我正在研究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);
    }

這是LinkBut​​ton的代碼:

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

我嘗試過設置斷點,但從未達到過該事件。 LinkBut​​ton也會導致回發,但永遠不會觸發OnClick事件。

任何幫助將非常感激!

這是網址: http//www.northarktest.net/edwards/balloonshop

似乎click事件在服務器上觸發,但在本地調試時。

如果它幫助了我有類似問題的人,我的LinkBut​​ton Click事件(在后面的代碼中分配)永遠不會被觸發。 事實證明,由於我的LinkBut​​tons是動態創建的, 我不得不將它們移出我的Page_Load()事件並進入Page_Init()事件 完成后,LinkBut​​ton Click事件開始工作......與頁面生命周期和所有有趣的東西有關。

我有這個問題。 就我而言,問題是我為其中的字段添加了一些帶有驗證器的modalpopup 如果要進行驗證並且此驗證失敗(即使您無法看到,如我的情況),除非您將CausesValidation屬性聲明為false,否則任何按鈕都不會引發事件。

從查看代碼,我看不出任何問題。 您可以嘗試以下方法:

  1. 嘗試將onclick方法從: OnClick="AddToCartButton_Click1"更改為OnClick="AddToCartButton_Click" 只需刪除數字1.對代碼隱藏方法也一樣。

  2. 重建您的項目。

  3. 如果這不起作用,請通過Visual Studio設計視圖在頁面中拖動一個新按鈕,然后雙擊Button以生成事件處理程序。 然后將舊按鈕事件( AddToCartButton_Click1 )中的代碼現有代碼添加到新代碼中。

我猜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);

為什么? 點擊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

現在,如果您注意到URL中缺少頁面,則應該包含以下內容: abc.aspx?ProductId=22

對不起其他帖子...

我得到了答案,禮貌的書作者之一...謝謝Andrei Rinea先生......

在product.aspx中的Page_load事件的頂部添加:

AddToCartButton.PostBackUrl = Request.Url.AbsoluteUri;

注意:當我嘗試將產品添加到BalloonShop購物車時,我跳過了PayPal購物車並在本書后面遇到了此問題。

希望能幫助到你!

我知道這是一個老問題但是,如果你添加:

AddToCartButton.PostBackUrl = Request.RawUrl;

對於Page_Load,它將更正Url問題。

我遇到了這個問題如果我將LinkBut​​ton更改為Button,它會有效

問題是我在同一頁面上有一個PayPal按鈕,它有一個name =“submit”的屬性,它以某種方式干擾了回發。 我刪除了屬性,鏈接按鈕工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM