簡體   English   中英

asp.NET 按鈕有一個 onclick 事件但是按鈕和 onclick 事件之間的連接沒有建立

[英]asp.NET button has an onclick event but the connection between the button and the onclick event is not made

這是 productdetails.aspx 文件中的按鈕代碼:

    <asp:Button ID="Button2" runat="server" Text="Add to Wish List" 
                 CssClass="btn btn-success" OnClick="Button2_Click" />

這是 productdetails.aspx.cs 文件中的 onclick 事件

protected void Button2_Click(object sender, EventArgs e)
{
    if (temp != 1)
    {
        try
        {
            con.Open();
            string insertWish = "insert into wishlist (labelP, desP, priceP, QtyP, photoPath) values (@labelP, @desP, @priceP, @QtyP, @photoPath)";
            SqlCommand com = new SqlCommand(insertWish, con);
            com.Parameters.AddWithValue("@labelP", lblP.Text);
            com.Parameters.AddWithValue("@desP", desP.Text);
            com.Parameters.AddWithValue("@priceP", priceP.Text);
            com.Parameters.AddWithValue("@QtyP", QtyP.Text);
            com.Parameters.AddWithValue("@photoPath", Image1.ImageUrl);
            com.ExecuteNonQuery();
            Label.Text = "Product added to wish list sucessfully";
            con.Close();
        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.ToString());
         }
     }
 }

單擊按鈕時出現以下錯誤:

CS1061:“productdetails_aspx”不包含“Button2_Click”的定義,並且找不到接受“productdetails_aspx”類型的第一個參數的可訪問擴展方法“Button2_Click”(您是否缺少 using 指令或程序集引用?)

注意:文件鏈接到母版頁

注意:我沒有足夠的聲譽在評論框中提出建議。 因此,將建議張貼在答案部分

  • 檢查按鈕是否添加到aspx designer器文件中,如果該文件未刷新,則會導致此錯誤。
  • 清理並重建代碼。
  • 如果上述步驟不起作用,請嘗試更改按鈕命名。

暫無
暫無

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

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