Link to home
Start Free TrialLog in
Avatar of Argiz
Argiz

asked on

ASP.NET Gridview with linkbutton

I have a gridview with linkbutton in it. But I'm hving trouble using the datakey value in my codebehind. I get an error "System.FormatException: Input string was not in a correct format."

Any help on accessing the datakeynames value?
<asp:GridView ID="GridTree" runat="server" DataSourceId="sdsTree" AutoGenerateColumns="False" BorderWidth="0" ShowHeader="false" DataKeyNames="xId">
                            <RowStyle BackColor="White" Height="14" VerticalAlign="Middle" Font-Size="X-Small" />
                            <AlternatingRowStyle BackColor="#B5B4B2" Height="14" VerticalAlign="Middle" Font-Size="X-Small" />
                            <Columns>
                                <asp:TemplateField ItemStyle-Width="173">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="linkTree" CommandName="NodeSelected" runat="server" style="text-decoration:none; color:#000000; "><%# Eval("xLabel") %></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>            
                        </asp:GridView>
 
 
    Protected Sub GridTree_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridTree.RowCommand
        Dim yNode As Integer
        yNode = Me.GridTree.DataKeys(Integer.Parse(e.CommandArgument.ToString())).Value
        Dim yChunk As Integer
        yChunk = xGetChunkId(yNode)
        Select Case e.CommandName
            Case "NodeSelected"
                If yChunk Then
                    ViewState("vsTreeParent") = Me.GridTree.DataKeys(Integer.Parse(e.CommandArgument.ToString())).Value
                    Call xShowTreeNodes(ViewState("vsTreeParent"))
                Else
                    xShowChunk(yChunk)
                End If
        End Select
    End Sub

Open in new window

Avatar of Anurag Agarwal
Anurag Agarwal
Flag of India image

Code looks good to me except one thing..
Put an if condition in the rowCommand event
if(e.CommandName=="NodeSelected")
then only execute your code
 
Anurag

 
Avatar of Obadiah Christopher
e.CommandArgument will be null and you are converting null into integer that's why the error.

Avatar of Argiz
Argiz

ASKER

And why will e.CommandArgument be NULL?
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Argiz

ASKER

Fixed. Thanks.