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

后端 未结 2 590

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

提交回复
热议问题