动态设置Asp:button命令参数

时间:2015-06-03 12:23:23

标签: c# asp.net

是否无法在.aspx页面上设置CommandArgument属性? 我读过它与viewstate有关吗?

将参数发送到按钮点击事件有哪些替代方法?

<% foreach (var s in person.Where(s => s.TableID == t.TableID)) { %>

<asp:Button ID="Button1" runat="server" BorderWidth="0" Text="Remove" CommandArgument="<%= s.Person.FirstName %>" oncommand="Button1_Command"  />

 <% } %>

2 个答案:

答案 0 :(得分:0)

你可以这样做:

<asp:Button ID="Button1" runat="server" BorderWidth="0" Text="Remove" 
CommandArgument='<%# s.Person.FirstName %>' oncommand="Button1_Command"  />

答案 1 :(得分:-1)

这是替代

<% foreach (var s in person.Where(s => s.TableID == t.TableID)) { %>

  <input type="button" name="<%= s.Person.FirstName %>" 
  runat="server" value="Remove" onServerClick="Button1_Command" />


   <% } 
%>

然后在服务器端

 protected void Button1_Command(object sender, EventArgs e)
  {  
    string val = "clicked value is is: "+ ((HtmlInputControl)sender).Name; 
  }

刚编辑 - 这将有效