[英]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.