How can I get the value of TextBox1, which is in WebForm1 in WebForm2?
This depends on the case. If what you're doing is posting WebForm1 to WebForm2, you will get it from Request.Form. The more typical case, though, is to transfer processing on the server from one form to the other. Some people tend to think Response.Redirect is the better way to do this, but it's not. HTTP Redirection is a client-side mechanism, that's why you have it in Response in the first place! It causes the page to return to the client, where the browser performs the new request to the "redirected" new address. You lose all data from the previous form in this way.
The better way to pass data around forms (and to transfer processing from one page to another) is to use Server.Transfer and the HttpContext.Items property. Server.Transfer allows the current HttpContext (you can get it from Page.Context for example) to ?survive? the transfer. The HttpContext.Items property is an IDictionary (i.e. a Hashtable) where you can store arbitrary data to pass from one form to the next, prior to the transfer.
On the next form, you can simply get that data out of the context and use it:
string value = System.Web.HttpContext.Current.Items["incoming"] as string;
Dig Deeper on Win Development Resources
Related Q&A from Daniel Cazzulino
Here Daniel Cazzulino explains how to load a DSL (domain specific language) domain model instance file programmatically. This requires the .NET type ... Continue Reading
Here we offer a glimpse at 12 of .NET development expert Danny Cazzulino's top ASP.NET questions and answers. Continue Reading
C# developers should NOT be modifying InitializeComponent method in the code-behind (or any of the variable definitions) by hand. Continue Reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.