Pages

Saturday 26 January 2013

ASP.Net GridView - Get CommandName and CommandArgument of Button clicked in RowCommand and Click events

Get GridView Row and GridView Row Index on RowCommand Event
Below is a simple ASP.Net GridView Control with a ButtonField and an OnRowCommand Event
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" 
    OnRowCommand = "OnRowCommand">
 <Columns>
  <asp:ButtonField CommandName = "ButtonField"  DataTextField = "CustomerID"
        ButtonType = "Button"/>
 </Columns>
</asp:GridView>
Now when the Button of the button is clicked the OnRowCommand event is executed.
C#
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow gvRow = GridView1.Rows[index]; 
}
VB.Net
Protected Sub OnRowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    Dim gvRow As GridViewRow = GridView1.Rows(index)
End Sub
The RowIndex of the GridView Row is contained in the CommandArgument Property. Thus with that index you can get the reference of the GridView Row.
Get GridView Row and GridView Row Index on Click Event of Button in TemplateField
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
    <Columns>
        <asp:TemplateField>
        <ItemTemplate>
           <asp:Button ID="Button1" runat="server" Text='<%#Eval("CustomerID")%>'
                  CommandArgument = "Button1"  OnClick = "Button1_Click" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Now when the Button is clicked the Click event of the Button is executed.
C#
protected void Button1_Click(object sender, EventArgs e)
{
    GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
    int index = gvRow.RowIndex;
}
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim gvRow As GridViewRow = CType(CType(sender, Control).Parent.Parent, _
                                        GridViewRow)
    Dim index As Integer = gvRow.RowIndex
End Sub
You will notice I am type casting the sender parameter to get the reference of the GridView Row. The sender is the Button Control, thus the parent of the Button Control is the Cell and the parent of the Cell is the GridView Row.
Button ----------> GridView Cell ----------> GridView Row
The same applies to LinkButton and ImageButton.
   
Get CommandArgument on Click Event of Button in TemplateField
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
    <Columns>
        <asp:TemplateField>
        <ItemTemplate>
           <asp:Button ID="Button1" runat="server" Text='<%#Eval("CustomerID")%>'
                  CommandArgument = "Button1"  OnClick = "Button1_Click" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Many occasions you will need the properties of the Button like its CommandName, CommandArgument. So in that case you will need to get the reference of the Button using the sender parameter.
C#
protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    string CommandName = btn.CommandName;
    string CommandArgument = btn.CommandArgument;
}
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
   Dim btn As Button = CType(sender, Button)
   Dim CommandName As String = btn.CommandName
   Dim CommandArgument As String = btn.CommandArgument
End Sub
Same can be done with LinkButtons and ImageButtons by creating an object and assigning the sender after type casting
   
Multiple values in CommandArgument
Now by default there’s no property to do it we need to use a simple string of concatenation and then splitting them into arrays Consider the following GridView
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
    <Columns>
        <asp:TemplateField>
        <ItemTemplate>
         <asp:Button ID="Button1" runat="server" Text='<%#Eval("CustomerID")%>'
          CommandArgument=
          '<%#Eval("PostalCode") + "," + Eval("City") + "," + Eval("Country") %>' 
              OnClick = "Button1_Click" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Now to get these values you need to do the following
C#
protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    string[] CommandArgument = btn.CommandArgument.Split(',');
    string CommandArgument1 = CommandArgument[0];
    string CommandArgument2 = CommandArgument[1];
    string CommandArgument3 = CommandArgument[2];
}
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
   Dim btn As Button = CType(sender, Button)
   Dim CommandName As String = btn.CommandName
   Dim CommandArgument As String() = btn.CommandArgument.Split(",")
   Dim CommandArgument1 As String = CommandArgument(0)
   Dim CommandArgument2 As String = CommandArgument(1)
   Dim CommandArgument3 As String = CommandArgument(2)
End Sub
 

No comments:

Post a Comment