C# 从 asp:textbox 获取文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10948360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get text from asp:textbox
提问by Nurlan
I am writing ASP.NET project in C#.
我正在用 C# 编写 ASP.NET 项目。
The UpdateUserInfo.aspx page consists textboxes and button. In pageLoad() method I set some text to the textbox and when button is cicked I get the new value of textbox and write it into DB.
UpdateUserInfo.aspx 页面由文本框和按钮组成。在 pageLoad() 方法中,我为文本框设置了一些文本,当单击按钮时,我会获取文本框的新值并将其写入数据库。
The problem is even if I have changed the value of textbox textbox.Text() method returns the old value of textbox ("sometext") and write this into DB.
问题是即使我改变了文本框 textbox.Text() 方法的值返回文本框的旧值(“sometext”)并将其写入数据库。
Here the methods:
这里的方法:
protected void Page_Load(object sender, EventArgs e)
{
textbox.text = "sometext";
}
void Btn_Click(Object sender,EventArgs e)
{
String textbox_text = textbox.text();// this is still equals "somevalue", even I change the textbox value
writeToDB(textbox_text);
}
So, how to make textbox to appear with somevalue initially, but when user changes this value getText method return the new changed value and write this into DB?
那么,如何使文本框最初显示为某个值,但是当用户更改此值时,getText 方法返回新更改的值并将其写入数据库?
采纳答案by dtsg
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
textbox.text = "sometext";
}
}
Postback is setting the textboxs text property back to "somevalue"on button click, you'll want to set the value only once as above.
回发是将文本框文本属性设置回"somevalue"按钮单击时,您只需要设置一次该值,如上所述。
Postback explained:
回传解释:
In the context of ASP web development, a postback is another name for HTTP POST. In an interactive webpage, the contents of a form are sent to the server for processing some information. Afterwards, the server sends a new page back to the browser.
This is done to verify passwords for logging in, process an on-line order form, or other such tasks that a client computer cannot do on its own. This is not to be confused with refresh or back actions taken by the buttons on the browser.
在 ASP Web 开发的上下文中,回发是 HTTP POST 的另一个名称。在交互式网页中,表单的内容被发送到服务器以处理一些信息。之后,服务器将一个新页面发送回浏览器。
这样做是为了验证登录密码、处理在线订单或客户端计算机无法自行完成的其他此类任务。这不要与浏览器上的按钮执行的刷新或返回操作相混淆。
Reading up on View Statewill also be helpful in understanding how it all fits together.
阅读View State也将有助于理解它们是如何组合在一起的。
回答by Raab
Actually on page load textboxis re-initilized
实际上在页面加载时textbox重新初始化
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
textbox.text = "sometext";
}
}
void Btn_Click(Object sender,EventArgs e)
{
String textbox_text = textbox.text;
writeToDB(textbox_text);
}
回答by Amol Kolekar
Please check Page PostBack in the Page Load Event....
请检查页面加载事件中的页面回发....
回答by Vinod
Try this:
尝试这个:
If (!IsPostBack)
{
textbox.text = "sometext";
}

