I have a gridview with a list of cars. The car make is a hyperlink which goes to the next page.
On the next page I have a label which says "your chosen car is:" then I have a label. I want the selected car from the gridview on the previous page to appear in place of the label.
Try to catch the querystring value and display in the label like shown below. Please make sure the QueryString key in the previous page need to match with what you have in getting the value
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Get the Query String value
Dim qryString As String = Request.QueryString("CustomerMake")
If Not String.IsNullOrWhiteSpace(qryString) Then
'Assign to Label Control
Label1.Text = qryString
End If
End Sub
you have 2 pages named ( ListofCars.aspx & CarsDetails.aspx )
Then In ListOfCars.aspx you have GridView (you will find the Option of GridView Paging, Selection, Delete etc)
Choose The "Selection" Option then.
Click on GridView Design View And you will find "Events" on "PageIndexChanging"
write this Code :
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex; // GridView1 depands on you may be you have the Different ID of GridView but default is GridView1
}
Then. write the Code on "SelectedIndexChanging"
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int newIndex = e.NewSelectedIndex;
string idGridview = GridView1.Rows[newIndex].Cells[1].Text; // Cell[1] means if you have 3 Columns Like ID, Name, ModelofCars then Cell[0] = ID and Cell[1] = Name And Cell[2] = ModelOfCars
Response.Redirect("CarsDetails.aspx?CarID=" + idGridview);
}
Now, you have the QueryString Passed Succesfully.
On CarsDetails.aspx Page Load you will have to write this Code:
please refer that article and find out different options like QueryStrings, Session, Cookies etc to share information...opt for the one which better suits you.,..
Protected Sub GridView1_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
End Sub
Protected Sub GridView1_SelectedIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int newIndex = e.NewSelectedIndex;
string idGridview = GridView1.Rows[newIndex].Cells[0].Text;
Response.Redirect("Reservations.aspx?CarID=" + idGridview);
}
End Sub
Then the reservations page code is:
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
string QueryCarID = Request.QueryString["CarID"];
YourLabel.Text = QueryCarID;
End Sub
uzzy
0 Points
116 Posts
Gridview selection to Label
Apr 16, 2012 10:25 PM|LINK
Hi
I have a gridview with a list of cars. The car make is a hyperlink which goes to the next page.
On the next page I have a label which says "your chosen car is:" then I have a label. I want the selected car from the gridview on the previous page to appear in place of the label.
Please can someone advise me on how to do this?
I am using VB
Thanks in advance
</div>sriramabi
Contributor
4351 Points
1277 Posts
Re: Gridview selection to Label
Apr 16, 2012 10:30 PM|LINK
hi
u r problem is alredy solve other answer
http://forums.asp.net/t/1792904.aspx/1?Gridview+selection+to+dropdownlist
thank u
uzzy
0 Points
116 Posts
Re: Gridview selection to Label
Apr 16, 2012 11:13 PM|LINK
Only half the problem was solved mate. The second half isn't solved yet.
Also I made an error I wanted it in a label and not a dropdownlist so I thought I would make a new thread to avoid confusion.
Bimalvv
Contributor
2356 Points
478 Posts
Re: Gridview selection to Label
Apr 17, 2012 12:18 AM|LINK
Try to catch the querystring value and display in the label like shown below. Please make sure the QueryString key in the previous page need to match with what you have in getting the value
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'Get the Query String value Dim qryString As String = Request.QueryString("CustomerMake") If Not String.IsNullOrWhiteSpace(qryString) Then 'Assign to Label Control Label1.Text = qryString End If End SubBimal
MahadPK
Participant
778 Points
225 Posts
Re: Gridview selection to Label
Apr 17, 2012 12:25 AM|LINK
Hey Uzzy Good Day :)
its the Complete Solution of your Prob. :)
listen and concentrate The scenario :
you have 2 pages named ( ListofCars.aspx & CarsDetails.aspx )
Then In ListOfCars.aspx you have GridView (you will find the Option of GridView Paging, Selection, Delete etc)
Choose The "Selection" Option then.
Click on GridView Design View And you will find "Events" on "PageIndexChanging"
write this Code :
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; // GridView1 depands on you may be you have the Different ID of GridView but default is GridView1 }Then. write the Code on "SelectedIndexChanging"
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { int newIndex = e.NewSelectedIndex; string idGridview = GridView1.Rows[newIndex].Cells[1].Text; // Cell[1] means if you have 3 Columns Like ID, Name, ModelofCars then Cell[0] = ID and Cell[1] = Name And Cell[2] = ModelOfCars Response.Redirect("CarsDetails.aspx?CarID=" + idGridview); }Now, you have the QueryString Passed Succesfully.
On CarsDetails.aspx Page Load you will have to write this Code:
string QueryCarID = Request.QueryString["CarID"]; YourLabel.Text = QueryCarID;( here you Done :-) ) and you are using QueryString Method the other method is "Session" but for your Scenario QueryString is Better
and there will not be Error if QueryString Value will not be passed. So, Enjoy
ramiramilu
All-Star
95403 Points
14096 Posts
Re: Gridview selection to Label
Apr 17, 2012 08:16 AM|LINK
DataTransfer between different pages - http://www.intstrings.com/ramivemula/articles/data-transfer-between-two-asp-net-pages/
please refer that article and find out different options like QueryStrings, Session, Cookies etc to share information...opt for the one which better suits you.,..
thanks,
JumpStart
kirupa.v
Contributor
2070 Points
531 Posts
Re: Gridview selection to Label
Apr 17, 2012 09:22 AM|LINK
Hi,
U Can use Session..
In the grid view row data bound ..
protected void grdinfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtBox1 = e.Row.FindControl("txtBox1") as TextBox;
Session ["TextValue"]=txtBox1.Text;
}
}
Go to the next page
lbl1.text=Session ["TextValue"].ToString();
kirupa.v
Contributor
2070 Points
531 Posts
Re: Gridview selection to Label
Apr 17, 2012 09:22 AM|LINK
Hi,
U Can use Session..
In the grid view row data bound ..
protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtBox1 = e.Row.FindControl("txtBox1") as TextBox;
Session ["TextValue"]=txtBox1.Text;
}
}
Go to the next page
lbl1.text=Session ["TextValue"].ToString();
uzzy
0 Points
116 Posts
Re: Gridview selection to Label
Apr 17, 2012 06:24 PM|LINK
I tried your method Mahad but it does not work. I still get errors :(
I have a page with prestige cars on and this is the code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:CommandField ShowSelectButton="True" /> <asp:BoundField DataField="CarMake" HeaderText="CarMake" SortExpression="CarMake" /> <asp:BoundField DataField="Colour" HeaderText="Colour" SortExpression="Colour" /> <asp:BoundField DataField="EngineSize" HeaderText="EngineSize" SortExpression="EngineSize" /> <asp:BoundField DataField="CarDetails" HeaderText="CarDetails" SortExpression="CarDetails" /> <asp:BoundField DataField="DailyHireRate" HeaderText="DailyHireRate" SortExpression="DailyHireRate" /> <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestString %>" SelectCommand="SELECT DISTINCT [CarMake], [Colour], [EngineSize], [CarDetails], [DailyHireRate], [Category] FROM [Cars] WHERE ([Category] = @Category)"> <SelectParameters> <asp:QueryStringParameter DefaultValue="Prestige" Name="Category" QueryStringField="Prestige" Type="String" /> </SelectParameters> </asp:SqlDataSource>Code behind is:
Protected Sub GridView1_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; } End Sub Protected Sub GridView1_SelectedIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { int newIndex = e.NewSelectedIndex; string idGridview = GridView1.Rows[newIndex].Cells[0].Text; Response.Redirect("Reservations.aspx?CarID=" + idGridview); } End SubThen the reservations page code is:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) string QueryCarID = Request.QueryString["CarID"]; YourLabel.Text = QueryCarID; End SubPlease can you help as I have no idea for this.
Thanks a lot
MahadPK
Participant
778 Points
225 Posts
Re: Gridview selection to Label
Apr 17, 2012 10:03 PM|LINK
Try This and brother. on GridView Events
http://foto.pk/index1.php?v=gridviewse.jpg ( check this Image i made for u :-) )
GridView1_PageIndexChanging Event :- GridView1.PageIndex = e.NewPageIndex; // write this Code in GridView1 Page Index changing. GridView1 Selected Index Changing Event :- int newIndex = e.NewSelectedIndex; string idGridview = GridView1.Rows[newIndex].Cells[0].Text; Response.Redirect("Reservations.aspx?CarID=" + idGridview);