3

What is the best way to have a page read values posted from another page? I have something that works, but it seems rather sloppy.

I'm working on a multi-page form with the first page using PostBackUrl to go to the second.

Within page 1 is content placeholder named "CPH1". Inside that is a field named "First_Name".

I submit the first page to the second using PostBackUrl.

The code behind file of the second page then reads the value and places it into a variable using this code:

protected void Page_Load(object sender, EventArgs e)
   {
       NameValueCollection nvc = Request.Form;
       string First_Name_Here = nvc["ctl00$CPH1$First_Name"] ;
   }

As you can see, I'm getting the info by using the final name of the field, "ctl00$CPH1$First_Name", instead of "First_Name".

Is there a better way of doing this?

Edit below:

Thanks to everyone for their help. Still haven't figured this out, but have gotten closer.

Using this code: ContentPlaceHolder CPH1 = Page.Master.FindControl("CPH1") as ContentPlaceHolder; Response.Write(CPH1.FindControl("First_Name"));

Gives me this result: System.Web.UI.WebControls.TextBox.

What I haven't figured out is how to get the value of the TextBox as a string. It seems like anything I try results in an error.

someguy
  • 41
  • 3
  • Since you are posting across pages, then likely the server control isnt available to you in your Page Load method, so I think relying on the predictability of the ASP.NET control naming (like you are doing already) -- is your best option. It's not sloppy, but the control name IS sorta ugly. – Glenn Ferrie Jul 01 '15 at 14:57

3 Answers3

0

Please go through the below article which contains all the information which you need.

http://www.asp.net/web-forms/overview/older-versions-getting-started/master-pages/control-id-naming-in-content-pages-cs

 ContentPlaceHolder MainContent = Page.Master.FindControl("MainContent") as ContentPlaceHolder;

 Label ResultsLabel = MainContent.FindControl("First_Name") as Label;
Varun
  • 597
  • 7
  • 26
0

There are a couple options you have depending on how you are posting to the other page.

You have an option to get the Previous Page controls using this syntax:

TextBox tbControl = (TextBox)PreviousPage.FindControl("First_Name");

You can also expose public properties from your Source page and access it on the target page.

Label1.Text = PreviousPage.First_Name;

MSDN article here about cross page posting: https://msdn.microsoft.com/en-us/library/ms178139.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

Rick S
  • 6,476
  • 5
  • 29
  • 43
0

How about you Request.Redirect to go the the other page, you can pass the parameters in the same request and then access them using Request.QueryString["NameoftheParameter"].

For example:- Calling page 2 with parameter field1:- Response.Redirect("~/Page2.aspx?field1="xyz.Text);

Access the variable field 1 on page 2 like this:- String field1 = Request.QueryString["field1"];

Hope this helps!

tjshah050291
  • 71
  • 1
  • 2
  • 9