问题
I have a FormView (with paging enabled) that is bound to a LinqDataSource on an ASP.NET page. I'm experiencing some very weird behavior and can't figure out why it's happening. For simplicity sake on this question I have removed some unneeded code (other FormView templates, etc) to demonstrate this behavior.
My FormView has 3 fields, two textboxes and one DropDownList. The DropDownList is bound to another LinqDataSource on the page and contains foreign key values. When the FormView's LinqDataSource only contains one record and I try to update it, the update fails because the selected value of the DropDownList is always empty, no matter which value I pick for it. When the FormView's LinqDataSource contains 2 or more records, it works as it should.
Now here's the really weird thing. The update is actually failing because of the FormView's PagerSettings! When I use just the default Pager settings, all is well. When I change the PagerMode to NextPreviousFirstLast
, the update fails.
Here's my FormView with it's data sources:
<asp:FormView ID="fvData" runat="server" AllowPaging="True"
DataKeyNames="ID" DataSourceID="ldsData" DefaultMode="Edit">
<EditItemTemplate>
<table class="pad5">
<tr>
<td class="field-name">AREA:</td>
<td>
<asp:DropDownList ID="cboAREA" runat="server" DataTextField="AREA_NAME"
DataValueField="AREA1" SelectedValue='<%# Bind("AREA") %>' DataSourceID="ldsAreas" />
</td>
</tr>
<tr>
<td class="field-name">LOOP:</td>
<td><asp:TextBox ID="txtLOOP" runat="server" Text='<%# Bind("LOOP") %>' /></td>
</tr>
<tr>
<td class="field-name">LOOP DESCRIPTION:</td>
<td><asp:TextBox ID="txtLOOP_DESCRIPTION" runat="server"
Text='<%# Bind("LOOP_DESCRIPTION") %>' style="width: 600px" /></td>
</tr>
</table>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" CausesValidation="True" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False" />
</EditItemTemplate>
<PagerSettings Mode="NextPreviousFirstLast"
FirstPageText="&lt;&lt; First" LastPageText="Last &gt;&gt;"
NextPageText="Next &gt;" PreviousPageText="&lt; Prev"
Position="TopAndBottom" />
<PagerStyle CssClass="pager" />
</asp:FormView>
<asp:LinqDataSource ID="ldsData" runat="server"
ContextTypeName="E_and_I.EAndIDataDataContext" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" EntityTypeName=""
TableName="INSTRUMENT_LOOP_DESCRIPTIONs" onselecting="ldsData_Selecting" OrderBy="ID ASC" >
</asp:LinqDataSource>
<asp:LinqDataSource ID="ldsAreas" runat="server"
ContextTypeName="E_and_I.EAndIDataDataContext" EntityTypeName=""
TableName="AREAs" onselecting="ldsAreas_Selecting">
</asp:LinqDataSource>
And here's both of my LinqDataSource's Selecting
events:
EAndIDataDataContext db = new EAndIDataDataContext();
protected void ldsData_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
e.Result = db.INSTRUMENT_LOOP_DESCRIPTIONs.Take(1); // we only want one record for testing
}
protected void ldsAreas_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
e.Result = db.AREAs.OrderBy(a => a.AREA1).Select(a => new { AREA1 = a.AREA1, AREA_NAME = "(" + a.AREA1 + ") " + a.AREA_NAME });
}
I've traced the problem to these lines:
<PagerSettings Mode="NextPreviousFirstLast"
FirstPageText="&lt;&lt; First" LastPageText="Last &gt;&gt;"
NextPageText="Next &gt;" PreviousPageText="&lt; Prev"
Position="TopAndBottom" />
As soon as I remove the above PagerSettings
element, the FormView updates the record just fine! Does anybody know why the hell the pager settings would have anything to do with this? I'm using the .NET Framework 4.0.
回答1:
I copied part of your code and I made an experiment and I think, I'm experimenting the same behavior.
This is what I did:
<asp:LinqDataSource runat="server" ID="lds"
TableName="jobs" ContextTypeName="WebApplication2.DataAccess.PubsDataContext" >
</asp:LinqDataSource>
<asp:LinqDataSource runat="server" ID="ldse"
TableName="employee" ContextTypeName="WebApplication2.DataAccess.PubsDataContext" OnSelecting="ldse_Selecting">
</asp:LinqDataSource>
<asp:FormView runat="server" DefaultMode="Edit" ClientIDMode="Predictable" DataKeyNames="emp_id" DataSourceID="ldse"
AllowPaging="true" OnItemCommand="Unnamed_ItemCommand" ID="formView">
<EditItemTemplate>
<div>
<asp:DropDownList runat="server" ID="ddlJobs" DataSourceID="lds" DataTextField="job_desc" DataValueField="job_id">
</asp:DropDownList>
</div>
<div>
<asp:TextBox runat="server" TextMode="MultiLine" ID="txtDesc" />
</div>
<div>
<asp:Button Text="Save" runat="server" CommandName="Save" CausesValidation="true" />
<asp:Button Text="Cancel" runat="server" CommandName="Cancel" CausesValidation="false" />
</div>
</EditItemTemplate>
<PagerSettings Mode="NextPreviousFirstLast"
FirstPageText="&lt;&lt; First" LastPageText="Last &gt;&gt;"
NextPageText="Next &gt;" PreviousPageText="&lt; Prev"
Position="TopAndBottom" />
These are my observations:
If the
FormView
contains only one row, then theFormView
behaves weird and my commands do not work as expected.My
Save
command is never fired, instead the `Cancel command is firedMy
Cancel
command causes myFormView
to disappear...
First I thought this was because of the &
characters, but this is not the case
I narrowed the problem to Position="TopAndBottom"
If you set the
Position
attribute to:Top
TopAndBottom
You will experiment the same problem. (As you mention probably a bug)
If you set the
Position
attribute to:- Buttom
It works as expected
To be honest, if this is a bug, I can't believe none else has found it and report it.
I made the same experiment with Visual Studio 2012 for Web (ASP.NET 4.5) and I'm experimenting the exact same behavior...
This makes me think that probably I'm doing something fundamentally wrong and perhaps this is not a bug and that's why I'm experimenting the same behavior with both versions ASP.NET 4 and ASP.NET 4.5, but if that's the case, I simply can't figure out what is it.
On the other hand, if this is a bug, the same bug will be present in ASP.NET 4.5
Edit 1
I uploaded the code to my GitHub site for reference
回答2:
It's most likely due to the url decoding of the & < and >.
Try taking out the &';' and just use the & symbol. You are able to inject using that method.
来源:https://stackoverflow.com/questions/12611347/is-this-a-formview-bug