BUTTON EVENT HANDLER NOT WORKING

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
C# 
C#
 786  View(s)
Ratings:
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();

            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.

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Button Event Handler Not Working

    Hi Ashish,


    Page_Load()

    {

       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)

    {

        System.Windows.Forms.MessageBox.Show(e.CommandArgument.ToString());

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!