Datagrid (OnSelectedIndexChanged) Problem

Posted on 2007-08-06
Last Modified: 2013-11-05

I have a link button which says:
<asp:LinkButton id="lbQuote" runat="server" commandname="select">Quote</asp:LinkButton>

I have a datagrid which says:

And in code behind it says:
Sub getQuoted(ByVal src As Object, ByVal e As EventArgs)
    response.redirect("page1.aspx?id=" & DataGrid1.DataKeys(DataGrid1.SelectedIndex).ToString)
End Sub

What I need now is another link button, next to the existing link button which does something different depending on the row in the datagrid it is in...

For example

Sub getReported(ByVal src As Object, ByVal e As EventArgs)
    response.redirect("page2.aspx?id=" & DataGrid1.DataKeys(DataGrid1.SelectedIndex).ToString)
End Sub

How would I go about doing that?
Question by:narmi2
    LVL 28

    Expert Comment

    Hello narmi2,

    just add the CommandArgument property and assing a value to it
    also do the same for the lbQuote
    assign both to the getSelected event
    and before call Response.Redirect check the value of the

     Dim clickedButton as LinkButton = CType(src, LinkButton)
     if clickedButton.CommandArgument = '' then

    LVL 8

    Accepted Solution

    why cant you add your redirect code in aspx page itself. try something like this.

                                    <asp:HyperLink runat="server" NavigateUrl='<%# "~/Details.aspx?RID=" + DataBinder.Eval(Container.DataItem, "RegNo")%>' Text='<%# DataBinder.Eval(Container.DataItem, "LastName") %>'></asp:HyperLink>
    LVL 1

    Author Comment


    I now have this

            Dim clickedButton As LinkButton = CType(src, LinkButton)

            If clickedButton.CommandArgument = "caQuoted" Then
                Response.Redirect("reply.aspx?t=" & CInt(Request.QueryString("t")) & "&p=" & DataGrid1.DataKeys(DataGrid1.SelectedIndex).ToString)
                Response.Redirect("report.aspx?t=" & CInt(Request.QueryString("t")) & "&p=" & DataGrid1.DataKeys(DataGrid1.SelectedIndex).ToString)
            End If

    and it says:

    Specified cast is not valid

    when I click one or any of the 2 linkbuttons.
    LVL 28

    Expert Comment

    I think I was wrong
    You have to use no the onSelectedIndexChanged of the grid but the onclick events of the button
    on other hand hte sabeesh solution looks more nice and clear


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 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

    10 Experts available now in Live!

    Get 1:1 Help Now