Home > DeveloperSection > Forums > Button Event Handler Not Working
Ashish Pandey

Total Post:128

Posted on    November-20-2014 10:30 PM

 C# C# 

 1 Reply(s)
 508  View(s)
Rate this:

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();


            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();


                newcell2.Text = row["author"].ToString();



                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);








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.

Sumit Kesarwani

Total Post:378

Posted on    November-20-2014 11:44 PM

Hi Ashish,



   Button b = new Button();

   b.ID = topic.Topic_Id + "_1"; // topic_Id is my unique ID for each topic on the blog

   b.Text = "Edit";

   b.ToolTip = "Edit";

   b.CommandArgument = b.ID; //passing this to event handler

   b.Command += new CommandEventHandler(b_Command); //handler


void b_Command(object sender, CommandEventArgs e)




Don't want to miss updates? Please click the below button!

Follow MindStick