Using Bind in TextBox renders bound data outside the TextBox control
I have a ListView
(in an update panel) bound to an ObjectDataSource
(connected to an NHibernate object) and in the EditItemTemplate
I use
Text='<%# Bind("HideLocation")%>'
to bind to a string property of the object.
When I click edit (twice, another issue I'm having) and view the code in Chrome, the output looks like this:
<div class="InputLine InputLineLevel2">
<div class="InputLabel">Hide Location:</div>
<div class="InputControl">
"
In locker 2317"
<input
name="ctl00$MainContentPlaceholder$ItemDetailsListView$ctrl0$txtItemHideLocation"
type="text"
maxlength="128"
id="ctl00_MainContentPlaceholder_ItemDetailsListView_ctrl0_txtItemHideLocation"
style="width:300px;">
</div>
</div>
The .aspx code looks like this:
<asp:UpdatePanel ID="ItemDetailsUpdatePanel" UpdateMode="Always" runat="server">
<asp:ListView ID="ItemDetailsListView" runat="ser开发者_如何学Cver" ...>
<EditItemTemplate>
<fieldset class="FieldsetLevel2">
<legend>Edit Item: <%# Eval("SearchItem.SearchItemName") %></legend>
<div class="InputLine InputLineLevel2">
<div class="InputLabel">Hide Location:</div>
<div class="InputControl">
<asp:TextBox ID="txtItemHideLocation" Width="300px"
Text='<%# Bind("HideLocation")%>'
runat="server"></asp:TextBox>
</div>
</div>
... a bunch of other controls & end tags
Any ideas why the value of the bound data (in this case: In locker 2317) is showing up outside of the text box instead of as the contents of the data?
Well, I found a solution, and it's an interesting one. I also provided false information in my original question, and that might have made all the difference.
I moved all my code into a separate project and pared it down to just the basics. I was still having to click buttons twice to get a refresh, and the text was still outside the TextBox. I finially found someone describing a different problem that made me realise I was not binding to an object datasource. I was binding a control inside the ListView, but not the listview itself. The listview was bound to the ISet returned from one of my data model objects.
So, the problem was that (apparently, I'll explain why I'm not convinced below) when you're not using an xDataSource control, you need to databind whenever you change the state of the ListView. I imagine this concept ports to other data controls as well, in fact I know I've run into it before with other DataBound controls.
What seemed to be different this time, and why I didn't clue in right away, was because it almost worked. The event fires on the first postback, and the EditItemIndex is set. This has no effect on the display (despite happening before the PreRender event) so I'm guessing it has to do with the interaction between the event model and the internal workings of the ListView control. On second postback however (clicking the button again) the event was not fired, but the display was updated (with data from my datasource, albeit not where I wanted it to be).
When I modified my code to databind after changing the EditIndex, not only did it remove the need for the second postback, but it placed the databound text inside the TextBox, instead of before it.
精彩评论