Solved

ASP.NET Gridview with linkbutton

Posted on 2009-04-06
5
2,713 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:anuragal
Comment Utility
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
Comment Utility
e.CommandArgument will be null and you are converting null into integer that's why the error.

0
 

Author Comment

by:Argiz
Comment Utility
And why will e.CommandArgument be NULL?
0
 
LVL 20

Accepted Solution

by:
informaniac earned 500 total points
Comment Utility
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
Comment Utility
Fixed. Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now