gigasausage
asked on
Passing selected values from gridview to another page
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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="SqlDataSourc e1" HorizontalAlign="center" Width="500px" EmptyDataRowStyle-BorderSt yle="None" EmptyDataRowStyle-BorderWi dth="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("uploadpic tures.aspx ")
End Sub
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="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: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("uploadpic
End Sub
ASKER
BTW.. im programming in VB
ASKER
i think if this can be converted to VB it will work for me..
OnRowCommand="GridviewUnAs signed_OnR owCommand"
protected void GridviewUnAssigned_OnRowCo mmand(Obje ct sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "status")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandA rgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = this.GridViewUnassigned.Ro ws[index];
//convert the id data in cell 1 to int
int id = Convert.ToInt32(row.Cells[ 0].Text);
Session["appraisalfileid"] = id;
Server.Transfer("page2.asp x");
}
}
OnRowCommand="GridviewUnAs
protected void GridviewUnAssigned_OnRowCo
{
if (e.CommandName == "status")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandA
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = this.GridViewUnassigned.Ro
//convert the id data in cell 1 to int
int id = Convert.ToInt32(row.Cells[
Session["appraisalfileid"]
Server.Transfer("page2.asp
}
}
ASKER
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.CommandA rgument)
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.CommandA rgument)
'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.asp x")
End If
End Sub
"Input string was not in a correct format."
that happens on this line ..
Dim myindex As Integer = Convert.ToInt32(e.CommandA
this is the whole sub routine..
Protected Sub GridView1_RowCommand(ByVal
If e.CommandName = "Upload" Then
'Convert the row index stored in the CommandArgument
'property to an Integer.
Dim myindex As Integer = Convert.ToInt32(e.CommandA
'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(
Session("KEYID") = id
Server.Transfer("page2.asp
End If
End Sub
ASKER
it seems that there is no value being passed into CommandArgument. looking at the debugger, it shows "0"
ASKER
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.CommandA rgument)
'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("uploadpic tures.aspx ")
End If
End Sub
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False " DataSourceID="SqlDataSourc e1" onrowcommand="GridView1_Ro wCommand" HorizontalAlign="Center" Width="500px" EmptyDataRowStyle-BorderSt yle="None" EmptyDataRowStyle-BorderWi dth="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>
Protected Sub GridView1_RowCommand(ByVal
If e.CommandName = "Upload" Then
'Convert the row index stored in the CommandArgument
'property to an Integer.
Dim myindex As Integer = Convert.ToInt32(e.CommandA
'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(
Session("KEYID") = id
Server.Transfer("uploadpic
End If
End Sub
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="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>
there are other ways to do it but that was the quickest that sprung to mind... working long hours and not enough for ee...
ASKER