forum

Home / DeveloperSection / Forums / Button Event Handler Not Working

Button Event Handler Not Working

Anonymous User 1529 20-Nov-2014

I am using the following lines of code for a button in C# : 

void reserve_click(object sender, EventArgs e)
{
   string req = ((Button)sender).ID;
}
 
 protected void Button2_Click(object sender, EventArgs e)
        {
            issuedBooks.Visible = false;
            search.Visible = true;
            string text = TextBox1.Text;
            string selectCommand = "SELECT id, title, author FROM book WHERE title LIKE '%" + text + "%' OR author LIKE '%" + text + "%'";
            string conString = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            SqlDataAdapter dad = new SqlDataAdapter(selectCommand, conString);
            DataTable dtblCategories = new DataTable();
            dad.Fill(dtblCategories);
            DataView view = new DataView(dtblCategories);
 
 
 
            foreach (DataRowView row in view)
            {
                TableRow newrow = new TableRow();
                TableCell newcell1 = new TableCell();
                TableCell newcell2 = new TableCell();
                TableCell newcell3 = new TableCell();   
                newcell1.Text = row["title"].ToString();
                newrow.Cells.Add(newcell1);
                newcell2.Text = row["author"].ToString();
                newrow.Cells.Add(newcell2);
 
                string book_id = row["id"].ToString();
 
                Button btn = new Button();
                btn.ID = "Button_1" + book_id;
                btn.Text = "Reserve";
                btn.Click += new EventHandler(reserve_click);
 
                newcell3.Controls.Add(btn);
                newrow.Cells.Add(newcell3);
 
                search.Rows.Add(newrow);
 
             }

I am using the above code in a dynamically added button in a table cell. But the above EventHandler is not working or getting fired. I am using asp.net and C#for the first time. Can someone help me out ? Thanks.


c# c# 
Updated on 20-Nov-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By