Powered by Blogger.

Monday, July 19, 2010

How to pass values from one page to another page without state management techniques?


Introduction


To pass values from one page to another page using Cross page Posting and Action property in Form Tag.



Action Property in Form Tag


In Dot Net provides Request.Form Collection class to  retrieves the values of form elements which posted from the HTTP request body in a form using the post method.

Example : Request.Form("TextBox1")
TextBox1 - name of the form element is to retrieve values

Action Property in Form Tag
The action property sets or returns the value of the action attribute in a form.The action attribute specifies where to send the form-data when a form is submitted.
Example:
<form id="frm1" action="second.aspx" "method=post">

Examples


FirstPage.aspx (Html Coding)

<form id="form1" runat="server" action="SecondPage.aspx" method="post">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 100;
            left: 148px; position: absolute; top: 101px" Text="Calling Second Page Using POST Method" TabIndex="2" />

        <asp:TextBox ID="TextBox2" runat="server" Style="z-index: 101; left: 146px; position: absolute;
            top: 62px" TabIndex="1"></asp:TextBox>
        <asp:Label ID="lbl" runat="server" Style="z-index: 102; left: 52px; position: absolute;
            top: 65px" Text="Last Name"></asp:Label>
        <asp:Label ID="Label2" runat="server" Style="z-index: 103; left: 53px; position: absolute;
            top: 26px" Text="First Name"></asp:Label>
        <asp:TextBox ID="txtFirstName" runat="server" Style="z-index: 104; left: 147px; position: absolute;
            top: 24px"></asp:TextBox>
   
    </div>
    </form>
SecondPage.aspx (Code behind )


String str = Request.Form["txtFirstName"] + Request.Form["TextBox2"];
Response.Write("Welcome : " + str);

Crosspage Posting

If you configure a page for cross-page posting to get information from the source page. In Dot Net provides page.prevoiuspage property, you can search for controls on the source page and extract their value.

Example: 

PreviousPage.FindControl("TextBox1")
From VS 2005 and later versions, Button have a property of PostBackURL to set the destination page for retrieving values.

Examples

CrossPagePosting1.aspx (design )
 <form id="form1" runat="server">
    <div>
        &nbsp; &nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 100;
            left: 148px; position: absolute; top: 101px" Text="Calling Second Page using Crosspage" TabIndex="2" PostBackUrl="~/CrossPagePosting2.aspx" />
        <asp:TextBox ID="TextBox2" runat="server" Style="z-index: 101; left: 146px; position: absolute;
            top: 62px" TabIndex="1"></asp:TextBox>
        <asp:Label ID="lbl" runat="server" Style="z-index: 102; left: 52px; position: absolute;
            top: 65px" Text="Last Name"></asp:Label>
        <asp:Label ID="Label2" runat="server" Style="z-index: 103; left: 53px; position: absolute;
            top: 26px" Text="First Name"></asp:Label>
        <asp:TextBox ID="txtFirstName" runat="server" Style="z-index: 104; left: 147px; position: absolute;
            top: 24px"></asp:TextBox>
  
    </div>
    </form>
CrossPagePosting2.aspx (Code Behind )


TextBox SourceTextBox =
        (TextBox)Page.PreviousPage.FindControl("txtFirstName");
        TextBox SourceTextBox1 =
        (TextBox)Page.PreviousPage.FindControl("TextBox2");

        Response.Write("Welcome " + SourceTextBox.Text + SourceTextBox1.Text);
here enclosed the demo project for crosspage posting and action property(Note should be in C:\Projects\PostingData)


Download


 Download source code for How to pass values from one page to another page without state management techniques?


  
by Muhilan




 

No comments:

Post a Comment

  ©Template by Dicas Blogger.

TOPO