2014-11-06 105 views
0

我有两个下拉菜单来显示类别,其次显示与所选类别相关的子类别。下拉列表没有获取第二个和第三个列表的值

情景是。类别值来自表格,并且正确读取。问题是,当我选择第一个类别时,第二个下拉菜单显示了确切的子类别。但是,当我选择第二个类别时,它不会显示与其相关的子类别。 请看到我已经绑定的类别和子类别为同一获取data.:-

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> 
+0

请粘贴HTML] – 2014-11-06 06:36:31

+0

@HardikParmar:看编辑的问题 – BNN 2014-11-06 06:38:09

+0

首先删除的 from HTML Bcoz您已经在Class文件中写入了该代码 – 2014-11-06 06:39:30

回答

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(); 

n这个也

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(); 
+0

非常感谢您的耐心帮助我。盖过花花公子。它的魅力工作.. :) :) – BNN 2014-11-06 08:18:28

+0

@NadeemKhan我的荣幸 – 2014-11-06 08:19:18

相关问题