how to take values from one page to another page in asp.net using vb

Mihir picture Mihir · Jan 27, 2011 · Viewed 14.4k times · Source

Suppose i have two text fields in one page, one for name and another one for age.

When i click the submit button those values should appear in another page. Can any one give the example for that one.. i am totally confused.

please help me Thank you

Answer

Jakob Gade picture Jakob Gade · Jan 27, 2011

MSDN has a page on this, How to: Pass Values Between ASP.NET Web Pages:

The following options are available even if the source page is in a different ASP.NET Web application from the target page, or if the source page is not an ASP.NET Web page:

  • Use a query string.
  • Get HTTP POST information from the source page.

The following options are available only when the source and target pages are in the same ASP.NET Web application.

  • Use session state.
  • Create public properties in the source page and access the property values in the target page.
  • Get control information in the target page from controls in the source page.

For your scenario, it sounds like using POST is the way to go, since you have textboxes on the first page. Example:

First page:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Page 1</title>
</head>
<body>
  <form id="form1" runat="server" action="WebForm2.aspx">
  <div>
    Name: <asp:TextBox ID="tbName" runat="server"></asp:TextBox><br />
    Age: <asp:TextBox ID="tbAge" runat="server"></asp:TextBox><br />
    <asp:Button ID="submit" runat="server" Text="Go!" />
  </div>
  </form>
</body>
</html>

Notice the action="WebForm2.aspx" which directs the POST to the second page. There's no code-behind.

Page 2 (receiving page):

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb" Inherits="WebApplication2.WebForm2" EnableViewStateMac="false" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page 2</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Literal ID="litText" runat="server"></asp:Literal>
    </form>
</body>
</html>

Notice the EnableViewStateMac="false" attribute on the Page element. It's important.

The code-behind, grabbing the values using a simple Request.Form():

Public Class WebForm2
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    litText.Text = Request.Form("tbName") & ": " & Request.Form("tbAge")
  End Sub
End Class

That should work... :)