在页面之间传递数据 [英] Passing data between pages

查看:76
本文介绍了在页面之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个webform1.aspx,其中包含100个文本框。我必须将这100个文本框值传递给webform2.aspx。



请帮助我,这是将值从一个页面传递到另一个页面的最佳方式。



除了Session是任何其他最好的技术..

解决方案

有很多方法可以做到但是从那以后你有大量的文本框,你可以设置数组中的值,并将数组分配给一个会话。

另外请看看你能做到的其他方法:

------------------------------------------------ ----------------------------



使用会话状态或应用程序变量:



使用这种技术,我将数据存储在客户机上的会话变量中,下一页将抓取它。专家建议使用应用程序变量而不是会话变量。



ASPX页面:



< asp :textbox id =txtDatarunat =serverxmlns:asp =#unknown>





< asp:button id =btnSessionStaterunat =servertext =会话状态onclick =btnSessionState_Clickxmlns:asp =#unknown>



代码隐藏:



protected void btnSessionState_Click(object sender,EventArgs e)

{

Session [Data] = txtData.Text;

Response.Redirect(SessionState.aspx);

}



接收方ASPX页:





会话状态



数据为:< ;%=会话[数据]%>

你们都设置了,运行它测试它。



使用查询字符串



使用这种技术,我将使用URL添加我的数据,并在下一页上抓取它。



ASPX Pag e:



< asp:textbox id =txtDatarunat =serverxmlns:asp =#unknown>





< asp:button id =btnQueryStringrunat =servertext =查询字符串onclick =btnQueryString_Clickxmlns:asp =#unknown> ;



代码隐藏:



protected void btnQueryString_Click(object sender,EventArgs e)

{

Response.Redirect(QueryString.aspx?Data =+ Server.UrlEncode(txtData.Text));

}



接收方ASPX页面:



查询字符串



数据是:<%= Server.UrlDecode(Request.QueryString [Data])%>

你们都设置了,运行它测试它。



使用HttpPost



使用这种技术,我将使用Request.From调用帖子返回URL和下一页。我会抓住它。 />


ASPX页面:



< asp:textbox id =txtDatarunat =serverxmlns: ASP = #未知 & gt;





< asp:button id =btnHttpPostrunat =servertext =HTTPPostpostbackurl =〜/ HttpPost .aspxxmlns:asp =#unknown>



注意:按钮属性中没有任何代码隐藏方法调用而不是postbackurl。



接收方ASPX页面:



HttpPost



数据是:<%= Request.Form [txtData]%>



你们都设置了,运行它测试它。



使用公共属性



使用这种技术我将使用公共方法发送,并在下一页上使用PreviousPage获取它.MethodName。



ASPX页面:



< asp:textbox id =txtDatarunat = serverxmlns:asp =#unknown>





< asp:button id =btnPublicPropertiesrunat =server text =Public Propertiesonclick =btnPublicProperties_Clickxmlns:asp =#unknown>



Code-Behind:



protected void btnPublicProperties_Click(object sender,EventArgs e)

{

Server.Transfer(PublicProperties.aspx );

}

公共字符串PublicData

{

get

{

返回txtData.Text;

}

}



接收方ASPX页面:



公共属性



数据为:<%= PreviousPage.PublicData%> :


1)如果你从webform1重定向到webform2直接

然后使用Server.transfer



on webform2假设

TextBox t1 =(TextBox)Page.PreviousPage.FindControl(你在webform1上的文本框名称);



2 )如果你想在应用程序中的几个页面上使用它,那么请创建一个类为这个类成员分配文本框值并将该对象存储在会话中并在几个页面上使用它


首先,一页上的100 TextBoxes 不是一个好主意。您应该考虑减少它并将其分解为多个页面或实现一些向导。如果您考虑用户体验也不好。



请参阅链接以了解有关如何在页面之间传输数据的更多信息。 - 如何:在ASP.NET网页之间传递值 [ ^ ]

Hi all,

I have a webform1.aspx, which contains 100 Textboxes . I have to pass these 100 textbox values to webform2.aspx.

Please help me which is the best way to to pass values from one page to another.

Apart from Session is any other best technique..

解决方案

There are many ways you can do it but since you have large number of textboxes you can set the values in an array and assign the array to a session.
Also please have a look at the other ways you can do it :
----------------------------------------------------------------------------

Using Session State or Application Variable :

Using this technique I will store the data in session variable on the client machine and on the next page will grab it. Using Application Variable instead of Session Variable is recommend by experts.

ASPX Page:

<asp:textbox id="txtData" runat="server" xmlns:asp="#unknown">



<asp:button id="btnSessionState" runat="server" text="Session State" onclick="btnSessionState_Click" xmlns:asp="#unknown">

Code-Behind:

protected void btnSessionState_Click(object sender, EventArgs e)
{
Session["Data"] = txtData.Text;
Response.Redirect("SessionState.aspx");
}

Receiver ASPX Page:


Session State


Data is: <%=Session["Data"] %>
And you all set, run it test it.

Using Query String

Using this technique I will add my data with URL and on the next page will grab it.

ASPX Page:

<asp:textbox id="txtData" runat="server" xmlns:asp="#unknown">



<asp:button id="btnQueryString" runat="server" text="Query String" onclick="btnQueryString_Click" xmlns:asp="#unknown">

Code-Behind:

protected void btnQueryString_Click(object sender, EventArgs e)
{
Response.Redirect("QueryString.aspx?Data=" + Server.UrlEncode(txtData.Text));
}

Receiver ASPX Page:

Query String


Data is: <%=Server.UrlDecode(Request.QueryString["Data"]) %>
And you all set, run it test it.

Using HttpPost

Using this technique I will call a post back url and the on next page using Request.From I will grab it.

ASPX Page:

<asp:textbox id="txtData" runat="server" xmlns:asp="#unknown">



<asp:button id="btnHttpPost" runat="server" text="HTTPPost" postbackurl="~/HttpPost.aspx" xmlns:asp="#unknown">

Note: There is no any code-behind method call instead of a postbackurl in button attribute.

Receiver ASPX Page:

HttpPost


Data is: <%=Request.Form["txtData"] %>

And you all set, run it test it.

Using Public Properties

Using this technique I will send the using a public method and on the next page will grab it using PreviousPage.MethodName.

ASPX Page:

<asp:textbox id="txtData" runat="server" xmlns:asp="#unknown">



<asp:button id="btnPublicProperties" runat="server" text="Public Properties" onclick="btnPublicProperties_Click" xmlns:asp="#unknown">

Code-Behind:

protected void btnPublicProperties_Click(object sender, EventArgs e)
{
Server.Transfer("PublicProperties.aspx");
}
public string PublicData
{
get
{
return txtData.Text;
}
}

Receiver ASPX Page:

Public Properties


Data is: <%=PreviousPage.PublicData %> :


1) if u redirect from webform1 to Webform2 direct
then use Server.transfer

on webform2 suppose
TextBox t1=(TextBox)Page.PreviousPage.FindControl("ur textbox name on webform1");

2)If u want to use this on several page in application
then create one class assign textbox value to this class member and store that object in session and use it on several pages


First of all, 100 TextBoxes on one Page is not a good idea. You should think of reducing it and breaking it to multiple pages or implement some wizard. It is also not good if you consider User Experience.

Refer the link to know more on how to transfer data between pages. - How to: Pass Values Between ASP.NET Web Pages[^]


这篇关于在页面之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆