Solved

ASP.NET Gridview with linkbutton

Posted on 2009-04-06
5
2,723 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Argiz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:Anurag Agarwal
ID: 24076694
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

 
0
 
LVL 20

Expert Comment

by:informaniac
ID: 24076905
e.CommandArgument will be null and you are converting null into integer that's why the error.

0
 

Author Comment

by:Argiz
ID: 24077264
And why will e.CommandArgument be NULL?
0
 
LVL 20

Accepted Solution

by:
informaniac earned 500 total points
ID: 24077354
Because you'll have to write the linkbutton tag like this

<asp:LinkButton ID="linkTree" CommandName="NodeSelected" runat="server" style="text-decoration:none; color:#000000; " CommandArgument='<% #Eval("xId") %'> ><%# Eval("xLabel") %></asp:LinkButton>

And in codebehind like this

ViewState("vsTreeParent") = e.CommandArgument.ToString()
0
 

Author Closing Comment

by:Argiz
ID: 31566982
Fixed. Thanks.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question