简体   繁体   中英

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">    
                <InsertItemTemplate>
                    t_p_id:
                    <asp:TextBox ID="t_p_idTextBox" runat="server" Text='<%# Bind("t_p_id") %>' />
                        <br />

                    t_step:
                    <asp:TextBox ID="t_stepTextBox" runat="server" Text='<%# Bind("t_step") %>' />
                    <br />
                    t_foreclosure_date:
                    <asp:TextBox ID="t_foreclosure_dateTextBox" runat="server" 
                        Text='<%# Bind("t_foreclosure_date") %>' />
                    <br />
                    t_date:
                    <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" >  
                    <HeaderTemplate>
                        <table cellpadding="5" cellspacing="5">
                        <tr style="padding-top: 5px;">
                            <td colspan="7">
                                <asp:Label ID="lblInstructions" runat="server" Text="Add entries here:" />
                            </td>
                        </tr>
                        <tr runat="server" id="trHeader" style="font-weight: bold;">
                            <td>Date</td>
                            <td>Cost</td>
                            <td>Type</td>
                            <td>Comment</td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>         
                    </HeaderTemplate> 
                    <ItemTemplate>
                        <tr>
                            <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>

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

                                 <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>
                            <td style="text-align: center;">
                                <asp:Button ID="btnMoveDown" runat="server" Width="90" 
                                 Text="Move Down" CommandName="Down" 
                                 CommandArgument='<%# Container.ItemIndex + 1 %>' />
                            </td>
                            <td style="text-align: center;">
                                <asp:Button ID="btnRemove" runat="server" Width="70" 
                                 Text="Remove" CommandName="Remove" 
                                 CommandArgument='<%# Container.ItemIndex %>' />
                            </td>
                        </tr>
                    </ItemTemplate> 
                    <FooterTemplate>
                        <tr style="padding-top: 5px;">
                           <td colspan="6">
                               <asp:Button ID="btnAdd" runat="server" 
                                Text="Add Row" CommandName="Add" />
                           </td>
                        </tr>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>


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

                    <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" />
                </InsertItemTemplate>
            </asp:FormView>

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");
        mycost.Add(cost1);

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

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

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

you can bind the SelectedValue property of Dropdown in your markup page ie

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

Hope, this will fix your problem.

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

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

... then you need to work with OnItemDataBound

Basically:

On repeater tag, add this attribute:

OnItemDataBound="FormatRepeaterRow"

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";
     }          
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM