按钮事件处理程序不工作 [英] Button Event Handler Not Working

查看:118
本文介绍了按钮事件处理程序不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用code的一个按钮以下行 C#

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

             }

我正在使用表格单元格动态添加的按钮上面的code。但上面的事件处理程序不工作或者被解雇。我使用 asp.net C#首次。有人可以帮我吗?谢谢你。

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.

推荐答案

这里是answer.Try它

here is the answer.Try it

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

这篇关于按钮事件处理程序不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆