Setting selected value on a dropdownlist from codebehind in a repeater in a formview

So here’s the situation.
I have a dropdownlist in a repeater that is inside a formview.

And the really special part is that the repeater is being used to add multiple rows of data dynamically.

What I’ve been trying to do and failing is to set the selected value of the drop down.

<asp:FormView ID="FormView3" runat="server" DataKeyNames="t_id" 
                DataSourceID="SqlDataSource25" DefaultMode="Insert">    
                    <asp:TextBox ID="t_p_idTextBox" runat="server" Text='<%# Bind("t_p_id") %>' />
                        <br />

                    <asp:TextBox ID="t_stepTextBox" runat="server" Text='<%# Bind("t_step") %>' />
                    <br />
                    <asp:TextBox ID="t_foreclosure_dateTextBox" runat="server" 
                        Text='<%# Bind("t_foreclosure_date") %>' />
                    <br />
                    <asp:TextBox ID="t_dateTextBox" runat="server" Text='<%# Bind("t_date") %>' />
                    <br />

                    <hr />

                    <asp:Repeater ID="repeater1" runat="server" OnPreRender="repeater1_PreRender" OnItemCommand="repeater1_ItemCommand" >  
                        <table cellpadding="5" cellspacing="5">
                        <tr style="padding-top: 5px;">
                            <td colspan="7">
                                <asp:Label ID="lblInstructions" runat="server" Text="Add entries here:" />
                        <tr runat="server" id="trHeader" style="font-weight: bold;">
                            <td><asp:TextBox ID="txtDate" runat="server" Width="55" 
                                 Text='<%#DataBinder.Eval(Container.DataItem, "date", "{0:d}")%>' /> </td>
                            <td><asp:TextBox ID="txtAmount" runat="server" Width="55"             
                                 Text='<%#DataBinder.Eval(Container.DataItem, "amount")%>' /> </td>

                                 <asp:DropDownList ID="ddlType" runat="server" DataSourceID="SqlDataSource26" 
                                    DataTextField="tt_type" DataValueField="tt_id"   ></asp:DropDownList>

                                 <td><asp:TextBox ID="txtComment" runat="server" Width="300" 
                                 Text='<%#DataBinder.Eval(Container.DataItem, "comment")%>' /> </td>
                            <td style="text-align: center;">
                                <asp:Button ID="btnMoveUp" runat="server" Width="70" 
                                 Text="Move Up" CommandName="Up" 
                                 CommandArgument='<%# Container.ItemIndex %>' />
                            <td style="text-align: center;">
                                <asp:Button ID="btnMoveDown" runat="server" Width="90" 
                                 Text="Move Down" CommandName="Down" 
                                 CommandArgument='<%# Container.ItemIndex + 1 %>' />
                            <td style="text-align: center;">
                                <asp:Button ID="btnRemove" runat="server" Width="70" 
                                 Text="Remove" CommandName="Remove" 
                                 CommandArgument='<%# Container.ItemIndex %>' />
                        <tr style="padding-top: 5px;">
                           <td colspan="6">
                               <asp:Button ID="btnAdd" runat="server" 
                                Text="Add Row" CommandName="Add" />

                    <hr />
                    <asp:CheckBoxList ID="tpTransmittalsDoclist" runat="server" DataSourceID="SqlDataSource13" 
                        DataTextField="document_name" DataValueField="document_id">

                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                        CommandName="Insert" Text="Insert" />
                    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                        CausesValidation="False" CommandName="Cancel" Text="Cancel" />

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?

// 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");

        Repeater r = FormView3.FindControl("repeater1") as Repeater;
        r.DataSource = mycost;

Edit: If you are using an integer. You will want to use.

 SelectedIndex='<%#DataBinder.Eval(Container.DataItem, "type")%>'

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

you can bind the SelectedValue property of Dropdown in your markup page i.e

<asp:DropDownList ID="ddlType" runat="server" DataSourceID="SqlDataSource26" 
   DataTextField="tt_type" DataValueField="tt_id" 
   SelectedValue='<%# Eval("ColumnName")%>'>

Hope, this will fix your problem.

Solution 2

If the value cant be set just int the drodownlist tag like…

SelectedValue='<%# Eval("ColumnWithValue")%>'

… then you need to work with OnItemDataBound


On repeater tag, add this attribute:


On page code-behind:

protected void FormatRepeaterRow(Object sender, RepeaterItemEventArgs e)
     if( (e.Item.ItemType == ListItemType.Item) || ( e.Item.ItemType == ListItemType.AlternatingItem)) 
      ((DropDownList)e.Item.FindControl("ddlType")).SelectedValue = "a_value";

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply