title: Setting selected value on a dropdownlist from codebehind in a repeater in a formview
body: <p>So here's the situation.
  I have a dropdownlist in a repeater that is inside a formview.</p>
  
  <p>And the really special part is that the repeater is being used to add multiple rows of data dynamically.</p>
  
  <p>What I've been trying to do and failing is to set the selected value of the drop down.</p>
  
  <pre><code>&lt;asp:FormView ID="FormView3" runat="server" DataKeyNames="t_id" 
                  DataSourceID="SqlDataSource25" DefaultMode="Insert"&gt;    
                  &lt;InsertItemTemplate&gt;
                      t_p_id:
                      &lt;asp:TextBox ID="t_p_idTextBox" runat="server" Text='&lt;%# Bind("t_p_id") %&gt;' /&gt;
                          &lt;br /&gt;
  
                      t_step:
                      &lt;asp:TextBox ID="t_stepTextBox" runat="server" Text='&lt;%# Bind("t_step") %&gt;' /&gt;
                      &lt;br /&gt;
                      t_foreclosure_date:
                      &lt;asp:TextBox ID="t_foreclosure_dateTextBox" runat="server" 
                          Text='&lt;%# Bind("t_foreclosure_date") %&gt;' /&gt;
                      &lt;br /&gt;
                      t_date:
                      &lt;asp:TextBox ID="t_dateTextBox" runat="server" Text='&lt;%# Bind("t_date") %&gt;' /&gt;
                      &lt;br /&gt;
  
                      &lt;hr /&gt;
  
                      &lt;asp:Repeater ID="repeater1" runat="server" OnPreRender="repeater1_PreRender" OnItemCommand="repeater1_ItemCommand" &gt;  
                      &lt;HeaderTemplate&gt;
                          &lt;table cellpadding="5" cellspacing="5"&gt;
                          &lt;tr style="padding-top: 5px;"&gt;
                              &lt;td colspan="7"&gt;
                                  &lt;asp:Label ID="lblInstructions" runat="server" Text="Add entries here:" /&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr runat="server" id="trHeader" style="font-weight: bold;"&gt;
                              &lt;td&gt;Date&lt;/td&gt;
                              &lt;td&gt;Cost&lt;/td&gt;
                              &lt;td&gt;Type&lt;/td&gt;
                              &lt;td&gt;Comment&lt;/td&gt;
                              &lt;td&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;/td&gt;
                          &lt;/tr&gt;         
                      &lt;/HeaderTemplate&gt; 
                      &lt;ItemTemplate&gt;
                          &lt;tr&gt;
                              &lt;td&gt;&lt;asp:TextBox ID="txtDate" runat="server" Width="55" 
                                   Text='&lt;%#DataBinder.Eval(Container.DataItem, "date", "{0:d}")%&gt;' /&gt; &lt;/td&gt;
                              &lt;td&gt;&lt;asp:TextBox ID="txtAmount" runat="server" Width="55"             
                                   Text='&lt;%#DataBinder.Eval(Container.DataItem, "amount")%&gt;' /&gt; &lt;/td&gt;
  
                              &lt;td&gt;
                                   &lt;asp:DropDownList ID="ddlType" runat="server" DataSourceID="SqlDataSource26" 
                                      DataTextField="tt_type" DataValueField="tt_id"   &gt;&lt;/asp:DropDownList&gt;
                                    &lt;/td&gt;
  
                                   &lt;td&gt;&lt;asp:TextBox ID="txtComment" runat="server" Width="300" 
                                   Text='&lt;%#DataBinder.Eval(Container.DataItem, "comment")%&gt;' /&gt; &lt;/td&gt;
                              &lt;td style="text-align: center;"&gt;
                                  &lt;asp:Button ID="btnMoveUp" runat="server" Width="70" 
                                   Text="Move Up" CommandName="Up" 
                                   CommandArgument='&lt;%# Container.ItemIndex %&gt;' /&gt;
                              &lt;/td&gt;
                              &lt;td style="text-align: center;"&gt;
                                  &lt;asp:Button ID="btnMoveDown" runat="server" Width="90" 
                                   Text="Move Down" CommandName="Down" 
                                   CommandArgument='&lt;%# Container.ItemIndex + 1 %&gt;' /&gt;
                              &lt;/td&gt;
                              &lt;td style="text-align: center;"&gt;
                                  &lt;asp:Button ID="btnRemove" runat="server" Width="70" 
                                   Text="Remove" CommandName="Remove" 
                                   CommandArgument='&lt;%# Container.ItemIndex %&gt;' /&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/ItemTemplate&gt; 
                      &lt;FooterTemplate&gt;
                          &lt;tr style="padding-top: 5px;"&gt;
                             &lt;td colspan="6"&gt;
                                 &lt;asp:Button ID="btnAdd" runat="server" 
                                  Text="Add Row" CommandName="Add" /&gt;
                             &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;/table&gt;
                      &lt;/FooterTemplate&gt;
                  &lt;/asp:Repeater&gt;
  
  
                      &lt;hr /&gt;
                      &lt;asp:CheckBoxList ID="tpTransmittalsDoclist" runat="server" DataSourceID="SqlDataSource13" 
                          DataTextField="document_name" DataValueField="document_id"&gt;
                          &lt;/asp:CheckBoxList&gt;
  
                      &lt;asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                          CommandName="Insert" Text="Insert" /&gt;
                      &amp;nbsp;&lt;asp:LinkButton ID="InsertCancelButton" runat="server" 
                          CausesValidation="False" CommandName="Cancel" Text="Cancel" /&gt;
                  &lt;/InsertItemTemplate&gt;
              &lt;/asp:FormView&gt;
  </code></pre>
  
  <p>On load of the page in code behind i'm inserting dummy values in the first repeater row. I'm using a class cost to set/store values for the repeater row. but I can't figure out how to set the selected value of the drop down list when I databind the repeater. I've tried a few different things but no joy. Any thoughts?</p>
  
  <pre><code>// load up costs in transmittals tab
      if (!Page.IsPostBack)
      {
          Costs mycost = new Costs();
          string date = DateTime.Now.ToShortDateString();
          Costs.Cost cost1 = new Costs.Cost(date, "1.99", 2, "1");
          mycost.Add(cost1);
  
          Repeater r = FormView3.FindControl("repeater1") as Repeater;
          r.DataSource = mycost;
          r.DataBind();
      }
  </code></pre>
  
  <p>Edit: If you are using an integer. You will want to use.</p>
  
  <pre><code> SelectedIndex='&lt;%#DataBinder.Eval(Container.DataItem, "type")%&gt;'
  </code></pre>