Note that there are some explanatory texts on larger screens.

plurals
  1. PODropDownList value is blank when saving a FormView's filtered result
    primarykey
    data
    text
    <p>I have a FormView that is used for viewing, editing and inserting items into a MSSQL database. The FormView is wired up to a LinqDataSource, and I use its <code>Selecting</code> event to filter the data displayed in the FormView based on parameters (columns) passed in the query string.</p> <p>In the FormView I have a DropDownList that displays values from a related table. Everything works as it should except when I try to edit - for some reason, the selected value of the DropDownList is always empty when I try to save the edit (even when I have a value selected). The insert works as it should.</p> <p>I have traced the problem down to my <code>Selecting</code> event where I do the filtering. If I comment out the method that does the filtering, it updates the item successfully - but I can't figure out why the filtering is breaking the update.</p> <p>Here is my (shortened) FormView:</p> <pre><code>&lt;asp:FormView ID="fvData" runat="server" AllowPaging="True" DataKeyNames="ID" DataSourceID="ldsData" ondatabound="fvData_DataBound"&gt; &lt;EditItemTemplate&gt; &lt;table class="pad5"&gt; &lt;tr&gt; &lt;td class="field-name"&gt;AREA:&lt;/td&gt; &lt;td&gt;&lt;asp:DropDownList ID="cboAREA" runat="server" DataTextField="AREA_NAME" DataValueField="AREA1" SelectedValue='&lt;%# Bind("AREA") %&gt;' DataSourceID="ldsAreas" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="field-name"&gt;LOOP:&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID="txtLOOP" runat="server" Text='&lt;%# Bind("LOOP") %&gt;' /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="field-name"&gt;LOOP DESCRIPTION:&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID="txtLOOP_DESCRIPTION" runat="server" Text='&lt;%# Bind("LOOP_DESCRIPTION") %&gt;' style="width: 600px" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" CausesValidation="True" /&gt; &lt;asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False" /&gt; &lt;/EditItemTemplate&gt; &lt;InsertItemTemplate&gt; &lt;table class="pad5"&gt; &lt;tr&gt; &lt;td class="field-name"&gt;AREA:&lt;/td&gt; &lt;td&gt; &lt;asp:DropDownList ID="cboAREA" runat="server" DataTextField="AREA_NAME" DataValueField="AREA1" SelectedValue='&lt;%# Bind("AREA") %&gt;' AppendDataBoundItems="true" DataSourceID="ldsAreas"&gt; &lt;asp:ListItem Text="" Value="" /&gt; &lt;/asp:DropDownList&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="field-name"&gt;LOOP:&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID="txtLOOP" runat="server" Text='&lt;%# Bind("LOOP") %&gt;' /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="field-name"&gt;LOOP DESCRIPTION:&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID="txtLOOP_DESCRIPTION" runat="server" Text='&lt;%# Bind("LOOP_DESCRIPTION") %&gt;' style="width: 600px" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Insert" CausesValidation="True" /&gt; &lt;asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False" /&gt; &lt;/InsertItemTemplate&gt; &lt;/asp:FormView&gt; </code></pre> <p>The LinqDataSource:</p> <pre><code>&lt;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" &gt; &lt;/asp:LinqDataSource&gt; </code></pre> <p>My <code>ldsData_Selecting</code> method:</p> <pre><code>protected void ldsData_Selecting(object sender, LinqDataSourceSelectEventArgs e) { EI.FilterData&lt;INSTRUMENT_LOOP_DESCRIPTION&gt;(ref e, db.INSTRUMENT_LOOP_DESCRIPTIONs, this.db); } </code></pre> <p>And finally <code>EI.FilterData</code>:</p> <pre><code>public static void FilterData&lt;T&gt;(ref LinqDataSourceSelectEventArgs e, IEnumerable&lt;T&gt; source, EAndIDataDataContext db) { if (HttpContext.Current.Request.QueryString.Keys.Count &gt; 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 =&gt; c.Name == key) != null &amp;&amp; val.Trim() != "" &amp;&amp; val != "*") { result = result.WhereLike(key, val.Replace("?", "_").Replace("*", "%")); filtered = true; } } if (filtered) e.Result = result; } } </code></pre> <p>Is there any reason filtering the results would break the update (and ONLY the DropDownList's - textboxes work fine)? The filtering works perfectly (that is, the FormView only shows the records based parameters entered by the user); if you're wondering what the <code>WhereLike</code> extension method does, you can check out <a href="https://stackoverflow.com/questions/11512539/using-wildcards-with-a-linqdatasource">this question's</a> answer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload