Pradit
Pradit

Reputation: 719

How to get Row value on button Click

<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) ?

Upvotes: 0

Views: 3543

Answers (3)

Sunil Babu
Sunil Babu

Reputation: 13

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

Upvotes: 0

Felipe Oriani
Felipe Oriani

Reputation: 38598

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>

Upvotes: 2

Brian Mains
Brian Mains

Reputation: 50728

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.

Upvotes: 0

Related Questions