简体   繁体   中英

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

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. 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. 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. and any CausesValidation set to true. If there is assign it false. Check this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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