Link to home
Create AccountLog in
Avatar of spectre921
spectre921Flag for United States of America

asked on

Use select button in gridview to move to a new page

I am fairly new to 2.0.  I have run into a problem with the gridview and I need help.  I have a drop down window.  I choose an item in the window and the gridview populates information based on the item that I have chosen.  Now, I want to click on the select button next to the row in the gridview and display a new page with detail information pertaining to that one row.  This detail information that is being displayed on a different page needs to pull data from a different table based on a particular field in the row on the previous page that I have selected.  I hope this makes sense.  Can anyone help?
Avatar of Dustin Hopkins
Dustin Hopkins
Flag of United States of America image

Without being too particular could you not use a hyperlink column to send querystring parameters to the other page?
If you don't want to do it that way, then you could set up the select button to send your datakey to a function that sets the key as a session variable and then does a transfer or redirect to the other page, where the session variable is then used to query the database for your item.
Add a Button Template to the GridView:

                    <asp:Button ID=btnSelect runat=server OnClick="btnSelect_Click" />
                <ItemStyle HorizontalAlign="Center" />

Protected Sub btnSelect_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim btnSelect As Button = CType(sender, Button)
        Dim row As GridViewRow = CType(btnSelect.NamingContainer, GridViewRow)

       Dim gvIDs As String = ""
       gvIDs += gv.Cells.Item([the data required to redirect]).Text

        Response.Redirect("Page.aspx?str=" & gvIDs)

End Sub

Hope this helps.
Avatar of spectre921


You previously mentioned a query parameter to send me to the next page.  I think you are right on target with my problem.  I have tried to do that.  I have dug around in books and on the Internet enough that I believe that is the way I should go.  But, I'm just not successful with it.  I now have the hyperlink set up in a column next to each row.  In that hyperlink, I have the id number that needs to be carried to the detail page.  I can click on the hyperlink (id number) and I do get the detail page with the id number showing in the url.  But, I do not get any data pertaining to that id number to display on the detail page.  This data that should be displaying on the detail page is being pulled from a different table that contains the id number from the main page.  I've checked the sql statements and can see that the where clause is inserted referencing the id number from the main table.  I keep looking at the code from the details page and can't see anything wrong with it.  Help!  
Are you grabbing the querystring?
Your url should look like this: id
Then your datasource can be set up with a querystring parameter.

<asp:SqlDataSource ID="datasourceid" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnectionString %>"
    SelectCommand="SELECT ItemId, ItemName, ItemDetail  FROM tbl_items WHERE (itemId = @id)">
           <asp:QueryStringParameter DefaultValue="" Name="id" QueryStringField="id" />

To tell you anything beyond thatwould require me to see some of your code.
My url does look like your example.

I have compared the piece of code that you added to what I have.  It is exactly the same with exception to naming convention.  I still can't see the detail information on the detail page.

Here is a sample of a few pieces of my code:

Here is the url:

Here is the hyperlink:
<asp:HyperLinkField HeaderText="XXXCustNo" DataTextField="XXXCustNo" DataNavigateUrlFields="XXXCustNo" DataNavigateUrlFormatString="FloatDetail.aspx?id={0}" >
   <HeaderStyle HorizontalAlign="Center" />

Here is the query parameter:
<asp:SqlDataSource ID="DetailsSqlDataSource" runat="server"
   ConnectionString="<%$ ConnectionStrings:FCNConnectionString %>"
   OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Client_Assigned_Float] WHERE ([XXXCustNo] = @XXXCustNo)">
     <asp:QueryStringParameter DefaultValue="" Name="XXXCustNo" QueryStringField="XXXCustNo"
        Type="Int32" />

I hope this helps...

I have to leave in 15 minutes and won't be back until Tuesday morning...  If I don't see a response from you by then, I will respond on Tuesday morning...  Thanks in advance for all your help.  I really appreciate it.
I should also mention that I don't receive any errors when I look at these pages in the browser.  FYI...
Since you're using OldValuesParameterFormatString="original_{0}" then your querystring needs to look like this.
...Float] WHERE ([XXXCustNo] = @original_XXXCustNo)">...
That should be the only thing that would need to be changed. Let me know how it turns out.
My apologies that would be for an update command. A select command will probably error out. Let me look at this some more over the weekend. I've replicated most of your situation and it seems to work fine on my end.
Avatar of Dustin Hopkins
Dustin Hopkins
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks so much.  Changing "XXXCustNo" to "id" worked.  That makes sense too.  I can't believe I overlooked reading the parameter that I have passed.  Sometimes, I have a hard time seeing "the forest through the trees" so to speak.  Thanks again.  I appreciate it.