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

15.1k views Asked by At

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

3

There are 3 answers

0
Jakob Gade On BEST ANSWER

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... :)

3
Nanda On

Put this code to your submit button event handler,

private void btnSubmit_Click(object sender, System.EventArgs e) { Response.Redirect("AnotherPage.aspx?Name=" + this.txtName.Text + "&Age=" + this.txtAge.Text); }

Put this code to second page page_load,

private void Page_Load(object sender, System.EventArgs e) { this.txtBox1.Text = Request.QueryString["Name"]; this.txtBox2.Text = Request.QueryString["Age"]; }

0
Zo Has On

You have a couple of options. **

1. Use Query string.


(Cons)
  - Text might be too lengthy 
  - You might have to encrypt/decrypt query string based on your requirement 
(Pros)
  - Easy to manage

2. Use Session

(Cons)
  - May increase processing load on server
  - You have to check and clear the session if there are too many transactions
(Pros) 
  - Values from different pages, methods can be stored once in the session and retrieved from when needed