How to get Row value on button Click

眉间皱痕 提交于 2019-12-25 05:08:16

问题


<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") %>' />

How do I get the ProjectID of the Row in which I click the close Project Button(btnCompleteProject) ?


回答1:


You can add an ItemCommand event to the repeater control and add a cole something like this:

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...

    }
}  

And add the repeater tag, you event:

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



回答2:


Add OnClick="btnCompleteProject_Click" to the button markup, and add a handler:

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

You could also listen to Repeater.ItemCommand, which should also catch the button click.




回答3:


Brian Mains's answer is correct, in the markup you are setting the ProjectID column's value in the buttons command argument. so in the button click with this code

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

you will get the ProjectID



来源:https://stackoverflow.com/questions/11312775/how-to-get-row-value-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!