简体   繁体   English

asp.net正常html输入runat =“ server”在代码隐藏中返回未更新的值

[英]asp.net normal html input runat=“server” return non-updated value in code-behind

I have edit server detail page called editServer.aspx. 我有一个名为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. 当用户单击“保存”按钮时,文本框中的值为空或未更新为用户键入的新值。

part of code in .aspx .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" />

in .aspx.cs 在.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.  
 }

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". 因此,当我打开.aspx页时,将看到带有“请勿关闭”的文本框。 When I change the value to "can shutdown" and click Save button, in aspx.cs server remarks value remains the same - "do not shutdown". 当我将值更改为“可以关闭”并单击“保存”按钮时,aspx.cs服务器中的备注值保持不变-“请勿关闭”。

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. 并且基于ASP.NET Pipeline Lifecycle ,首先执行Page_Load ,然后控制控件的事件处理程序。 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. 为了避免这种情况,仅在页面首次加载到GET请求而不是POST请求时才需要运行上述代码行。 You can change the Page_Load event handler like this... 您可以像这样更改Page_Load事件处理程序...

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;
 }

Use IsPostBack clause or every time your page loads your textbox will be populated from database value . 使用IsPostBack子句,或者每次页面加载时,将从数据库值中填充文本框。

When you click save button , it causes postback which causes your serverRemarksTB value again to "do not shutdown" . 当您单击保存按钮时,它将导致回发,这将导致您的serverRemarksTB值再次“不关闭”

if(!Page.IsPostBack)
{
serverRemarksTB.Value = server.remarks;
}

That's because your page_Load serverRemarksTB.Value = server.remarks; 那是因为您的page_Load serverRemarksTB.Value = server.remarks; code is not wrapped in IsPostback block. 代码未包装在IsPostback块中。 So when you post your form back the value of the text box gets updated to database value. 因此,当您向后发布表单时,文本框的值将更新为数据库值。 You should put your database code inside IsPostback check. 您应该将数据库代码放入IsPostback检查中。 Like 喜欢

 if(!IsPostBack)
    {
       serverRemarksTB.Value = server.remarks;
    } 

in the Page_Load method. 在Page_Load方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET c#代码隐藏中没有获得HTML输入类型的价值 - Not getting value of HTML input type in ASP.NET c# code-behind 从 asp.net 代码隐藏操作 HTML - Manipulating HTML from the asp.net code-behind 如何通过ASP.NET代码隐藏获取放置在转发器中的html文本框的值? - How to get the value of an html textbox placed in a repeater, through ASP.NET code-behind? 尽管Runat = Server,但ASP.net代码中的表单字段为空 - Form Field Null in ASP.net Code Behind Despite Runat=Server 如何在后面的代码中创建Runat服务器DIV? ASP.NET和C# - How to create a runat server DIV in the behind code ? ASP.NET & C# 为什么用&#39;runat =“ server”装饰HTML元素不能使它们从后面的代码访问? - Why does decorating HTML elements with 'runat=“server”' not make them accessible from the code-behind? ASP.NET 代码隐藏类中的静态方法是非线程安全的吗? - Are static methods in ASP.NET code-behind classes non-thread-safe? <script runat=“server”> vs. code-behind file - <script runat=“server”> vs. code-behind file 将来自用户控件的HTML文本输入值(不带runat服务器)传递给后面的代码 - Passing the value of an HTML text input (without runat server) from a user control to code behind 传递 Javascript 更新的值<input type="button">到 ASP.NET 代码后面 - Passing a Javascript-updated value from <input type="button"> to ASP.NET code behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM