如何将值传递给 c# 中的图像按钮单击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/920381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:13:24 来源:igfitidea点击:
How Do I pass Values into an imagebutton click event in c#?
提问by Roger Walsh
Trying to pass some values into an imagebutton click event, something like this:
尝试将一些值传递给 imagebutton 单击事件,如下所示:
<asp:ImageButton id="imagebutton1" runat="server" AlternateText="5 Star Luxury Villas in North Cyprus" ImageUrl="/download/1/luxury_villas.jpg" OnClick="ImageButton_Click('value1', 'value2')"/>
then in code behind:
然后在后面的代码中:
protected void ImageButton_Click(object sender, ImageClickEventArgs e, string value1, string value2)
{
Response.Write(Value2);
}
采纳答案by Eoin Campbell
Try using OnCommand
instead of OnClick
尝试使用OnCommand
代替OnClick
Then you can specify values in the CommandName
& CommandArgument
Properties
然后您可以在CommandName
&CommandArgument
属性中指定值
<asp:ImageButton ID="blah" runat="server" OnCommand="blah_command" CommandName="val1" CommandArgument="val2,val3,val4" />
And
和
protected void blah_Command(object sender, CommandEventArgs e)
{
string val1 = e.CommandName.ToString();
string [] othervals = e.CommandArgument.ToString().Split(',');
}