<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