VBを使用してASP.NETのあるページから別のページに値を取得する方法
-
25-10-2019 - |
質問
1つのページに2つのテキストフィールドがあるとします。1つは名前用、もう1つは年齢に1つあります。
送信]ボタンをクリックすると、これらの値は別のページに表示されます。誰かがその例を挙げることができますか..私は完全に混乱しています。
ありがとうございます
解決
MSDNにはこれに関するページがあります、 方法:ASP.NET Webページ間で値を渡します:
ソースページがターゲットページから別のASP.NET Webアプリケーションにある場合、またはソースページがASP.NET Webページではない場合でも、次のオプションが利用できます。
- クエリ文字列を使用します。
- ソースページからHTTPの投稿情報を取得します。
次のオプションは、ソースページとターゲットページが同じASP.NET Webアプリケーションにある場合にのみ使用できます。
- セッション状態を使用します。
- ソースページにパブリックプロパティを作成し、ターゲットページのプロパティ値にアクセスします。
- ソースページのコントロールからターゲットページの制御情報を取得します。
シナリオでは、最初のページにテキストボックスがあるため、投稿を使用する方法であるように聞こえます。例:
先頭ページ:
<%@ 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>
に注意してください action="WebForm2.aspx"
これは、投稿を2番目のページに指示します。コードビハインドはありません。
2ページ(受信ページ):
<%@ 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>
に注意してください EnableViewStateMac="false"
ページ要素の属性。それは重要です。
Code-Behindは、単純なものを使用して値をつかみます 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
それはうまくいくはずです... :)
他のヒント
このコードを送信ボタンイベントハンドラーに入れてください。
private void btnsubmit_click(object sender、system.eventargs e){response.redirect( "anotherpage.aspx?name =" + this.txtname.text + "&age =" + this.txtage.text); }
このコードを2ページに配置しますpage_load、
private void page_load(object sender、system.eventargs e){this.txtbox1.text = request.querystring ["name"]; this.txtbox2.text = request.querystring ["age"]; }
いくつかのオプションがあります。 **
1.クエリ文字列を使用します。
(Cons)
- Text might be too lengthy
- You might have to encrypt/decrypt query string based on your requirement
(Pros)
- Easy to manage
2.セッションを使用します
(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