Datagrid (OnSelectedIndexChanged) Problem

Hi

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

I have a datagrid which says:
onSelectedIndexChanged="getSelected"

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?
LVL 1
narmi2Asked:
Who is Participating?
 
sabeeshConnect With a Mentor Commented:
why cant you add your redirect code in aspx page itself. try something like this.

 <ItemTemplate>
                                <asp:HyperLink runat="server" NavigateUrl='<%# "~/Details.aspx?RID=" + DataBinder.Eval(Container.DataItem, "RegNo")%>' Text='<%# DataBinder.Eval(Container.DataItem, "LastName") %>'></asp:HyperLink>
                            </ItemTemplate>
0
 
mmarinovCommented:
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
.....

Regards,
Martin
0
 
narmi2Author Commented:
Thanks

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)
        Else
            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.
0
 
mmarinovCommented:
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

Martin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.