通过查询字符串填充下拉列表

时间:2010-04-15 13:28:04

标签: asp.net

我有下拉列表,我想用特定条件填充数据 我使用了这段代码,但它运作良好

<cs>
protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "ThumbnailViewPage";

    if (Request.QueryString["Category_Id"] != null)
    {
        using (SqlConnection Con = Connection.GetConnection())
        {

            SqlCommand Com = new SqlCommand("GetProducFamilyTP2", Con);
            Com.CommandType = CommandType.StoredProcedure;
            Com.Parameters.Add(Parameter.NewInt("@Category_Id", Request.QueryString["Category_Id"]));
            SqlDataReader DR = Com.ExecuteReader();
            if (DR.Read())
            {
                DDlProductFamily.DataTextField = DR["Name"].ToString();
                DDlProductFamily.DataValueField = DR["ProductCategory_Id"].ToString();
            }
        }

    }
    else if (Request.QueryString["ProductCategory_Id"] != null)
    {
        using (SqlConnection Con = Connection.GetConnection())
        {

            SqlCommand Com = new SqlCommand("GetProducFamilyTP3", Con);
            Com.CommandType = CommandType.StoredProcedure;
            Com.Parameters.Add(Parameter.NewInt("@ProductCategory_Id", Request.QueryString["ProductCategory_Id"]));
            SqlDataReader DR = Com.ExecuteReader();
            if (DR.Read())
            {
                DDlProductFamily.DataTextField = DR["Name"].ToString();
                DDlProductFamily.DataValueField = DR["ProductCategory_Id"].ToString();
            }
        }

    }

}

                                    <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                                        <ContentTemplate>
                                            <asp:DropDownList ID="DDlProductFamily" runat="server" 
                                                ondatabound="DDlProductFamily_DataBound" onload="DDlProductFamily_Load" 
                                                onselectedindexchanged="DDlProductFamily_SelectedIndexChanged">
                                            </asp:DropDownList>
                                        </ContentTemplate>
                                        <Triggers>
                                            <asp:AsyncPostBackTrigger ControlID="DDlProductFamily" EventName="SelectedIndexChanged" />
                                        </Triggers>
                                    </asp:UpdatePanel>

1 个答案:

答案 0 :(得分:1)

尝试添加

DDlProductFamily.DataSource = DR;
DDlProductFamily.DataBind();

if (DR.Read()) {
   DDlProductFamily.DataSource = DR;
   DDlProductFamily.DataTextField = "Name";
   DDlProductFamily.DataValueField = "ProductCategory_Id";
   DDlProductFamily.DataBind();
}