从ASP获取文本:文本框 [英] Get text from asp:textbox

查看:764
本文介绍了从ASP获取文本:文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的ASP.NET项目在C#。

该UpdateUserInfo.aspx页面由文本框和按钮。在页面加载()方法我设置一些文本到文本框,当按钮被cicked我得到的文本框的新值,并将其写入到数据库。

问题是,即使我已经改变文本框textbox.Text的值()方法返回文本框(SomeText则会)的旧值和写入DB这一点。

下面的方法:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    textbox.text =SomeText则会;
}无效Btn_Click(对象发件人,EventArgs的发送)
{
    字符串textbox_text = textbox.text(); //这仍是等于someValue中,连我改变文本框的值
    writeToDB(textbox_text);
}

那么,如何使文本框与someValue中初步显现,但是当用户改变这个值的getText方法返回新的当前值,并写入数据库呢?


解决方案

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
   如果(!Page.IsPostBack)
    {
       textbox.text =SomeText则会;
    }
}

回发上按一下按钮设置textboxs text属性回someValue中,你要设置的值只能作为一次以上。

回发解释说:


  

在ASP网页开发的背景下,回发是另一个名字
  HTTP POST。在交互式网页,表单的内容被发送
  到服务器用于处理某些信息。然后,服务器
  发送一个新的页面返回给浏览器。


  
  

这是为了验证密码在网上登录,过程中
  订单,或者客户端计算机不能​​做其他任务等
  它自己的。这不是与刷新混淆或收回行动
  通过在浏览器上的按钮。


来源

视图状态也将有助于理解这一切是如何结合在一起阅读起来。

I am writing ASP.NET project in C#.

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.

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.

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?

解决方案

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.

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.

Source

Reading up on View State will also be helpful in understanding how it all fits together.

这篇关于从ASP获取文本:文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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