我动态创建了一个按钮,我希望当我的按钮点击和onclick事件被触发时,一个带有此事件的变量传递。
Button btn = new Button();
btn.Click += new EventHandler(Button_Click);
//btn.Click += new EventHandler(Button_Click(32)); <-- I want something like this
divUserUploadedList.Controls.Add(btn);
这是Onclick事件:
protected void Button_Click(object sender, EventArgs e)
{
//I want to access that value here
}
答案 0 :(得分:3)
您可以使用 CommandArgument 属性
执行此操作protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Click += btn_Click;
btn.CommandArgument = "12"; //<-- you can pass argument like this
divUserUploadedList.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string value = btn.CommandArgument;
}
答案 1 :(得分:2)
您正在寻找的可能是CommandArgument属性。