문제

<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