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

后端 未结 2 584

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 r

相关标签:
2条回答
  • 2021-01-25 19:32

    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";
         }          
    }
    
    0 讨论(0)
  • 2021-01-25 19:35

    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")%>'>
     </asp:DropDownList>
    

    Hope, this will fix your problem.

    0 讨论(0)
提交回复
热议问题