• [代码]如何在ASP.NET网页间传递数据


    重点总结
    目前为止在ASP.NET网页中传递数据的方式至少有5种:
    1、通过查询字符串传递数据。
    2、通过HTTP POST传递数据。
    3、通过会话状态传递数据。
    4、通过源页的公共属性传递数据。
    5、通过源页中的控件值传递数据。
    到底使用哪种方式来进行数据的传递,这可能受到两方面的影响:
    1、页面重定向的方式。
    2、源页和目标页是否位于相同的ASP.NET应用程序中。
    如果源页和目标页位于不同的ASP.NET应用程序中则只能通过查询字符串和HTTP POST传递数据。
    而如果源页和目标页位于相同的ASP.NET应用程序中,则可以使用五种方式中的任意一种。

    一、通过查询字符串传递数据
    下面的两个URL,第一个只传递了产品编号,第二个不仅传递了产品编号,同时也传递产品名称。
    http://localhost/Demo/DestinationPage.aspx?ProductID=777
    http://localhost/Demo/DestinationPage.aspx?ProductID=777&ProductName=Glass

    在目标页中则可以通过Page.Request.QueryString属性来获取查询字符串中传递的键值。比如下面的代码:

    this.Response.Write(this.Request.QueryString["ProductID"]);
    this.Response.Write("<br />");
    this.Response.Write(string.Format("ProductID={0} ProductName={1}",
        this.Request.QueryString["ProductID"],
        this.Request.QueryString["ProductName"]));
    

    二、通过HTTP POST传递数据
    此示例代码在源页中,为用户提供了输入用户名、生日和年龄的文本框,并且将Button控件的PostBackUrl属性设置为DestinationPage.aspx。也就是说当单击【提交到目标页】按钮后,源页窗体的数据会被传递到DestinationPage.aspx页面。
    在目标页中则通过Page.Request.Form属性来获取这些传递过来的数据。
    源页的页面源码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SourcePage.aspx.cs" Inherits="SourcePage" %>
    
    <!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>源页!</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            User Name :
            <asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox>
            <br />
            Birth Date : 
            <asp:TextBox ID="BirthDateTextBox" runat="server"></asp:TextBox>
            <br />
            Age : 
            <asp:TextBox ID="AgeTextBox" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="SubmitButton" runat="server" Text="提交到目标页"
                PostBackUrl="~/DestinationPage.aspx" />
        </div>
        </form>
    </body>
    </html>
    


    目标页中获取源页窗体数据的代码如下:

    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder SBuilder = new StringBuilder();
        NameValueCollection PostedValues =
            this.Request.Form;
        
        for (int Index = 0; Index < PostedValues.Count; Index++)
        {
            if (PostedValues.Keys[Index].Substring(0, 2) != "__")
            {
                SBuilder.Append(string.Format("{0} = {1}",
                    PostedValues.Keys[Index],
                    PostedValues[Index]));
                SBuilder.Append("<br />");
            }
        }
    
        this.Response.Write(SBuilder.ToString());
    }
    


    代码中的if语句主要是为了避免获取以两个下划线__开头的隐藏字段的数据,比如__VIEWSTATE、__EVENTTARGET、__EVENTARGUMENT。当然也可以去掉这个if语句,然后就能同时获取这些隐藏字段的数据了。

    三、通过会话状态传递数据
    使用会话状态的好处是可以在与源页处于相同ASP.NET应用程序的所有网页间共享数据。缺点是会增加服务器的内存开销。
    下面的代码中,用户在源页的【用户名】文本框中输入自己的姓名,然后单击【提交数据】按钮。服务器代码会通过Page.Session属性将用户名存储到会话状态中,然后使用Response.Redirect()方法将页面强制重定向到另外一个页面(DestinationPage.aspx)。在这个目标页面中再次通过Page.Session属性来获取用户在源页中保存的用户名。
    源页的源代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SourcePage.aspx.cs" Inherits="SourcePage" %>
    
    <!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>源页:通过会话状态传递数据!</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            用户名:
            <asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox>
            <asp:Button ID="SubmitButton" runat="server" Text="提交数据" 
                onclick="SubmitButton_Click" />
        </div>
        </form>
    </body>
    </html>
    


    源页中用来保存用户名到会话状态并重定向到目标页的代码如下:

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        this.Session["UserName"] = this.UserNameTextBox.Text;
        this.Response.Redirect("DestinationPage.aspx");
    }
    


    目标页中获取保存在会话状态中的用户名的代码如下:

    protected void Page_Load(object sender, EventArgs e)
    {
        string UserName = this.Session["UserName"].ToString();
        this.Response.Write(UserName);
    }
    



    四、通过源页的公共属性传递数据
    在示例代码中,我们在源页中提供了一个输入用户名的文本框,然后通过单击【提交到目标页】按钮将数据提交到目标页。
    源页的源代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SourcePage.aspx.cs" Inherits="SourcePage" %>
    
    <!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>源页:通过公共属性传递数据!</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            用户名:<asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="SubmitButton" runat="server" Text="提交到目标页"
                PostBackUrl="~/DestinationPage.aspx" />
        </div>
        </form>
    </body>
    </html>
    


    并且为源页定义了一个名为UserName的公共属性,此属性返回的是【用户名】文本框中输入的用户名。

    public string UserName
    {
        get { return this.UserNameTextBox.Text; }
    }
    


    这样我们就创建了一个包含公共属性的源页。接着创建目标页,并且为目标页添加@PreviousPageType指令,当然也可以使用@Reference指令。

    <%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>
    


    这样,ASP.NET就会自动将目标页的Page.PreviousPage属性转换为源页的类型,从而就可以直接访问UserName属性。注意观察下面的代码,我们并没有将Page.PreviousPage进行显示的类型转换。

    if (this.PreviousPage != null)
    {
        string UserName = this.PreviousPage.UserName;
        this.Response.Write(UserName);
    }
    


    需要特别强调的是不要忘记判断Page.PreviousPage属性是否为null,因为它确实有为null的可能性。如果在为null的情况下获取UserName这个源页的公共属性,那是会跳出异常的。



    五、通过源页中的控件值传递数据
    这最后一种传递数据的方式就是直接获取源页的控件对象了,然后通过控件的属性值来获取所需的数据。比如本示例代码中,我们就是通过获取源页的TextBox控件,然后通过访问TextBox.Text属性来获取用户在源页中输入的数据。
    下面的示例代码中,我们在源页放置了一个输入用户名的文本框,ID为UserNameTextBox。通过Page.PreviousPage.FindControl()方法就可以获取此控件的引用。
    源页的源代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SourcePage.aspx.cs" Inherits="SourcePage" %>
    
    <!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>源页:通过控件属性传递数据!</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            用户名:<asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="SubmitButton" runat="server" Text="提交到目标页"
                PostBackUrl="~/DestinationPage.aspx" />
        </div>
        </form>
    </body>
    </html>
    


    目标页中获取文本框控件,并获取其Text属性值的代码如下:

    if (this.PreviousPage != null)
    {
        TextBox UserNameTextBox =
            (TextBox)this.PreviousPage.FindControl("UserNameTextBox");
        if (UserNameTextBox != null)
        {
            this.Response.Write(UserNameTextBox.Text);
        }
    }
    


    如果所要获取的控件位于某个控件的内部,比如下面的代码,UserNameTextBox控件位于名为UserPanel的Panel控件内部。那么首先找出这个Panel控件,然后通过此控件的FindControl()方法找出内部的文本框控件。
    源页的源代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SourcePage.aspx.cs" Inherits="SourcePage" %>
    
    <!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>源页:通过控件属性传递数据!</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Panel ID="UserPanel" runat="server">
                用户名:<asp:TextBox ID="UserNameTextBoxInPanel" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="SubmitButtonInPanel" runat="server" Text="提交到目标页"
                    PostBackUrl="~/DestinationPage.aspx" />
            </asp:Panel>
        </div>
        </form>
    </body>
    </html>
    


    目标页中获取这个位于Panel控件内部的TextBox控件的代码如下:

    if (this.PreviousPage != null)
    {
        Panel UserPanel = (Panel)this.PreviousPage.FindControl("UserPanel");
        if (UserPanel != null)
        {
            TextBox UserNameTextBox =
                (TextBox)UserPanel.FindControl("UserNameTextBoxInPanel");
            if (UserNameTextBox != null)
            {
                this.Response.Write(UserNameTextBox.Text);
            }
        }
    }
    


    不管控件位于那个级别的命名容器控件内部,都是通过这种方式来获取的。
    一定不要忘记判断所获取的控件引用是否为null。


    到这里为止,5种在网页间传递数据的方式基本已经展示完毕!^_^


    光脚丫思考 2010-11-9

  • 相关阅读:
    deepsort+yolov3实现多类别多目标跟踪
    WAR2020暑期补题集
    【数据结构】浅谈主席树
    Github本地上传命令
    【蓝桥杯】2017年第八届蓝桥杯C/C++B组省赛——C题 承压计算
    【蓝桥杯】2017年第八届蓝桥杯C/C++B组省赛——B题 等差素数列
    【蓝桥杯】2019年第十届蓝桥杯C/C++ B组省赛——I题 后缀表达式
    防御Mimikatz-转载
    SQL注入之判断数据库
    XPATH注入
  • 原文地址:https://www.cnblogs.com/GJYSK/p/1873166.html
Copyright © 2020-2023  润新知