数据绑定在文本框和标签上的行为不同
我有一个数据库,其中一行的“noteText”列中包含“TEXTTEXT”,而该列中的另一行则为 null
。如果我运行一个生成 1 行的查询,然后运行另一个包含 2 行的查询,则 Eval("noteText")
在放置在 TextBox
中与在放置在 TextBox
中时返回不同的信息。放置在标签
中。 TextBox
和 Label
是专门为此测试创建的;代码隐藏中没有引用它。我认为第二个查询的第 1 行的 TextBox 是以某种方式提取第一个查询的第 1 行中使用的数据。
我不认为这可能是与浏览器相关的问题,因为返回页面的源代码本身不正确。
Aspx文件代码:
<asp:TemplateField HeaderText ="Notes" SortExpression="noteid">
<ItemTemplate>
STARTXX
<asp:TextBox ID="AtxtNote" ViewStateMode="Disabled" runat="server"
TextMode= "SingleLine" Width="260" Text='<%# Eval("noteText") %>' />
<br />
<asp:Label ID="BtxtNote" runat="server" Width="260"
Text='<%# Eval("noteText") %>' />
<br />
ENDXX
</ItemTemplate>
</asp:TemplateField>
页面源代码:
STARTXX
<input name="ctl00$CPH1$gv$ctl02$AtxtNote" type="text"
value="TEXTTEXT" id="ctl00_ContentPlaceHolder1_gv_ctl02_AtxtNote"
style="width:260px;" />
<br />
<span id="ctl00_CPH1_gv_ctl02_BtxtNote"
style="display:inline-block;width:260px;"></span><br />
ENDXX
实际间距有点不同;我编辑了源代码和输出代码的文本以防止水平滚动。
我预计两个 Eval
调用都会显示空字符串。
I have a database with a row that has "TEXTTEXT" inside a "noteText" column and null
in that column for a different row. If I run a query that generates 1 rows, then run a different query that has 2 rows, Eval("noteText")
returns different information when placed in a TextBox
vs. placed in a Label
. The TextBox
and Label
were created specifically for this test; it is not referenced in the codebehind. I think the TextBox
for row 1 of the second query is somewhow pulling the data used in row1 of the first query.
I don't think it could be a browser-related issue since the source code of the returned page is itself incorrect.
Aspx file code:
<asp:TemplateField HeaderText ="Notes" SortExpression="noteid">
<ItemTemplate>
STARTXX
<asp:TextBox ID="AtxtNote" ViewStateMode="Disabled" runat="server"
TextMode= "SingleLine" Width="260" Text='<%# Eval("noteText") %>' />
<br />
<asp:Label ID="BtxtNote" runat="server" Width="260"
Text='<%# Eval("noteText") %>' />
<br />
ENDXX
</ItemTemplate>
</asp:TemplateField>
Source code on page:
STARTXX
<input name="ctl00$CPH1$gv$ctl02$AtxtNote" type="text"
value="TEXTTEXT" id="ctl00_ContentPlaceHolder1_gv_ctl02_AtxtNote"
style="width:260px;" />
<br />
<span id="ctl00_CPH1_gv_ctl02_BtxtNote"
style="display:inline-block;width:260px;"></span><br />
ENDXX
Actual spacing is a bit different; I edited the text of the source and output code to prevent horizontal scrolling.
I expected both Eval
calls to show an empty string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们通过添加更多对
DataBind
的调用来修复此问题。当前的理论是,在单击按钮查询数据库时正在提交
TextBox
。DataBind
发生后,Asp.Net 使用提交的数据重新填充TextBox
。现在,TextBox
在逻辑上对应于数据库中的不同行,但与上一页面视图中的TextBox
处于相同位置,因此具有相同的ClientID
。造成这种怀疑的原因之一是这些帖子触发了
OnTextChanged
事件。我对此仍然不满意;尽管现在正在发挥作用,但我并不完全相信我知道发生了什么。
We fixed it by adding more calls to
DataBind
.The current theory is that the
TextBox
was being submitted when clicking the button to query the database. After theDataBind
happened, Asp.Net repopulated theTextBox
with the data that was submitted. TheTextBox
now logically corresponded to a different row in the database, but was in the same position as theTextBox
from the previous page view and thus had the sameClientID
.One reason for this suspicion is that the posts were triggering
OnTextChanged
events.I'm still unhappy with this; I don't have full confidence that I know what's going on, even though it is now working.