Hi all
I'm trying to do the following and hope you can help
1 User selects a value in a dropDownList(Scheme) on Page1 and then clicks Next button which has a PostBackUrl of Page2
2 on Page2 collect the value that was submitted from Page1 and use this value to query a database. e.g. select * from databasetable where SchemeID = (the value of Scheme Passed from Page1)
Anyone know how I can achieve this?
regards
Chubster
it wasn't me, a big boy did it and ran away
![]() |
0 |
![]() |
Hi Chubster,
Using cross page postback, access the PreviousPage property of Page2 to retrieve values from Page1.
More info here and sample here: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/tipstricks/default.aspx
Jason
MCP
ASPNETWorld.com
![]() |
0 |
![]() |
Hi,
if you use a PostBackUrl property, ASP.NET loads both old and new pages on the next postback. The old page (from which you went to new page) is accessible via PreviousPage property from the new page.
You should get values from controls on the old page and initialize your data source. Note, that if you will need this value later on postbacks, you need to remeber values retrived from the PreviousPage.
-yuriy
![]() |
0 |
![]() |
You can call server.transfer("Page2.aspx?WhatEverYouWant=" + dropDown.SelectedValue).
In Page2.aspx, you can add a QueryStringParameter to to the datasource's SelectParameters with the QueryStringField="WhatEverYouWant"
![]() |
0 |
![]() |
OK, thanks all
I'm trying the example from Asp.net tutorials as suggested, just to see that I can get the relevant values onto the page
In CodeBehind of Page2 at PageLoad I've added
In Page2.aspx I've added
_______
Dim SearchTerm As TextBox
SearchTerm = Page.PreviousPage.FindControl("Scheme")
LabelScheme.Text = SearchTerm.Text
_______
________
<asp:Label ID="LabelScheme" runat="server" Visible="False"></asp:Label>
________However this generates an error as follows.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 14: Dim SearchTerm As TextBox Line 15: SearchTerm = Page.PreviousPage.FindControl("Scheme") Line 16: LabelScheme.Text = SearchTerm.TextAny ideas? Could this be because the value passed from Page1 belongs to a DropDownList?
regards
Chubster
it wasn't me, a big boy did it and ran away
![]() |
0 |
![]() |
Hi Chubster,
two things to your solution:
chubster:
_______
Dim SearchTerm As TextBox
SearchTerm = Page.PreviousPage.FindControl("Scheme")
LabelScheme.Text = SearchTerm.Text
_______At first, check allways if the Control you assigned with FindControl() is NOT null, or nothing in VB.
If you wont the user can see a controll, use the css-Style attribute instead visible false. When you're using visible false, asp.net wont generate a html output and the control is not present at the client side. Better use Style.Visible=none.
chubster:
In Page2.aspx I've added
________
<asp:Label ID="LabelScheme" runat="server" Visible="False"></asp:Label>
________
Servus,
Klaus
I haven't the faintest idea, but great many therefrom.
klaus_b@.NET
![]() |
0 |
![]() |
thanks
Ive changed the label to visible for the time being as suggested and I've added the following code to the code behind page on Page2.
"Scheme")SearchTerm = Page.PreviousPage.FindControl(
If Not SearchTerm Is Nothing Then
LabelScheme.Text = SearchTerm.Text
Else
LabelScheme.Text = "NothingPassedToPage"
End IfThe result is that the label(LabelScheme) on Page2 produces the text "NothingPassedtoPage" rather than the select value (a number) in the DropDownList("Scheme") on page 1.
Any views?
regards
Chubster
it wasn't me, a big boy did it and ran away
![]() |
0 |
![]() |
chubster:
SearchTerm = Page.PreviousPage.FindControl("Scheme")If Not Page.PreviousPage Is Nothing Then ... SearchTerm = CType(Page.PreviousPage.FindControl("Scheme"), TextBox) ... End IfSee if the above modification works...
Jason
MCP
ASPNETWorld.com
![]() |
0 |
![]() |
Hi again,
chubster:
The result is that the label(LabelScheme) on Page2 produces the text "NothingPassedtoPage" rather than the select value (a number) in the DropDownList("Scheme") on page 1.
Simply question: you've added the <PreviuosPage="yourPreviousPage.aspx"> attribute to youre aspx Site shurly?
Servus,
Klaus
I haven't the faintest idea, but great many therefrom.
klaus_b@.NET
![]() |
0 |
![]() |
thanks
I've added
Dim
SearchTerm As TextBoxIf Not Page.PreviousPage Is Nothing Then
SearchTerm = CType(Page.PreviousPage.FindControl("Scheme"), TextBox)
' LabelScheme.Text = SearchTerm.Text
LabelScheme.Text = "SomethingWasPassedToPage"Else
LabelScheme.Text = "NothingPassedToPage"
End IfSo, Something is being passed as Page.PreviousPage as the label produces the "SomethingWasPassedtoPage" text.
However if I use LabelScheme.Text = SearchTerm.Text I get the error as described previously
"Object reference not set to an instance of an object.
Source Error:
Line 16: If Not Page.PreviousPage Is Nothing Then Line 17: SearchTerm = CType(Page.PreviousPage.FindControl("Scheme"), TextBox) Line 18: LabelScheme.Text = SearchTerm.Text Line 19: Else Line 20: LabelScheme.Text = "NothingPassedToPage"
Source File: C:\Inetpub\wwwroot\GPPortal\Portal\NewSubmission_SchemeArea.aspx.vb Line: 18regards
chubster
it wasn't me, a big boy did it and ran away
![]() |
0 |
![]() |
To your Previous.Page check additonally every control your accessing to check for nothing.
At next check in a debug session if the control is found in the privous page. Maybe is a typing error everywhere on the page.
Servus,
Klaus
I haven't the faintest idea, but great many therefrom.
klaus_b@.NET
![]() |
0 |
![]() |
klaus_b:
Simply question: you've added the <PreviuosPage="yourPreviousPage.aspx"> attribute to youre aspx Site shurly?
Erm
and where do I add this?
regards
chubster
it wasn't me, a big boy did it and ran away
![]() |
0 |
![]() |
chubster:
Erm
and where do I add this?
in the Top Section of your aspx site eg
<.... inherits ="_Default" codebehind="Default.aspx.vb" PreviousPage="PostingSite.aspx">
instead of "PostingSite.aspx" the site where your post from.
Wrote this from my mind, because no VS in front of me. But i'm shure you now where i mean
Servus,
Klaus
I haven't the faintest idea, but great many therefrom.
klaus_b@.NET
![]() |
0 |
![]() |
Hi
Cast the object to a dropdownlist and get the selecteditems text property.
![]() |
0 |
![]() |
Hi
thanks
Ok, I've started again with this. The original Page1 and Page2 belong to a MasterPage. Does this effect anything?
I've created a couple of new pages that don't belong to a master just to see if it works without a master and in this case it does.
e.g. I have a text box on Page3 and I click a button and pass the values from that text box to a label on Page4 using something like this in the CodeBehind of Page4
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SearchTerm As TextBox
If Not Page.PreviousPage Is Nothing Then
If Not Page.PreviousPage.FindControl("Scheme") Is Nothing Then
SearchTerm = CType(Page.PreviousPage.FindControl("Scheme"), TextBox)
LabelScheme.Text = SearchTerm.Text
Else
LabelScheme.Text = "PagePassedButNoData"
End If
Else
LabelScheme.Text = "NothingPassed"
End If
End SubSo the question is can you postback to a different page where the pages belong to a master page?
regards
chubster
it wasn't me, a big boy did it and ran away
![]() |
0 |
![]() |
chubster:
Ok, I've started again with this. The original Page1 and Page2 belong to a MasterPage. Does this effect anything?
Only when the controls a reside on the MasterPage and NOT on the contentpage.
Servus,
Klaus
I haven't the faintest idea, but great many therefrom.
klaus_b@.NET
![]() |
0 |
![]() |
chubster:
So the question is can you postback to a different page where the pages belong to a master page?
Shure, you can do this.
Here is a working sample with two sites, both are contenpages of a masterpage. I post it in C# because VB is not mine.
the SendingPage MarkUp:
1 <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" 2 CodeFile="SendingPage.aspx.cs" Inherits="SendingPage" Title="Untitled Page" %> 3 4 <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 5 <asp:TextBox ID="Scheme" runat="server"> 6 </asp:TextBox> 7 <%--Define the site you're posting to as PostbackUrl--%> 8 <asp:Button runat="server" ID="SenderButton" Text="Go" PostBackUrl="GettingPage.aspx" 9 OnClick="SenderButton_Click"></asp:Button> 10 </asp:Content>the SendingPage codebehind
1 public partial class SendingPage : System.Web.UI.Page 2 { 3 // public string for direct access via PriviousPage 4 public string sendingString; 5 6 protected void Page_Load(object sender, EventArgs e) 7 { 8 9 } 10 11 protected void SenderButton_Click(object sender, EventArgs e) 12 { 13 // pass the value from the TextBox to the public string 14 sendingString = Scheme.Text; 15 } 16 }__________________________________________________________________________________________
the GettingPage MarkUp
1 <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" 2 CodeFile="GettingPage.aspx.cs" Inherits="GettingPage" Title="Untitled Page" %> 3 4 <%--Define the Previous Page where is postet from--%> 5 <%@ PreviousPageType VirtualPath="~/SendingPage.aspx" %> 6 <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 7 <asp:Label ID="LabelScheme" runat="server" Text=""></asp:Label> 8 </asp:Content>the GettingPage codebehind
1 public partial class GettingPage : System.Web.UI.Page 2 { 3 protected void Page_Load(object sender, EventArgs e) 4 { 5 // check if PreviousPage NOT is null 6 if (PreviousPage != null) 7 { 8 // access direct the string from the PriviousPage 9 // without any casting 10 LabelScheme.Text = PreviousPage.sendingString; 11 } 12 } 13 } 14More easy it could'nt be
Servus,
Klaus
I haven't the faintest idea, but great many therefrom.
klaus_b@.NET
![]() |
0 |
![]() |
Hi chubster,
Based on my understanding, the problem is caused by the master page. When a master page is applied, you should find the control's parent container first, and then find the control. In your case, you should find the ContentPlaceHolder where the TextBox is nested in, and then find the TextBox in the ContentPlaceHolder.
Please try the following code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SearchTerm As TextBox
If Not Page.PreviousPage Is Nothing Then
Dim cp As ContentPlaceHolder = CType(Page.PreviousPage.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)If Not cp.FindControl("Scheme") Is Nothing Then
SearchTerm = CType(cp.FindControl("Scheme"), TextBox)
LabelScheme.Text = SearchTerm.Text
Else
LabelScheme.Text = "PagePassedButNoData"
End If
Else
LabelScheme.Text = "NothingPassed"
End If
End SubHope it helps,
Jessica
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
![]() |
0 |
![]() |
Hi
thanks all, I've tried the code supplied by Jissica, which gets me a bit further along the line and seems to reference the dropDownList(scheme). However I now receive a typecast error as follows.
___________
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.DropDownList' to type 'System.Web.UI.WebControls.TextBox'.
Source Error:
Line 17:
Line 18: If Not cp.FindControl("Scheme") Is Nothing Then
Line 19: SearchTerm = CType(cp.FindControl("Scheme"), TextBox) 'the error is here
Line 20: LabelScheme.Text = SearchTerm.Text
Line 21: Else
Source File: C:\Inetpub\wwwroot\GPPortal\Portal\NewSubmission_SchemeArea.aspx.vb Line: 19
__________So, any ideas as to what might be wrong here? The DropDownList (Scheme) should pass a number to Page2 that can then be used in a database query.
In the mean time I' might have a go at creating this functionality using the code supplied by Klaus to see whether I can get some values passed with that.
regards
Chubster
it wasn't me, a big boy did it and ran away
![]() |
0 |
![]() |
Hi Chubster,
Since the Scheme is a DropDownList control, it should be cast to a DropDownList instead of the TextBox, change the code like this
SearchTerm = CType(cp.FindControl("Scheme"), DropDownList)
Hope it helps,
Jessica
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
![]() |
0 |
![]() |
thanks
I think I'm there now, at least with regards to getting the value onto Page2. . Your were right, DropDownList can't be cast as a textbox so on line 1 I changed it to Dim SearchTerm As DropDownList on line 1 and then again change SearchTerm = CType(cp.FindControl("Scheme"), TextBox) to SearchTerm = CType(cp.FindControl("Scheme"), DropDownList)
Here is the code for anyone other noobs that might have similar problems.
1 Dim SearchTerm As DropDownList 2 If Not Page.PreviousPage Is Nothing Then 3 Dim cp As ContentPlaceHolder = CType(Page.PreviousPage.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder) 4 5 If Not cp.FindControl("Scheme") Is Nothing Then 6 SearchTerm = CType(cp.FindControl("Scheme"), DropDownList)
8 LabelScheme.Text = SearchTerm.Text 9 Else 10 LabelScheme.Text = "PagePassedButNoData" 11 End If 12 Else 13 LabelScheme.Text = "NothingPassed" 14 End Ifbest regards
Chubster
it wasn't me, a big boy did it and ran away
![]() |
0 |
![]() |
Thank you so much!!!!!
I have been trying to figure this out for hours! You do need the to include the .Master before using FindControl.
Thank you again!
Bart
![]() |
0 |
![]() |