True!! I started to wonder about the difference in approach between a Web App and a Windows Application. In case of a windows application passing data between 2 forms is a piece of cake. But how do I pass values between web pages? I was sure that it wasn't something very complex, but I realized that it was something I did not know. So I decided to try that and did so in a matter of minutes. Thanks to the documentation and example at msdn! This is what I did.
1. I created a new website with 2 pages Default.aspx and Default2.aspx
2. In Default.aspx I added a Textbox and a button.
3. In Default2.aspx I added a Label. My Objective was to display text entered in the text box in the label on click of the button.
4. I set the PostbackUrl of button to Default2.aspx
ID="Button1" runat="server" Text="Button" PostBackUrl="~/Default2.aspx"
5. Next I write the following code in the page load of Default2.aspx
if (Page.PreviousPage != null)
{
TextBox SourceTextBox = (TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null)
{
Label1.Text = SourceTextBox.Text;
}
}
6. And that's it! The value of the text box is passed onto the Label on the second page.
This is one of the many ways of passing data between pages. I will explore and post about them in the coming weeks.
Till Then Happy Programming !!
No comments:
Post a Comment