Solved

ASP.NET Gridview with linkbutton

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

626 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