我有一個FormView用於查看,編輯和插入項目到MSSQL數據庫。 FormView連接到一個LinqDataSource,我使用它的Selecting
事件過濾基於查詢字符串中傳遞的參數(列)在FormView中顯示的數據。保存FormView的過濾結果時,DropDownList值爲空
在FormView中,我有一個DropDownList來顯示相關表中的值。除了當我嘗試編輯時,所有東西都可以正常工作 - 出於某種原因,當我嘗試保存編輯時(即使選擇了某個值),DropDownList的選定值始終爲空。插入工作,因爲它應該。
我已經將問題追溯到我做過濾的Selecting
事件。如果我註釋掉進行過濾的方法,它會成功更新項目 - 但我無法弄清楚爲什麼過濾正在破壞更新。
這裏是我的(縮短)FormView控件:
<asp:FormView ID="fvData" runat="server" AllowPaging="True"
DataKeyNames="ID" DataSourceID="ldsData"
ondatabound="fvData_DataBound">
<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>
<InsertItemTemplate>
<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") %>' AppendDataBoundItems="true" DataSourceID="ldsAreas">
<asp:ListItem Text="" Value="" />
</asp:DropDownList>
</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="btnInsert" runat="server" Text="Insert" CommandName="Insert" CausesValidation="True" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False" />
</InsertItemTemplate>
</asp:FormView>
的使用LinqDataSource:
<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>
我ldsData_Selecting
方法:
protected void ldsData_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
EI.FilterData<INSTRUMENT_LOOP_DESCRIPTION>(ref e, db.INSTRUMENT_LOOP_DESCRIPTIONs, this.db);
}
最後EI.FilterData
:
public static void FilterData<T>(ref LinqDataSourceSelectEventArgs e, IEnumerable<T> source, EAndIDataDataContext db)
{
if (HttpContext.Current.Request.QueryString.Keys.Count > 0)
{
var result = source.AsQueryable();
bool filtered = false;
// get column names
var columnNames = db.Mapping.MappingSource.GetModel(typeof(EAndIDataDataContext)).GetMetaType(typeof(T)).DataMembers;
foreach (string key in HttpContext.Current.Request.QueryString.Keys)
{
string val = HttpContext.Current.Request.QueryString[key];
// check the query string key exists as a column in the table, etc
if (columnNames.SingleOrDefault(c => c.Name == key) != null && val.Trim() != "" && val != "*")
{
result = result.WhereLike(key, val.Replace("?", "_").Replace("*", "%"));
filtered = true;
}
}
if (filtered)
e.Result = result;
}
}
是否有任何理由篩選結果會破壞更新(並且只有DropDownList的 - 文本框工作正常)?過濾完美地工作(也就是說,FormView只顯示用戶輸入的記錄參數);如果你想知道WhereLike
擴展方法的作用,你可以查看this question's的答案。
嘗試在ldsData_Selecting處理程序中編寫過濾器代碼並查看會發生什麼情況?我認爲''錯誤*'ondatabound =「fvData_DataBound」' – adatapost
過濾器代碼只在'ldsData_Selecting'事件中執行,而不是在'fvData_DataBound'中執行。如果它處於ReadOnly模式,'fvData_DataBound'事件只是在FormView中設置一個標籤。 – Hoff