简体   繁体   English

为什么按钮不触发点击事件

[英]Why does the button not fire a click event

Following is my code. 以下是我的代码。 It's not giving me desired result. 这没有给我想要的结果。 I can't understand what is the problem here. 我不明白这里出了什么问题。 Please tell me what's wrong here. 请告诉我这里怎么了。 The click event for btnbookavil is not being fired and there is no error in output 不会触发btnbookavil的click事件,并且输出中没有错误

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LibraryConnectionString"].ConnectionString);
protected void btnsubmit_Click(object sender, EventArgs e)
{
    con.Open();
    SqlCommand cmd = new SqlCommand("bookinsertion", con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@idnumber",txtid.Text);
    cmd.Parameters.AddWithValue("@name", txtname.Text);
    cmd.Parameters.AddWithValue("@year", txtyear.Text);
    cmd.Parameters.AddWithValue("@department", txtdepart.Text);
    cmd.Parameters.AddWithValue("@bookname", txtbook.Text);           
    cmd.ExecuteNonQuery();       
    con.Close();
    Response.Redirect("~/LendingForm2.aspx");
}
protected void btnbookavail_Click(object sender, EventArgs e)
{
    con.Open();
    SqlCommand cmd = new SqlCommand("availablebooks", con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@bookname", txtbook.Text);
    SqlParameter output=new SqlParameter();
    output.ParameterName="@BooksAvailable";
    output.SqlDbType=System.Data.SqlDbType.Int;
    output.Direction=System.Data.ParameterDirection.Output;
    cmd.Parameters.Add(output);          
    cmd.ExecuteNonQuery();
    con.Close();
    string bookavail = output.Value.ToString();
    if (Convert.ToInt32(bookavail) != 0 && Convert.ToInt32(bookavail) > 0)
    {
        lblbookavail.Visible = true;
        lblbookavail.Text = bookavail + "books are available";
    }
    else
    {
        lblbookavail.Text = "No books available";
    }
}

I can't say much without understanding entire problem. 不了解整个问题我不能说太多。 But if by no error in output you mean you are not getting the error message in case there is no book available, I suggest set the visibility of your control true as it appears that its not visible and gets visible only if books are available. 但是,如果在输出中没有错误,则意味着在没有可用的书的情况下您没有收到错误消息,我建议将控件的可见性设置为true,因为它看起来不可见,并且仅在有书时才可见。 So in case book is not found, you set the text property of your label to "No books available" butyou don't set its visibility true. 因此,如果找不到书,则将标签的text属性设置为“无可用书”,但不要将其可见性设置为true。 So make your label visible irrespective of result. 因此,无论结果如何,都应使标签可见。

lblbookavail.Visible = true;
if (Convert.ToInt32(bookavail) != 0 && Convert.ToInt32(bookavail) > 0)
       {           
           lblbookavail.Text = bookavail + "books are available";
       }
       else
       {
           lblbookavail.Text = "No books available";
       }

also check if there is an onclick event specified in your button. 还检查按钮中是否指定了onclick事件。 and any CausesValidation set to true. 并将所有CausesValidation设置为true。 If there is assign it false. 如果有分配,则为false。 Check this 检查一下

也许,也许吧,设计器属性中的按钮名称与您在后面的代码中拥有的名称不匹配,请确认。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM