asp.net正常html输入runat =“服务器"在代码隐藏中返回未更新的值 [英] asp.net normal html input runat="server" return non-updated value in code-behind

查看:148
本文介绍了asp.net正常html输入runat =“服务器"在代码隐藏中返回未更新的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为editServer.aspx的编辑服务器详细信息页面.在页面加载时,我从数据库中检索数据并将值设置为文本框.当用户单击保存"按钮时,文本框中的值为空或未更新为用户键入的新值.

I have edit server detail page called editServer.aspx. On page load, I retrieve data from database and set value to textbox. When user clicks save button, the value from textbox is empty or not updated to new value that users key in.

.aspx中的部分代码

part of code in .aspx

<input type="text" class="form-control" id="serverRemarksTB" name="serverRemarksTB" 
       placeholder="general server remarks" runat="server"/>
<asp:Button ID="editPhysicalServerBtn" runat="server" Text="Save" class="btn btn-success"
  style="padding-left:30px; padding-right:30px;" onclick="editPhysicalServerBtn_Click" />

在.aspx.cs中

 protected void Page_Load(object sender, EventArgs e)
 {
    //code to retrieve data from database and store in server instance
    serverRemarksTB.Value = server.remarks;
 }        
 protected void editPhysicalServerBtn_Click(object sender, EventArgs e)
 {      
     string remarks = serverRemarksTB.Value; //this is not updated.  
 }

例如,在数据库中,服务器注释为请勿关闭".因此,当我打开.aspx页时,将看到带有请勿关闭"的文本框.当我将值更改为可以关闭"并单击保存"按钮时,aspx.cs服务器中的备注值保持不变-请勿关闭".

For example, in database the server remarks is "do not shutdown". So when I open the .aspx page, I will see the textbox with "do not shutdown". When I change the value to "can shutdown" and click Save button, in aspx.cs server remarks value remains the same - "do not shutdown".

推荐答案

这是因为每次加载页面时,都会用此行代码覆盖输入的值...

It's because everytime you load the page you override the value of the input with this line of code...

serverRemarksTB.Value = server.remarks;

并基于

and based on the ASP.NET Pipeline Lifecycle, Page_Load is executed first and then controls' event handlers. To avoid that you will need to run the above mentioned line of code only when the page first load...on a GET request and not on a POST request. You can change the Page_Load event handler like this...

protected void Page_Load(object sender, EventArgs e)
{
        //code to retrieve data from database and store in server instance
        if(!IsPostBack)
            serverRemarksTB.Value = server.remarks;
 }

这篇关于asp.net正常html输入runat =“服务器"在代码隐藏中返回未更新的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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