Databinding behaving differently on textbox and label
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 pa开发者_JS百科ge 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.
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 the DataBind
happened, Asp.Net repopulated the TextBox
with the data that was submitted. The TextBox
now logically corresponded to a different row in the database, but was in the same position as the TextBox
from the previous page view and thus had the same ClientID
.
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.
精彩评论