0

I have a page called webForm1, this page contains a textfield, when a user enters a value, I want the value to show up in a label on webForm2, when I do that, I am getting an error: Label1 is inaccessible due to its protection level

This is what I am doing in webForm1

webForm2 webform = new webForm2(); webform.Label = textBox1.Text; Response.redirect("~/webForm2.aspx"); 

but the above is not working, I am new to programming and not familiar with classes and complicated programming, what is the easiest way to get the value of the textbox in the label?

Thank you.

3
  • 1
    In asp.net sites, you don't manually instantiate page classes, the framework does that for you. There are several ways to make data available between pages.. which is best depends on the situation. I would suggest picking up a book on web development. Commented Aug 8, 2014 at 16:46
  • Could you give me a topic that I can follow about the question? Commented Aug 8, 2014 at 16:48
  • You could use the query string, you could post the data, you could use cookies or local storage, you could save the value in a database... Commented Aug 8, 2014 at 16:50

2 Answers 2

4

You can't instantiate the page class (webForm2) in your current page. You'll have to pass the value in another way to the second page and then bind the label. As Jason P says, the ASP.NET framework instantiates the webForm2 page for you, you can't do it yourself.

If the data is not sensitive, use the Query String:

Response.Redirect("~/webForm2.aspx?label=" + textBox1.Text); 

This will redirect the user to a page with the url of whatever.com/webForm2.aspx?label=whatevervalue. On the second page, you can pull the text from the query string and bind it to the label:

public void Page_Load(object sender, EventArgs e) { Label.Text = Request.QueryString["label"].ToString(); } 
Sign up to request clarification or add additional context in comments.

Comments

3

Unlike WinForms, you don't instantiate the next form like that. Essentially, your first two lines are incorrect for WebForms. The third line is where you want to focus your attention. You redirect the user to the second form, allowing the framework to take care of instantiating it.

This is because WebForms, despite being "forms", is still an HTTP web application and does everything through requests and responses. By issuing a redirect you are telling the client to abandon the current page and make a new request for the specified page.

There are a number of ways to send a value to this next page. You can store it in some persisted medium (such as a database), you can use session state, etc. Probably the simplest approach at the moment would be to include it on the query string:

Response.Redirect("~/webForm2.aspx?label=" + textBox1.Text); 

Then in the next page you'd get the string from:

Request.QueryString["label"] 

You may want to URL-encode the text value first, I don't know if Redirect() does that for you. Also keep in mind that this isn't a "secure" transfer of data from one page to the next, because the client has full access to modify values in the URL. So if this is in any way sensitive data then you'll want to look into other approaches. (Keep in mind that "sensitive" could be a relative term... The information itself might not be sensitive but you might be doing system-sensitive things with it on the next page, which we can't know from the code posted.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.