Lecture - Passing Variables in Web Forms


Passing Variables in Web Forms

 

Summary

 

This video (9 minutes) demonstrates how to pass a variable from a web form to another web form. The three methods demonstrated are (1) using the Session state, (2) Using HTTP query string method, and (3) using HTTP POST method.

 

Video

 

http://online1.daytonastate.edu/player2.php?id=2f4fe03d77724a7217006e5d16728874 

 

 

Code

 

 

 

 

 

 

 

 

 

 

FormA.aspx

<body>
    <form id="form1" runat="server" method="post">
    <div style="font-weight: 700">
        Enter Value to Transfer<asp:TextBox ID="tbData" runat="server"></asp:TextBox><br /><br />
        <asp:Button ID="btnSession" runat="server" 
            Text="Transfer Using Session Variable" onclick="btnSession_Click" />
        <asp:Button ID="btnGet" runat="server" Text="HTTP Get Request" 
            onclick="btnGet_Click" />
        <asp:Button ID="btnPost" runat="server" Text="HTTP Post Request" 
             PostBackUrl="~/FormD.aspx" />
    </div>
    </form>
</body> 

 

 

 

 

 

 

 

 

FormA.cs

 

 

    public partial class FormA : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnSession_Click(object sender, EventArgs e)
        {
            string value = tbData.Text;
            Session.Add("val", value);
            Response.Redirect("FormB.aspx");
        }
        protected void btnGet_Click(object sender, EventArgs e)
        {
            Response.Redirect("FormC.aspx?val=" + tbData.Text);
        }      
    } 

 

 

 

 

 

FormB.aspx

<body>
    <form id="form1" runat="server">
    <div>
    You passed: 
        <asp:TextBox ID="tbValue" runat="server" ></asp:TextBox>
    </div>
    </form>
</body>
 

 

 

 

 

FormB.cs

 public partial class FormB : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            tbValue.Text=Session["val"].ToString();
        }       
    } 

 

 

 

 

FormC.aspx

 

<body>
    <form id="form1" runat="server">
    <div>
     You passed: 
        <asp:TextBox ID="tbValue" runat="server" ></asp:TextBox>
    </div>
    </form>
</body> 

 

 

 

 

FormC.cs

public partial class FormC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            tbValue.Text = Request["val"].ToString();
        }
    } 

 

 

 

 

FormD.aspx

 

  <body>
    <form id="form1" runat="server">
    <div>
        You passed: 
        <asp:TextBox ID="tbValue" runat="server" ></asp:TextBox>
    </div>
    </form>
</body>

 

 

 

 

 

FormD.cs

public partial class FormD : System.Web.UI.Page
     {
         protected void Page_Load(object sender, EventArgs e)
         {
             string[] s = Request.Form.GetValues("tbData");
             tbValue.Text = s[0];
         }       
     } 

 

 

Resources

 

How to Pass values Between ASP.Net Web Pages - http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx 

 

HttpResponse Class - http://msdn.microsoft.com/en-us/library/system.web.httpresponse.aspx 

 

HttpRequest Class - http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx 

 

Http Session - http://msdn.microsoft.com/en-us/library/ms178581.aspx