我有一个带有许多 asp:TextBox 的表单。用户可以编辑这些文本框中的文本,当按下“保存”按钮时,这些新值将写入数据库。
我总共有3个文本框,前2个文本框的新值毫无问题地传递到后面的代码。然而,第三个文本框的旧内容被传递到服务器,而不是用户编辑的数据。
有问题的文本框的标记如下所示:
<asp:TextBox ID="descriptionBox" ReadOnly="true" style" runat="server" TextMode="MultiLine" Text='<%# Eval("Description") %>'></asp:TextBox>
以下是 C# 代码,它尝试在用户编辑文本框后获取文本框的内容。请注意,这些文本框放置在转发器中:
string description = ((TextBox)e.Item.FindControl("descriptionBox")).Text;
有人可以建议问题可能是什么吗?其他文本框使用完全相同的标记和代码,并且它们工作得很好。
最佳答案
请参阅MSDN documentation - 这是导致此问题的 ReadOnly 属性:
The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box.
另一种方法是在客户端执行此操作:
descriptionBox.Attributes["readonly"] = "readonly"
关于c# - 尝试查找 TextBox 中包含的值时出现奇怪的 ASP.NET 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8010353/