I have a web form that allows the user to modify data in certain fields (mostly TextBox controls, with a couple of CheckBox, DropDownList, and one RadioButtonList control) with a submit button to save the changes. Pretty standard stuff. The catch is, I need to keep track of which fields they modified. So I'm using ASP.NET HiddenField controls to store the original value and then on submit comparing that to the value of the corresponding TextBox (for example) control to determine which fields have been modified.
我有一个Web表单,允许用户使用提交按钮修改某些字段中的数据(主要是TextBox控件,带有几个CheckBox,DropDownList和一个RadioButtonList控件)以保存更改。很标准的东西。问题是,我需要跟踪他们修改的字段。所以我使用ASP.NET HiddenField控件来存储原始值,然后在提交时将其与相应的TextBox(例如)控件的值进行比较,以确定哪些字段已被修改。
However, when I submit the form and do the comparison, the value of the TextBox control in the code behind still reflects the original value, even though I have changed the contents of the TextBox, so it isn't registering the change. Here is an example of a set of TextBox/HiddenField pairings (in this case last, first, middle names) in my ASP.NET form:
但是,当我提交表单并进行比较时,后面代码中TextBox控件的值仍然反映原始值,即使我已经更改了TextBox的内容,因此它没有注册更改。以下是我的ASP.NET窗体中一组TextBox / HiddenField配对(在本例中为last,first,middle name)的示例:
<div id="editName" class="editField" style="display: none">
<asp:TextBox ID="tbxLName" runat="server" class="editable"></asp:TextBox>,
<asp:TextBox ID="tbxFName" runat="server" class="editable"></asp:TextBox>
<asp:TextBox ID="tbxMName" runat="server" class="editable"></asp:TextBox>
<asp:HiddenField ID="hdnLName" runat="server" />
<asp:HiddenField ID="hdnFName" runat="server" />
<asp:HiddenField ID="hdnMName" runat="server" />
</div>
I'm setting the original values of all these controls (".Text" for the TextBox controls, ".Value" for the HiddenField controls) on PageLoad in the code behind.
我在后面的代码中的PageLoad上设置所有这些控件的原始值(TextBox控件的“.Text”,HiddenField控件的“.Value”)。
Here's an example of where I'm doing the comparison when I submit the form (I'm adding the field name, old value, and new value to List<string> objects if the values differ):
这是我在提交表单时进行比较的示例(如果值不同,我将字段名称,旧值和新值添加到List
if (tbxLName.Text != hdnLName.Value)
{
changes.Add("ConsumerLastName");
oldVal.Add(hdnLName.Value);
newVal.Add(tbxLName.Text);
}
But when I enter a new value into the TextBox control and click Submit:
但是当我在TextBox控件中输入一个新值并单击Submit时:
then step through the code in the debugger, it shows me that the value of the control is still the old value:
然后逐步调试调试器中的代码,它告诉我控件的值仍然是旧值:
Why is the comparison happening against the original value of the TextBox even though the new value is there when I click the submit button?
为什么比较发生在TextBox的原始值上,即使单击提交按钮时新值存在?
Update: @David gets the credit for this, even though he didn't post it as an answer -- I was forgetting to enclose the method for pre-filling the original values of the controls in a check for IsPostBack; I really should have known better, I've been doing this for quite a while!
更新:@David得到了这个功劳,即使他没有把它作为答案发布 - 我忘了在IsPostBack的支票中附上预先填充控件原始值的方法;我真的应该知道的更好,我已经做了很长一段时间了!
4 个解决方案
#1
15
Are you checking for IsPostback in Page_Load so you don't overwrite the values sent in the Postback?
您是否在Page_Load中检查IsPostback,因此您不会覆盖在回发中发送的值?
#2
1
Make sure that you are not overwriting your values in the Page_Load
method:
确保您没有在Page_Load方法中覆盖您的值:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
someTextField = "Some Value";
}
}
It took a while for me to get that the Page_Load
method works as an "before anything goes" method and not only a method that is being ran when you visit the page with GET
.
我需要一段时间才能得到Page_Load方法作为“before before goes”方法,而不仅仅是当您使用GET访问页面时正在运行的方法。
#3
0
Make sure you're not overwriting the value for the textbox somewhere in page init or load without checking for the IsPostback flag.
确保您没有在页面init或加载的某处覆盖文本框的值而不检查IsPostback标志。
#4
0
It may happen due to postback. If you code for set textbox not in !isPostBack
then put it.
它可能由于回发而发生。如果您编写的set textbox不在!isPostBack中,那么就把它放进去吧。
i.e.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
tbxLName.Text="anything";
}
}
#1
15
Are you checking for IsPostback in Page_Load so you don't overwrite the values sent in the Postback?
您是否在Page_Load中检查IsPostback,因此您不会覆盖在回发中发送的值?
#2
1
Make sure that you are not overwriting your values in the Page_Load
method:
确保您没有在Page_Load方法中覆盖您的值:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
someTextField = "Some Value";
}
}
It took a while for me to get that the Page_Load
method works as an "before anything goes" method and not only a method that is being ran when you visit the page with GET
.
我需要一段时间才能得到Page_Load方法作为“before before goes”方法,而不仅仅是当您使用GET访问页面时正在运行的方法。
#3
0
Make sure you're not overwriting the value for the textbox somewhere in page init or load without checking for the IsPostback flag.
确保您没有在页面init或加载的某处覆盖文本框的值而不检查IsPostback标志。
#4
0
It may happen due to postback. If you code for set textbox not in !isPostBack
then put it.
它可能由于回发而发生。如果您编写的set textbox不在!isPostBack中,那么就把它放进去吧。
i.e.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
tbxLName.Text="anything";
}
}