Gridview : passing the DataKey with CommandArgument

Last post 06-12-2008 2:40 AM by Sudhindra. 5 replies.

Sort Posts:

  • Gridview : passing the DataKey with CommandArgument

    10-09-2007, 11:32 AM
    • Loading...
    • MSchumacher
    • Joined on 07-13-2007, 7:00 PM
    • Montreal, Quebec, Canada
    • Posts 18

    I need to retreive the DataKey of the selected row in a GridView. Here's my situation :

    In a previous post ( http://forums.asp.net/t/1166794.aspx ), I asked help to add a clickable image in a gridview. The solution proposed in that post works and I did it, so I set that post resolved. Now, my problem is to access the DataKey. I know that, usually, I need to use the CommandArgument to retreive the index of the row, and then retreive different value, including the DataKey property (wich is the one I look for). Curiously, the CommandArgument passed in the OnRowCommand event is set to NULL. I trace it in the debugger, in the RowDataBound event, and it look ok at this stage; the command argument of the row having the index number that I can use further in the OnRowCommand event. But no matter if I let the default value of if I force one, I still get a null in the post back (OnRowCommand event).

    So, I suppose that it my problem is related with the solution I use ( http://forums.asp.net/t/1166794.aspx ), because the other clickable column forcing a postback passes a good CommandArgument (the index of the row). So, do someone have an idea what I should do to get the index from the CommandArgument ?

    Actually, the only way I found to get something from the CommandArgument parameter is to set it in the aspx page like this :

    <asp:TemplateField>

    <ItemTemplate>

    <asp:LinkButton ID="btnCopy" runat="server" CausesValidation="False" CommandName="CopyButton" CommandArgument='<%# Eval("DATAFIELD1")+ ";" + Eval("DATAFIELD2")%>'>

    <asp:Image ID="imgBtnCopy" runat="server" imageUrl="~/Common/Images/copy.gif" />

    </asp:LinkButton>

    </ItemTemplate>

    In this exemple, DATAFIELD1 and DATAFIELD2 are data column. With that solution, my problem is that I dont always have the key display in the data column. Sometime, as an exemple, a table may have a primary key that I want to be hidden (ex : next number). I can put this key in the DataKey property ... but I dont know if I can send it back, with the CommandArgument, in the aspx file using a syntaxe like CommandArgument='<%... %>'

    Thanks for help !

    M. Schumacher
  • Re: Gridview : passing the DataKey with CommandArgument

    10-09-2007, 2:21 PM
    • Loading...
    • MSchumacher
    • Joined on 07-13-2007, 7:00 PM
    • Montreal, Quebec, Canada
    • Posts 18

    Finaly, I found a solution. If someone has a better one, please send it.

    So, to solve my problem of a NULL CommandArgument, I found a way to set the index of the row selected. After, I use the index as normal to access the DataKeys. 

    <asp:TemplateField>

    <ItemTemplate>

    <asp:LinkButton ID="btnCopy" runat="server" CausesValidation="False" CommandName="CopyButton" CommandArgument='<%# GridView1.Rows.Count.ToString() %>'>

    <%--<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="CopyButton" CommandArgument='<%# Eval("UDCTable") %>'>--%>

    <asp:Image ID="imgBtnCopy" runat="server" imageUrl="~/Common/Images/copy.gif" />

    </asp:LinkButton>

    </ItemTemplate>

    </asp:TemplateField>

    M. Schumacher
  • Re: Gridview : passing the DataKey with CommandArgument

    10-09-2007, 2:24 PM
    • Loading...
    • CSharpSean
    • Joined on 10-21-2006, 5:43 AM
    • Orlando, FL
    • Posts 873

    Hello, 

    You can use the Click event of the linkbutton to get the row information from inside the gridview (if this is what you mean). 

    For example:

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            LinkButton btn = (LinkButton)sender;
            GridViewRow row = (GridViewRow)btn.NamingContainer;
            Response.Write("Row Index of Link button: " + row.RowIndex +
                           "DataKey value:" + GridView1.DataKeys[row.RowIndex].Value.ToString());
        }

      

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"  SortExpression="ID" />
                    <asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />
                    <asp:BoundField DataField="PHONE" HeaderText="PHONE" SortExpression="PHONE" />
                    <asp:BoundField DataField="CITY" HeaderText="CITY" SortExpression="CITY" />
                    <asp:BoundField DataField="ABBR" HeaderText="ABBR" SortExpression="ABBR" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Get Row Info</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

     

    Hope this helps. 

  • Re: Gridview : passing the DataKey with CommandArgument

    10-09-2007, 2:40 PM
    • Loading...
    • MSchumacher
    • Joined on 07-13-2007, 7:00 PM
    • Montreal, Quebec, Canada
    • Posts 18

    Thanks CSharpSean,

     I figure there's a way to get it with the Click event, but I didn't now how. Im pretty sure this information will serve me one day, but for now, I will keep my solution because I like to have all the postback, initiated by a click, in the OnRowCommand event.

    Thanks again

    M. Schumacher
  • Re: Gridview : passing the DataKey with CommandArgument

    03-05-2008, 12:00 PM
    • Loading...
    • N1ck73
    • Joined on 03-05-2008, 4:50 PM
    • Posts 2

    I like MSchumacher´s solution for this and I have used the same method, previously I achieved it using the RowDataBound event

    protected void _grid_RowDataBound(object sender, GridViewRowEventsArgs e) {

            if (e.Row.RowType == DataControlRowType.DataRow) {

                  //Button button = e.Row.Cells[0].FindControls("btnEdit") as ImageButton;

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

          }

    }

  • Re: Gridview : passing the DataKey with CommandArgument

    06-12-2008, 2:40 AM
    • Loading...
    • Sudhindra
    • Joined on 11-14-2007, 4:02 AM
    • Posts 6

    OK here is one of the way you can retrive DataKey. There can be only one DataKey.

     

    "<asp:GridView ID="grd1" runat="Server" width="500px" AutoGenerateColumns="false" DataKeyNames="StateID" OnRowEditing="grd1_RowEditing">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="btnDelete" runat="server" CommandArgument='<%#Eval("StateID")%>' OnCommand="lnkDelete" Text="Delete">
    </asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    Now the Code Behind:-
    protect void lnkDelete(Object sender,CommandArgument e)
    {
    	int iStID=int32.Parse(e.CommandArgument.ToString());
    }
    //iStID has the DataKey value which you can use. 
    Hope this helps.
    This can be done for a button too. 
     
     
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter