What's the best way to post page read values ​​from another page?

What is the best way to read page values ​​sent from another page? I have something that works, but it looks pretty messy.

I am working on a multi-page form with the first page using PostBackUrl to navigate to the second.

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

I am posting the first page to the second using PostBackUrl.

The second page file code then reads the value and puts 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 am getting the information using the final field name "ctl00 $ CPH1 $ First_Name" instead of "First_Name".

Is there a better way to do this?

Edit below:

Thanks everyone for the help. I still didn't understand it, but came 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.

I have not figured out how to get the TextBox value as a string. It seems that everything I am trying to get results in an error.

+3


source to share


3 answers


Please move on to the next article for all the information 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;

      

0


source


There are several options you have, depending on how you post messages to another page.

You have the option to get controls Previous Page

using this syntax:

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

      



You can also open public properties

from your original page and access it on the landing page.

Label1.Text = PreviousPage.First_Name;

      

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

0


source


How about you Request.Redirect, to navigate to another page, you can pass parameters in one request and then access them using Request.QueryString ["NameoftheParameter"].

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

Accessing variable field 1 on page 2 as follows: String field1 = Request.QueryString ["field1"];

Hope this helps!

0


source







All Articles