Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set DropDownList's selected value inside a Repeater?
    primarykey
    data
    text
    <p>I need to create a group of <code>DropDownList</code>s to show and allow change of a property group for an item.</p> <p>I have the following code on my ASP page.</p> <pre><code>&lt;asp:Repeater runat="server" ID="repeaterProperties"&gt; &lt;ItemTemplate&gt; &lt;p&gt;&lt;asp:DropDownList runat="server" ID="ddProperty" OnInit="ddProperty_OnInit" /&gt;&lt;p&gt; &lt;/ItemTemplate&gt; &lt;/asp:Repeater&gt; </code></pre> <p>The <code>ddProperty_OnInit</code> populates the <code>DropDownList</code> with all the possible values with a database query.</p> <p>How can I set the selected value of each created <code>DropDownList</code> according to the <code>Repeater</code>'s source data?</p> <p>Let's say, for example, that we have the possible property values of <code>A</code>, <code>B</code> and <code>C</code>.<br> If the database output for the <code>Repeater</code> contains two of those values, <code>A</code> and <code>B</code>, the <code>Repeater</code> outputs two <code>DropDownList</code>s, both with all 3 values availabla and the first one with <code>A</code> as selected value and the second one with <code>B</code> as selected value.</p> <p><strong>Edit:</strong><br> It seems that adding <code>OnItemDataBound="repeater_ItemDataBound"</code> to the <code>Repeater</code> and selecting the appropriate value in there is not the way to go in my case. This is because I also need to save the possibly changed values to a database.</p> <p>The <code>ItemDataBound</code> event of the <code>Repeater</code> is fired before the <code>OnClick</code> event on a <code>Button</code> and changes the selected values to their old values before the new selections can be saved.</p> <p>Any suggestion on how to work around this?</p> <p><em>Current code:</em></p> <pre><code>&lt;asp:Repeater runat="server" ID="repeaterJako" OnItemDataBound="repeater_ItemDataBound"&gt; &lt;ItemTemplate&gt; &lt;asp:DropDownList id="ddJako" runat="server" OnInit="ddJako_OnInit"&gt; &lt;/asp:DropDownList&gt;&lt;br /&gt; &lt;/ItemTemplate&gt; &lt;/asp:Repeater&gt; &lt;asp:Button runat="server" id="updateButton" Text="Save" OnClick="update_OnClick" /&gt; </code></pre> <p>In the code-behind, <code>ddJako_OnInit</code> populates the drop down list with all the possible choises, while the <code>repeater_ItemDataBound</code> uses the method suggested by Bryan Parker to select the proper value.</p>
    singulars
    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.
    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