asp.net提交到其他页例程

本文介绍ASP.NET中如何实现从一个页面(PostAnotherPage.aspx)向另一个页面(PostAnotherPage2.aspx)传递数据,包括文本框输入及下拉列表选择的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    本人也是菜鸟一只,看了些提交到其他页的文章,觉得亲自试一下记忆比较深刻,于是乎行文于此。

源页       PostAnotherPage.aspx

目标页    PostAnotherPage2.aspx

 

PostAnotherPage.aspx 

<form id="form1" runat="server" target="_blank">
<%--    当提交到新页时,需要设置target,否则默认在本页打开提交 --%>
    
<div>
        
<asp:Label ID="lblName" runat="server" Text="姓名:"></asp:Label>
        
<asp:TextBox ID="tbName" runat="server"></asp:TextBox><br />
        
<asp:Label ID="lblEmail" runat="server" Text="邮箱:"></asp:Label>
        
<asp:TextBox ID="tbEmail" runat="server"></asp:TextBox><br />
        
<br />
<asp:DropDownList ID="ddlFruit" runat="server">
            
<asp:ListItem Value="1">香蕉</asp:ListItem>
            
<asp:ListItem Value="2">苹果</asp:ListItem>
            
<asp:ListItem Value="3">哈密瓜</asp:ListItem>
            
<asp:ListItem Value="4">香橙</asp:ListItem>
            
<asp:ListItem Value="5">西瓜</asp:ListItem>
        
</asp:DropDownList><br />
        
<asp:Button ID="btnSubmit" runat="server" Text="提交到PostAnotherPage2" PostBackUrl="~/PostAnotherPage2.aspx" /></div>
<%--    提交按钮的PostBackUrl属性为提交的目的页 --%>
</form>

PostAnotherPage.aspx.cs

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

    }

    
public string UserEmail
    
{
        
get
        
{
            
return this.tbEmail.Text;
        }

}

//把提交内容设置为公有属性(也可以通过其他方法获取源页属性,后面介绍)

PostAnotherPage2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PostAnotherPage2.aspx.cs" Inherits="PostAnotherPage2" %>
<%@ PreviousPageType VirtualPath="~/postAnotherPage.aspx" %>
<%--    接收页必须设置此项,并指明源页 --%>
<!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:Label ID="lblPreName" runat="server" Text="前页传递的name:"></asp:Label>
        
<asp:Label ID="Label1" runat="server" Text="Label" Width="429px"></asp:Label>
        
<br />
        
<asp:Label ID="lblPreEmail" runat="server" Text="前页传递的Email:"></asp:Label>
        
<asp:Label ID="Label3" runat="server" Text="Label" Width="426px"></asp:Label>
        
<br />
<asp:Label ID="lblFruit" runat="server" Text="所选水果:" Width="130px"></asp:Label>
        
<asp:Label ID="Label4" runat="server" Text="Label" Width="428px"></asp:Label></div>
</div>
    
</form>
</body>
</html>

PostAnotherPage2.aspx

if (PreviousPage != null)
//判断该页的源页属性是否为空
    {
            
if (!IsCrossPagePostBack)
          
//判断该页是否为其他页提交而来
            {
                
//访问前页的公有属性
                Label1.Text = PreviousPage.UserName;
                Label3.Text 
= PreviousPage.UserEmail;
                
//访问前页的非公有属性,通过FindControl的方法即可
                DropDownList fruit=(DropDownList)PreviousPage.FindControl("ddlFruit");
                
if (fruit != null)
                
{
                    Label4.Text 
= string.Format("value:{0}   text:{1}", fruit.SelectedValue, fruit.SelectedItem.Text);
                }

            }

     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值