表单帖子不包含文本框数据 [ASP.NET C#]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:11:23  来源:igfitidea点击:

Form post doesn't contain textbox data [ASP.NET C#]

提问by Darren Steinweg

I have several "ASP:TextBox" controls on a form (about 20).
When the form loads, the text boxes are populated from a database.
The user can change the populated values, and when they submit the form, I take the values posted to the server and conditionally save them (determined by some business logic).
All but 1 of the text boxes work as intended.

我在一个表单上有几个“ASP:TextBox”控件(大约 20 个)。
当表单加载时,文本框从数据库填充。
用户可以更改填充的值,当他们提交表单时,我将发布到服务器的值并有条件地保存它们(由某些业务逻辑确定)。
除了 1 个文本框外,所有文本框都按预期工作。

The odd box out, upon postback, does not contain the updated value that the user typed into the box.
When debugging the application, it is clear that myTextBox.Textreflects the old, pre-populated value, not the new, user-supplied value.
Every other box properly shows their respective user-supplied values.

在回发时,奇数框输出不包含用户在框中键入的更新值。
调试应用程序时,很明显myTextBox.Text反映的是旧的预填充值,而不是用户提供的新值。
每隔一个框正确显示它们各自的用户提供的值。

I did find a workaround.
My solution was to basically extract the text box's value out of the Request.Formobject: Request.Form[myTextBox.UniqueID], which does contain the user-supplied value.

我确实找到了解决方法。
我的解决方案是基本上从Request.Formobject: 中提取文本框的值Request.Form[myTextBox.UniqueID],其中包含用户提供的值。

What could be going on, here?
As I mentioned, the other text boxes receive the user-supplied values just fine, and this particular problematic text box doesn't have any logic associated to it -- it just takes the value and saves it.
The main difference between this text box and the others is that this is a multi-line box (for inputting notes), which I believe is rendered as an HTML "textarea" tag instead of an "input" tag in ASP.NET.

这里会发生什么?
正如我所提到的,其他文本框可以很好地接收用户提供的值,而这个有问题的特定文本框没有与之关联的任何逻辑——它只是获取值并保存它。
这个文本框和其他文本框的主要区别在于,这是一个多行框(用于输入笔记),我相信它呈现为 HTML“textarea”标签,而不是 ASP.NET 中的“input”标签。

采纳答案by Darren Kopp

Are you initially loading the data only when !Page.IsPostBack? Also, is view state enabled for the text box?

您是否最初仅在 !Page.IsPostBack 时加载数据?另外,是否为文本框启用了视图状态?

回答by FlySwat

Remember the order of the page lifecycle, and where you are databinding your form.

记住页面生命周期的顺序,以及您在哪里对表单进行数据绑定。

  • PreInit
  • Init
  • Load
  • Your Control Event Handler
  • 预初始化
  • 在里面
  • 加载
  • 您的控制事件处理程序

If you are reading the value in the Control Event handler, yet databinding in Init or Load, you'll have the old value.

如果您正在读取控制事件处理程序中的值,但在 Init 或 Load 中进行数据绑定,您将拥有旧值。

The trick is to always databind in the correct event, or check for postback and don't databind then.

诀窍是始终在正确的事件中进行数据绑定,或者检查回发然后不进行数据绑定。

回答by Rob Cooper

I would second Jonathan's responseI would check your databinding settings.

我会第二个乔纳森的回应我会检查你的数据绑定设置。

If you do not need ViewState for the textboxes (i.e. no postback occurs until form submit) then you should disable it.

如果您不需要文本框的 ViewState(即在提交表单之前不会发生回发),那么您应该禁用它。

It sounds like you are not having problems saving the data (since you said you have managed to get the control to read the correctdata back). Therefore, I would say the problem loads in your databinding code.

听起来您保存数据没有问题(因为您说您已设法获得读取正确数据的控制权)。因此,我会说问题加载到您的数据绑定代码中。

回答by Jon Erickson

this happens to me all the time.

这一直发生在我身上。

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // populate text boxes from database
    }
}

回答by Darren Steinweg

Are you initially loading the data only when !Page.IsPostBack? Also, is view state enabled for the text box?

您是否最初仅在 !Page.IsPostBack 时加载数据?另外,是否为文本框启用了视图状态?

I had almost forgotten to check the ViewState, but ended up remembering to verify that it wasn't disabled before making my post here on SO. I even set EnableViewState="true" to make sure.

我几乎忘记检查 ViewState,但最终记得在 SO 上发表我的帖子之前验证它没有被禁用。我什至设置了 EnableViewState="true" 来确保。

I did find the solution, and it coincided with most of the answers here. The form was indeed loading its data more than once (which is intentional behavior). I implemented some special code for this field, and all is well.

我确实找到了解决方案,它与这里的大多数答案一致。表单确实不止一次加载其数据(这是有意的行为)。我为这个领域实现了一些特殊的代码,一切都很好。

Thanks for your replies, all!

谢谢大家的回复!