将文本框文本从一种Web表单移动到另一种Web表单 [英] moving textbox text from one web form to another

查看:103
本文介绍了将文本框文本从一种Web表单移动到另一种Web表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天正在尝试多种形式,试图在一个页面上获取文本框的文本值,以在重定向后在下一页中显示为标签.即时通讯没有收到任何错误,但同时也无法正常工作,对此没有任何帮助.

Webform主控件

按钮-ButtonMoveName
文本框-TextBoxNametoMove

代码...

playing round with multiple forms today trying to get the text value of a textbox on one page to show as a label in the next page after a redirect. im not getting any errors but at the same time its not working either, any help on this.?


Webform Main Controls

Button - ButtonMoveName
Textbox - TextBoxNametoMove

Code...

protected void ButtonMoveName_Click(object sender, EventArgs e)
        {
            Response.Redirect("GrabName.aspx");
        }




Webform GrabName控件

标签-LabelMovedName

代码...




Webform GrabName Controls

Label - LabelMovedName

Code...

protected void Page_Load(object sender, EventArgs e)
        {
            if (PreviousPage != null)
            {
                TextBox movedNameTextbox =(TextBox)PreviousPage.FindControl("TextBoxNametoMove");
                LabelMovedName.Text = movedNameTextbox.Text;
            }
        }

推荐答案

为按钮添加PostBackUrl
Add PostBackUrl for the button


使用回发网址:
引入了ASP.NET 2.0跨页面发布.通过PreviousPage对象,您可以在此处重定向从哪个页面搜索控件的值.要实现跨页面发布,您必须将page1按钮的PostBackUrl属性定义为page2.喜欢:


Using Post Back URL:
ASP.NET 2.0 has been introduced Cross page posting. By PreviousPage object you can search controls value from which page you are redirected here. To implement Cross page posting you have to define the PostBackUrl property of page1 button to page2. Like:

<asp:button id="cmdTransfer" runat="server" text="Transfer" postbackurl="page2.aspx" xmlns:asp="#unknown" />



现在,在page2 load事件中编写以下代码:



Now write the below code in page2 load event:

TextBox txtName = (TextBox)(PreviousPage.FindControl("txtName"));
string myname =txtName.Text;

FYI
将数据/参数/值从一个aspx页面传递到另一个aspx页面 [ ^ ]

FYI
Passing data/parameters/values from one aspx page to another aspx page[^]


很可能是因为文本框控件被设置为保护状态.将文本框控件设置为public或更好的方法是将属性添加到上一页.像下面这样.注意我不会像我做页面PreviousPage那样命名.仅作为示例.

您可以通过以下链接进行操作

PreviousPage.FindControl

[ ^ ]


或者您也可以使用ViewState或Session来存储文本框值. 如果解决了您的问题,则标记为解决方案
Likely it is because the text box control is set to protected. Either set the textbox control to public or the better approach is to add a property to the previous page. Something like below. Note I would not name like I did a page PreviousPage. It is for example only.

You can go though following link to get it work

PreviousPage.FindControl

[^]


Or Instead of this you can simply use ViewState or Session to store textbox value also.
Mark as solution if its solved your problem


最好将会话对象中的文本另存为
Session ["prevoiusPageText"] = TextBoxNametoMove.Text;
Its better to save the text in a session object as
Session["prevoiusPageText"]=TextBoxNametoMove.Text;


这篇关于将文本框文本从一种Web表单移动到另一种Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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