Вопрос

<asp:DropDownList ID="ddlOptionDependant" runat="server" AutoPostBack="True" 
  DataSourceID="sdsOptionDependant" DataTextField="product_option_name" 
  DataValueField="product_option_id" AppendDataBoundItems="True">
  <asp:ListItem Value="0" Text="None"></asp:ListItem>
</asp:DropDownList>

This DropDownList is built from a Query, I need to dynamically check the product_option_id and when I find a match set that match to the selected= true

Нет правильного решения

Другие советы

You can achieve that using the DataBound event of the DropDownList. Here is an example.. In your aspx page you need to add the OnDataBound property like this:

<asp:DropDownList ID="ddlOptionDependant" runat="server" AutoPostBack="True" 
    DataSourceID="sdsOptionDependant" DataTextField="product_option_name" 
    DataValueField="product_option_id" AppendDataBoundItems="True" 
    OnDataBound="ddlOptionDependant_DataBound">
    <asp:ListItem Value="0" Text="None"></asp:ListItem>
    </asp:DropDownList>
</asp:Content>

And in your code behind add this:

protected void ddlOptionDependant_DataBound(object sender, EventArgs e)
{
    //Get the value of the ID you want to match here:
    int someId = 1;
    foreach(ListItem item in ddlOptionDependant.Items)
            item.Selected = item.Value == someId.ToString();
}

Hope this helps you

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top