0

I am a beginner when it comes to asp.net. I am trying since yesterday to find the answer to a question: How can I pass the rownumber of a gridview as a commandargument using asp.net?

I have tried many methods/ways, but none worked (the commandargument is null in code-behind), so I thought it would be proper to try with a javascript function (if there's a shorter,clearer way than using javascript, please tell me).

Here's the function :

     <script type = "text/javascript">
   function GetSelectedRow(lnk) {
       var row = lnk.parentNode.parentNode;
       var rowIndex = row.rowIndex - 1;
       alert("RowIndex: " + rowIndex);
       return rowIndex;
   }
</script>

And also:

    <ItemTemplate>
                <asp:Label ID="labelStatus" 
                           runat="server" 
                           Text='<%#Bind("Statut")%>'
                           CommandArgument='<%#"Eval(GetSelectedRow(lnk))"%>'>
                </asp:Label>
   </ItemTemplate>

The reason I need the row number is in OnRowCommand event handler, in order to be able to specify which row is being edited. ( myGridView.EditIndex = Convert.Int16(e.CommandArgument.ToString()); ).

Thank you in advance :)

SunnyDay
  • 317
  • 2
  • 8
  • 19

2 Answers2

1

You can do it on a server side in RowCreated handler:

protected void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Find the control which fires the command
        LinkButton button = (LinkButton)e.Row.Cells[0].FindControl("LinkButton1");

        button.CommandArgument = e.Row.RowIndex.ToString();
    }
}

Now after LinkButton1 is clicked the RowCommand event will be triggered, and you can access this CommandArgument in the handler:

protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    int rowIndex;
    if (int.TryParse(e.CommandArgument as string, out rowIndex))
    {
        // Do something with row index
    }
}
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • Makes sense what you just said, looks nice and clean, but it's still not working, because e.CommandArgument.ToString() is still null, in my case(I do not know why). Thank you guys, anyways for your nice suggestions. – SunnyDay Jul 26 '12 at 12:13
  • Can you show your Grid markup (especially events subscription and button that fires Command event) and code behind? It might help us to identify the problem. – Andrei Jul 26 '12 at 12:41
  • I just came back to thank you once again, I just solved my problem thanks to you. I did as you told me, but because of a small mistake of mine, it didn't work. But now it does! Multumesc :) – SunnyDay Jul 26 '12 at 12:49
0

you find here an full example about GridView and OnRowCommand

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowcommand.aspx

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • Thank you. But I cannot see where is the explicit value attribution of CommandArgument in asp page. That was my real problem :) that I didn't find how to pass the row number to the commandargument. – SunnyDay Jul 26 '12 at 08:53