如何在按钮上获取行值单击

时间:2012-07-03 14:11:30

标签: asp.net button repeater

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"
  OnItemDataBound="Repeater1_ItemDatabound">
    <ItemTemplate>
      <tr>
        <td>
          <%#Eval("Priority") %>
        </td>
        <td>
        <%#Eval("ProjectName") %>
        </td>                 
        <td>
          <%#Eval("DisplayName") %>
        </td>
        <td>
          <asp:HyperLink ID="HyperLink1" runat="server"
            NavigateUrl='<%# Eval("EmailID" , "mailto:{0}") %>'
            Text='<%# Eval("EmailID") %>'></asp:HyperLink>
        </td>
        <td>
          <%#Eval("ProjectID") %>
        </td>
        <td>
          <asp:Button ID="btnCompleteProject" runat="server" Text="Close Project"
            OnCommand="CloseProject"  CommandName="Close"
            CommandArgument='<%# Eval("ProjectID") %>' />

如何获取我单击关闭项目按钮(btnCompleteProject)的Row的ProjectID?

3 个答案:

答案 0 :(得分:2)

您可以在转发器控件中添加ItemCommand event并添加类似这样的cole:

public void Repeater1_ItemCommand(Object Sender, RepeaterCommandEventArgs e) 
{
    // check if the command name is close (if it's the button)
    if (e.CommandName == "Close") {
       // get CommandArgument you have seeted on the button
       int projectd = (int)e.CommandArgument;

       // your code here...

    }
}  

并添加转发器标记,即事件:

<asp:Repeater ID="Repeater1" 
   runat="server" 
   DataSourceID="SqlDataSource1" 
   OnItemDataBound="Repeater1_ItemDatabound" 
   OnItemCommand="Repeater1_ItemCommand">
...
</asp:Repeater>

答案 1 :(得分:0)

OnClick="btnCompleteProject_Click"添加到按钮标记,然后添加处理程序:

protected void btnCompleteProject_Click(object sender, EventArgs e)
{
    var argValue = (int)((Button)sender).CommandArgument;
}

您还可以收听Repeater.ItemCommand,它也应该点击按钮。

答案 2 :(得分:0)

Brian Mains的回答是正确的,在标记中你在按钮命令参数中设置了ProjectID列的值。所以在按钮中单击此代码

protected void btnCompleteProject_Click(object sender, EventArgs e)
 {
   var argValue = (int)((Button)sender).CommandArgument;
 }

您将获得ProjectID