我有下拉列表,我想用特定条件填充数据 我使用了这段代码,但它运作良好
<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>
答案 0 :(得分:1)
尝试添加
DDlProductFamily.DataSource = DR;
DDlProductFamily.DataBind();
或
if (DR.Read()) {
DDlProductFamily.DataSource = DR;
DDlProductFamily.DataTextField = "Name";
DDlProductFamily.DataValueField = "ProductCategory_Id";
DDlProductFamily.DataBind();
}