'asp.NET button has an onclick event but the connection between the button and the onclick event is not made
Here is the button code in the productdetails.aspx file:
<asp:Button ID="Button2" runat="server" Text="Add to Wish List"
CssClass="btn btn-success" OnClick="Button2_Click" />
Here is the onclick event in the productdetails.aspx.cs file
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());
}
}
}
When clicking the button the following error appears:
CS1061: 'productdetails_aspx' does not contain a definition for 'Button2_Click' and no accessible extension method 'Button2_Click' accepting a first argument of type 'productdetails_aspx' could be found (are you missing a using directive or an assembly reference?)
Note: the files are linked to a master page
Solution 1:[1]
Note: I do not have enough reputation to give suggestions in the comments box. So, posting the suggestions in the Answer Section.
- Check the button is added in the
aspx designer
file, this error causes if that file is not refreshed. - Clean and rebuild the code.
- try changing the button naming if the above steps didn't work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | EnthusiasticLearner |