Solved

ASP.NET Gridview with linkbutton

Posted on 2009-04-06
5
2,720 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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