【问题标题】:Dropdownlist not fetching values for Second and third list Item下拉列表未获取第二个和第三个列表项的值
【发布时间】:2014-11-06 06:28:10
【问题描述】:

我有两个用于显示类别的下拉菜单,第二个用于显示与所选类别相关的子类别。

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

 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>

【问题讨论】:

  • 请粘贴您的 HTML]
  • @HardikParmar:查看编辑后的问题
  • 首先从 HTML 中删除 因为您已经在 Class 文件中编写了该代码
  • 好的,从 HTML 中删除
  • 你调试过你的代码吗?如果是,那么您在调试时可以看到什么是任何错误即将到来

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


【解决方案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();

【讨论】:

  • 非常感谢 hardik,感谢您的耐心和帮助。脱帽大佬。它是一种魅力..:) :)
  • @NadeemKhan 我的荣幸
猜你喜欢
  • 2014-11-16
  • 1970-01-01
  • 2023-03-14
  • 2013-12-06
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
相关资源
最近更新 更多