未检测到用户更新文本框 [英] Textbox update by user not being detected

查看:95
本文介绍了未检测到用户更新文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net中使用c#并且我有一个ASP TextBox,我将它的文本设置为代码中的当前用户名,然后有一个更新按钮来保存它。当我从X1更改为X2后检查文本时,它仍然返回X1而不是X2。



我尝试过:



页面加载时运行的代码:

 UsernameTextBox.Text = DataValue.Action(Users,Username,SessionID,value); 



单击按钮时运行的内容:

 System.Windows.Forms.MessageBox.Show (UsernameTextBox.Text); 





TextBox:

< asp:TextBox runat =serverID =UsernameTextBoxclass =form-control/> 





按钮:

< asp:按钮runat = serverid =SaveOnClick =Save_Clickclass =btn btn-primaryText =Save Changes/> 

解决方案

不完全清楚,但最可能的原因是你的在页面加载时运行的代码不在 if(!IsPostBack){... } 阻止。



当您单击按钮时,会触发 Page_Load 事件。如果你没有检查 IsPostBack 属性,你最终会再次运行你的代码,覆盖用户输入的任何值。



在加载值之前测试属性:

  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
UsernameTextBox.Text = DataValue.Action( 用户 用户名 SessionID value );
}
}





注意: 系统。 Windows.Forms.MessageBox.Show NOT 在ASP.NET应用程序中工作。代码在服务器上运行 ,这就是消息框出现的地方。



可能似乎在Visual Studio中调试代码时工作;但这只是因为,在这种特定情况下,服务器和客户端是同一台计算机。



一旦将代码部署到真实服务器,其中一个事情会发生。最好的情况是代码抛出一个异常,告诉您无法在非交互式进程中显示消息框。另一个选项是消息框出现在服务器上,没有人会看到它,你的代码挂起等待某人登录到服务器并在数百条等待消息上单击确定


Hi, in asp.net im using c# and I have a ASP TextBox, I set the text of it to the current username in the code, then there is an update button to save it. When I check the text of it after changing it from like "X1" to "X2" it still returns "X1" instead of "X2".

What I have tried:

Code that run's when page loads:

UsernameTextBox.Text = DataValue.Action("Users", "Username", "SessionID", value);


What's ran when the button is clicked:

System.Windows.Forms.MessageBox.Show(UsernameTextBox.Text);



TextBox:

<asp:TextBox runat="server" ID="UsernameTextBox" class="form-control" />



Button:

<asp:Button runat="server" id="Save" OnClick="Save_Click" class="btn btn-primary" Text="Save Changes" />

解决方案

Not entirely clear, but the most likely cause is that your "Code that run's when page loads" is not inside an if (!IsPostBack) { ... } block.

When you click the button, the Page_Load event fires. If you don't check the IsPostBack property, you end up running your code again, overwriting whatever value the user has entered.

Test the property before loading the value:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        UsernameTextBox.Text = DataValue.Action("Users", "Username", "SessionID", value);
    }
}



NB: System.Windows.Forms.MessageBox.Show will NOT work in an ASP.NET application. The code is running on the server, and that is where the messagebox will appear.

It might seem to work when you're debugging the code in Visual Studio; but that's only because, in that specific case, the server and client are the same computer.

As soon as you deploy your code to a real server, one of two things will happen. The best case scenario is that the code throws an exception telling you that you can't display a messagebox in a non-interactive process. The other option is that the messagebox appears on the server, where nobody will ever see it, and your code hangs waiting for someone to log in to the server and click "OK" on the hundreds of waiting messages.


这篇关于未检测到用户更新文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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