passing user input from one asp page to another

Rate It (2)

Last post 04-20-2008 7:12 AM by devbrat_ghosh. 2 replies.

Sort Posts:

  • passing user input from one asp page to another

    11-23-2007, 10:51 PM
    • Loading...
    • Dante
    • Joined on 06-24-2006, 9:56 PM
    • Posts 42

    There are four methods or maybe more, but here you are going to lern four.

    You need three pages (default.aspx,default2.aspx,default3.aspx)

    First prepare default.aspx as fallow:

    <%@ Page Language="VB" %>

    <a href="Default.aspx">Default.aspx</a>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    Protected Sub op_color_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Select Case op_color.SelectedIndex

    Case 0

    Button1.PostBackUrl = "default2.aspx"

    HyperLink1.NavigateUrl = "default2.aspx?name=" & tx_name.Text & "&color=" & op_color.SelectedValue

    LinkButton1.PostBackUrl = "default2.aspx?name=" & tx_name.Text & "&color=" & op_color.SelectedValue

    Case 1

    Button1.PostBackUrl = "default3.aspx"

    HyperLink1.NavigateUrl = "default3.aspx?name=" & tx_name.Text & "&color=" & op_color.SelectedValue

    LinkButton1.PostBackUrl = "default3.aspx?name=" & tx_name.Text & "&color=" & op_color.SelectedValue

    End Select

     

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Response.Redirect("default2.aspx?name=" & tx_name.Text & "&color=" & op_color.SelectedValue)

    End Sub

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Página Inicial</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    Name

    <asp:TextBox ID="tx_name" runat="server" Width="74px"></asp:TextBox>&nbsp;<br />

    Color<asp:RadioButtonList ID="op_color" runat="server" AutoPostBack="True" OnSelectedIndexChanged="op_color_SelectedIndexChanged" RepeatDirection="Horizontal">

    <asp:ListItem>Black</asp:ListItem>

    <asp:ListItem>White</asp:ListItem>

    </asp:RadioButtonList><br />

    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Redirect" /><br />

    <asp:HyperLink ID="HyperLink1" runat="server"

    Width="98px">HyperLink</asp:HyperLink><br />

    <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton><br />

    <asp:Button ID="Button1" runat="server" Text="Cross Page Posting" Width="131px"/></div>

    </form>

    </body>

    </html>

    Second prepare default2.aspx as fallow

    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim n, c As String

    Response.Write("This page is default2<br>")

    If Not Page.PreviousPage Is Nothing Then

    Dim txname As TextBoxDim opcolor As RadioButtonList

     

    Response.Write("Previous Page was fired<br>")

    txname = CType(PreviousPage.FindControl("tx_name"), TextBox)

    opcolor = CType(PreviousPage.FindControl("op_color"), RadioButtonList)

    If Not txname Is Nothing Then

    Label1.Text = txname.Text

    Label2.Text = opcolor.SelectedValue

    End If

    Else

    Response.Write("Previous Page was not fired<br>")

    End If

     

    n = Request.QueryString(
    "name")

    c = Request.QueryString("color")

    If Not c Is Nothing Then

    Response.Write("Values passed by querystring: ")

    Response.Write("Name is " & n & " and color is " & c)

    End If

    End Sub

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:Label ID="Label1" runat="server" Text="Label" Width="121px"></asp:Label>

    <br />

    <asp:Label ID="Label2" runat="server" Text="Label" Width="44px"></asp:Label></div>

    </form>

    </body>

    </html>

    Finally prepare copy default2 code in a new page, change the instrucction

    Response.Write("This page is default2<br>") by Response.Write("This page is default3<br>") and save it as default3.aspx

    Default.aspx has a textbox named tx_name and a radiobuttonlist named op_color.  The objetive is to send the name filled in the textbox and the selected color to page default2.aspx and to default3.aspx.  To do that, default.aspx has a button to call Redirect Method, a hyperlink, a linkbutton and another button to use cross page posting method.

    Method 1. The easier way to call a page and send values to it is Redirect.  The code behind button2, used to call Redirect is

    Response.Redirect("default2.aspx?name=" & tx_name.Text & "&color=" & op_color.SelectedValue).  When you click this button it calls default2.aspx and sends tx_name and op_color values to it, asigned to parameters name and color.  Inside default2.aspx you need to write special code to retrieve the values of parameters name and color.  That code is

    n = Request.QueryString("name")

    c = Request.QueryString("color")

    Once you click the button you will receive

    This page is default2
    Previous Page was not fired
    Values passed by querystring: Name is Dante and color is White 

    Method 2.  Asign a page and a secuence of parameters and values to the NavigateUrl property of a HyperLink control, like

    HyperLink1.NavigateUrl = "default2.aspx?name=" & tx_name.Text & "&color=" & op_color.SelectedValue

    In the called page, the Request.QueryString instructions are used to read the values passed.  If you selected White you will recive

    This page is default3
    Previous Page was not fired
    Values passed by querystring: Name is Dante and color is White

    Method 3.  Asign a page and a secuence of parameters and values to the PostBackUrl property of a LinkButton control, like

    LinkButton1.PostBackUrl = "default2.aspx?name=" & tx_name.Text & "&color=" & op_color.SelectedValue

    If you click the linkbutton you receive

    This page is default3
    Previous Page was fired
    Values passed by querystring: Name is Dante and color is White
    Dante
    White

    It indicates that default3.aspx received the values Dante and White two times, one by the querystring method and another by the Previous Page method.

    Method 4 Asign a page and a secuence of parameters and values to the PostBackUrl property of  a button, likeButton1.PostBackUrl = "default2.aspx?name=" & tx_name.Text & "&color=" & op_color.SelectedValue

    You will receive

    This page is default3
    Previous Page was fired
    Dante
    White

     

     

  • Re: passing user input from one asp page to another

    04-20-2008, 5:54 AM
    • Loading...
    • spavkov
    • Joined on 11-25-2007, 10:23 PM
    • Serbia, Kula
    • Posts 26

    Nice post.

    Here is a detailed information on Cross Page Postbacks in ASP.NET for all the beginner developers struggling with this. 

    ASP.NET FAQs


    Click "Mark as Answer" on the post that helped you to help future readers.
  • Re: passing user input from one asp page to another

    04-20-2008, 7:12 AM

    Really a good example.

    Regards

    Dev

Page 1 of 1 (3 items)