下拉列表未获取第二个和第三个列表项的值

时间:2014-11-06 06:28:11

标签: sql asp.net drop-down-menu

我有两个显示类别下拉列表,第二个显示与所选类别相关的子类别。

情景是。类别值来自表格。它正在正确获取。问题是,当我选择第一个类别时,第二个下拉列表会显示确切的子类别。但是当我选择第二类时,它并没有显示与之相关的子类别。 请参阅我已将类别和子类别绑定的代码以获取数据。: -

 SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select CategoryName from dbo.CategoriesForMerchant where ParentId is null", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        ddlCategories.DataSource = dr;
        ddlCategories.Items.Clear();
        ddlCategories.DataTextField = "CategoryName";
        ddlCategories.DataValueField = "CategoryName";
        ddlCategories.DataBind();
        ddlCategories.Items.Insert(0, new ListItem("--Select Category--", "0"));
        conn.Close();
    }
}
protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("Select * from CategoriesForMerchant where ParentId=0" + ddlCategories.SelectedIndex + "", conn);
    SqlDataReader dr = cmd.ExecuteReader();
    ddlSubCategories.DataSource = dr;
    ddlSubCategories.Items.Clear();
    ddlSubCategories.DataTextField = "CategoryName";
    ddlSubCategories.DataValueField = "CategoryName";
    ddlSubCategories.DataBind();
    ddlSubCategories.Items.Insert(0, new ListItem("--Select Sub Category--", "NA"));
    conn.Close();
}

另请参阅SQL表结构: -

CREATE TABLE [dbo].[categoriesformerchant] 
         ( 
                      categoryid   INT IDENTITY(1,1) NOT NULL, 
                      categoryname NVARCHAR(50) NOT NULL, 
                      parentid     INT NULL, 
                      CONSTRAINT [pk_CategoriesForMerchant] PRIMARY KEY CLUSTERED (categoryid ASC)
         )goALTER TABLE [dbo].[categoriesformerchant] WITH CHECK ADD CONSTRAINT [fk_subcategories] FOREIGN KEY(parentid) REFERENCES [dbo].[categoriesformerchant] ([categoryid])goALTER TABLE [dbo].[categoriesformerchant] CHECK CONSTRAINT [fk_subcategories]go

另请参阅代码的HTML: -

 <asp:DropDownList ID="ddlCategories" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged">
            <asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="reqCategory" runat="server" ControlToValidate="ddlCategories" ErrorMessage="Please select the category" InitialValue="0"></asp:RequiredFieldValidator>
        <br />

        <asp:DropDownList ID="ddlSubCategories" runat="server" AutoPostBack="True">
            <asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="reqSubCategory" runat="server" ControlToValidate="ddlSubCategories" ErrorMessage="Please select the sub-category" InitialValue="0"></asp:RequiredFieldValidator>

1 个答案:

答案 0 :(得分:1)

替换此部分

string xyz = "";
if (!String.IsNullOrEmpty(ddlCategories.SelectedValue.ToString()))
{
    xyz = ddlCategories.SelectedValue.ToString();
}
SqlCommand cmd = new SqlCommand("Select * from CategoriesForMerchant where ParentId ='" + xyz + "'", conn);
SqlDataReader dr = cmd.ExecuteReader();
ddlSubCategories.DataSource = dr;
ddlSubCategories.Items.Clear();
ddlSubCategories.DataTextField = "CategoryName";
ddlSubCategories.DataValueField = "CategoryName";
ddlSubCategories.DataBind();
ddlSubCategories.Items.Insert(0, new ListItem("--Select Sub Category--", "0"));
cm.con.Close();

这也是

    SqlCommand cmd = new SqlCommand("Select * from dbo.CategoriesForMerchant where ParentId is null", conn);
    SqlDataReader dr = cmd.ExecuteReader();
    ddlCategories.DataSource = dr;
    ddlCategories.Items.Clear();
    ddlCategories.DataTextField = "CategoryName";
    ddlCategories.DataValueField = "CategoryId";
    ddlCategories.DataBind();
    ddlCategories.Items.Insert(0, new ListItem("--Select Category--", "0"));
    cm.con.Close();