Solved

Passing selected values from gridview to another page

Posted on 2006-10-20
9
1,487 Views
Last Modified: 2008-01-09
I have 2 pages...

Page 1 has a gridview on it which lists an item.

If you click the "select" button for said item, it opens a formview below it with all the detail information.

I want to add another colum in the gridview that has a button in it so that when you click that button it sends the selectedvalue of the gridview to a second page. I want to use that value to filter my sqldatasource on page2.

I can not figure out how to send the selected value of page 1 to page 2.
0
Comment
Question by:gigasausage
  • 7
  • 2
9 Comments
 
LVL 12

Accepted Solution

by:
deanvanrooyen earned 125 total points
Comment Utility
you can add a template button with some code like this
OnRowCommand="GridviewUnAssigned_OnRowCommand"

    protected void GridviewUnAssigned_OnRowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "status")
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked
            // by the user from the Rows collection.
            GridViewRow row = this.GridViewUnassigned.Rows[index];

            //convert the id data in cell 1 to int
            int id  = Convert.ToInt32(row.Cells[0].Text);
            Session["appraisalfileid"] = id;
            Server.Transfer("page2.aspx");
        }
    }


                    <asp:GridView ID="GridViewUnassigned" runat="server" AutoGenerateColumns="False" Font-Size="X-Small"
                        Height="80px" OnRowCommand="GridviewUnAssigned_OnRowCommand" Width="786px" Caption="Unassigned">
                        <Columns>
                            <asp:BoundField DataField="id" HeaderText="ID">
                                <ItemStyle Width="25px" HorizontalAlign="Left" />
                                <HeaderStyle BackColor="Silver" BorderColor="Silver" Width="25px" HorizontalAlign="Left" />
                            </asp:BoundField>
                            <asp:ButtonField DataTextField="status" HeaderText="Status" CommandName="status" Text="go to detail">
                                <ItemStyle HorizontalAlign="Left" Width="120px" />
                                <HeaderStyle BackColor="Silver" BorderColor="Silver" HorizontalAlign="Left" Width="120px" />
                            </asp:ButtonField>
                        </Columns>
                    </asp:GridView>

this is 2 basic ways to do it - transfer through session
            Session["appid"] = id;
            Server.Transfer("page2.aspx");
on page 2 - int x = (int)Session["appraisalfileid"]

or  Server.Transfer("page2.aspx?id= " +  id.ToString());
then in page 2  string x = Request.QueryString["id"].ToString();
0
 

Author Comment

by:gigasausage
Comment Utility
I am not sure the issue here, but the ASP Tag <asp:ButtonField> is not a known element. I am using ASP.net 2.0. If you are using 1.0 I am wondering if it was removed??
0
 

Author Comment

by:gigasausage
Comment Utility
Here is my gridview, you can see I have a template field with a button on it that I have assigned an on-click event to. When this fires i set a session variable = to the selectedvalue of the gridview, problem is that if the user doesnt first make a selection, then the selectedvalue of gridview1 = nothing.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" HorizontalAlign="center" Width="500px" EmptyDataRowStyle-BorderStyle="None" EmptyDataRowStyle-BorderWidth="0" DataKeyNames="KEYID">
  <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="KEYID" HeaderText="Listing #" InsertVisible="False" ReadOnly="True" SortExpression="KEYID" />
    <asp:BoundField DataField="txtAddress" HeaderText="Address" SortExpression="txtAddress" />
    <asp:BoundField DataField="isActive" HeaderText="Active" SortExpression="isActive" />
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Button ID="Button2" runat="server" Text="Upload Pictures" OnClick="Button2_Click" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
  <RowStyle HorizontalAlign="Center" />
  <EmptyDataRowStyle BorderStyle="None" BorderWidth="0px" />
</asp:GridView>

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Session("KEYID") = GridView1.Selectedvalue
        Server.Transfer("uploadpictures.aspx")

    End Sub
0
 

Author Comment

by:gigasausage
Comment Utility
BTW.. im programming in  VB
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:gigasausage
Comment Utility
i think if this can be converted to VB it will work for me..

OnRowCommand="GridviewUnAssigned_OnRowCommand"

    protected void GridviewUnAssigned_OnRowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "status")
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked
            // by the user from the Rows collection.
            GridViewRow row = this.GridViewUnassigned.Rows[index];

            //convert the id data in cell 1 to int
            int id  = Convert.ToInt32(row.Cells[0].Text);
            Session["appraisalfileid"] = id;
            Server.Transfer("page2.aspx");
        }
    }
0
 

Author Comment

by:gigasausage
Comment Utility
Going through this I have converted it to VB myself, though I am getting an error which doesnt make sense, since the code is basically copied from the Microsoft site itself http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

"Input string was not in a correct format."

that happens on this line ..

Dim myindex As Integer = Convert.ToInt32(e.CommandArgument)

this is the whole sub routine..

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

        If e.CommandName = "Upload" Then

            'Convert the row index stored in the CommandArgument
            'property to an Integer.
            Dim myindex As Integer = Convert.ToInt32(e.CommandArgument)

            'Retrieve the row that contains the button clicked
            'by the user from the Rows collection.
            Dim row As GridViewRow = GridView1.Rows(myindex)

            'convert the id data in cell 1 to int
            Dim id As Integer = Convert.ToInt32(row.Cells(2).Text)
            Session("KEYID") = id
            Server.Transfer("page2.aspx")
        End If
    End Sub
0
 

Author Comment

by:gigasausage
Comment Utility
it seems that there is no value being passed into CommandArgument. looking at the debugger, it shows "0"
0
 

Author Comment

by:gigasausage
Comment Utility
i just got it.. completed code as follows..

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

        If e.CommandName = "Upload" Then

            'Convert the row index stored in the CommandArgument
            'property to an Integer.
            Dim myindex As Integer = Convert.ToInt32(e.CommandArgument)

            'Retrieve the row that contains the button clicked
            'by the user from the Rows collection.
            Dim row As GridViewRow = GridView1.Rows(myindex)

            'convert the id data in cell 1 to int
            Dim id As Integer = Convert.ToInt32(row.Cells(1).Text)
            Session("KEYID") = id
            Server.Transfer("uploadpictures.aspx")
        End If
End Sub

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand" HorizontalAlign="Center" Width="500px" EmptyDataRowStyle-BorderStyle="None" EmptyDataRowStyle-BorderWidth="0" DataKeyNames="KEYID" EnableViewState="False">
                                <Columns>
                                    <asp:CommandField ShowSelectButton="True" />
                                    <asp:BoundField DataField="KEYID" HeaderText="Listing #" InsertVisible="False" ReadOnly="True" SortExpression="KEYID" />
                                    <asp:BoundField DataField="txtAddress" HeaderText="Address" SortExpression="txtAddress" />
                                    <asp:BoundField DataField="isActive" HeaderText="Active" SortExpression="isActive" />
                                    <asp:ButtonField ButtonType="Button" CommandName="Upload" HeaderText="Pictures" ShowHeader="True" Text="Upload Pictures" />
                                </Columns>
                                <RowStyle HorizontalAlign="Center" />
                                <EmptyDataRowStyle BorderStyle="None" BorderWidth="0px" />
                            </asp:GridView>
0
 
LVL 12

Expert Comment

by:deanvanrooyen
Comment Utility
there are other ways to do it but that was the quickest that sprung to mind... working long hours and not enough for ee...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

12 Experts available now in Live!

Get 1:1 Help Now