【问题标题】:Picking category from one dropdownlist to show sub categories in another dropdownlist从一个下拉列表中选择类别以在另一个下拉列表中显示子类别
【发布时间】:2012-09-14 05:49:54
【问题描述】:

我正在尝试通过 C# 制作另一个下拉列表控件。 我的计划是,第一个 DropDownList 显示诸如“大小”“性别”“模型”之类的类别,当您选择其中一个时,将出现一个新的 DropDownList,其中包含先前选择的类别的新子类别。

例如,如果我选择“Size”,则会出现一个新的 DropDownList,并且可以从多种尺寸中进行选择。

我在测试时遇到错误,听起来像这样:确保您的方法参数格式正确。

我的代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ContactTableAdapters;

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ddlTwo_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlTwo.Items.Clear();
        if (ddlThree.SelectedValue != "0")
        {
            Contact.CategoriesDataTable table;
            ddlTwo.AppendDataBoundItems = true;
            ddlTwo.Items.Add(new ListItem("Choose", "0"));
            CategoriesTableAdapter subM = new CategoriesTableAdapter();
            int CategoryID = Convert.ToInt32(ddlThree.SelectedValue); //This is where I get the error
            table = subM.GetCategoryByCategoryID(CategoryID);
            foreach (Contact.CategoriesRow row in table)
            {


                string text = row.Category;
                string value = row.CategoryID.ToString();
                ddlTwo.Items.Add(new ListItem(text, value));

            }
        }
    }
}

谁能告诉我,我可能做错了什么?

【问题讨论】:

  • 请调试并查看 ddlThree.SelectedValue 实际上是什么。我的猜测是空的......
  • “输入字符串的格式不正确”它说。
  • 那是异常信息的值,对吗?我需要知道 ddlThree.SelectedValue) 的值是什么
  • 它可能是一个空字符串。因此它无法将其解析为 int。你确定 ddlThree.SelectedValue 有项目,如果有,选择一个?我怀疑它
  • ddlTwo 的值应该是“Size”“Gender”“Model”,当您选择其中一个时,应该在 ddlTwo 中选择的值将为 ddlThree 打开,其中应该包含值 Sizes ,男/女”或“模特”——你说的是什么?

标签: c# asp.net drop-down-menu code-behind


【解决方案1】:

您返回的值似乎无法转换为整数。

你可以做的是创建一个可以引用你的值的枚举:

int CategoryID = Enum.GetValues(typeof(Category), (ddlThree.SelectedValue));

你的枚举看起来像这样:

enum Category
{
Size= 1,
Gender = 2,
Value = 3
}

【讨论】:

  • 我完全不知道枚举是什么!
  • Kasper,枚举(或简称枚举)是一种可区分的对象类型,允许您存储专门的常量值。在您的情况下,我们使用一种“类别”类型,它同时具有自定义字符串和整数值。我们可以使用 Enum.GetValues 的任一本机枚举函数来返回整数,或使用 Enum.GetNames 来返回字符串,而不是硬编码转换(例如 Convert.ToInt32)。希望这能解释这种类型的使用。
【解决方案2】:

如果你实际上是在响应 ddlTwo 上的事件,那么使用这个:

protected void ddlTwo_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlThree.Items.Clear();
        if (ddlTwo.SelectedValue != "0")
        {
            Contact.CategoriesDataTable table;
            ddlThree.AppendDataBoundItems = true;
            ddlThree.Items.Add(new ListItem("Choose", "0"));
            CategoriesTableAdapter subM = new CategoriesTableAdapter();
            int CategoryID = Convert.ToInt32(ddlTwo.SelectedValue); //This is where I get the error
            table = subM.GetCategoryByCategoryID(CategoryID);
            foreach (Contact.CategoriesRow row in table)
            {


                string text = row.Category;
                string value = row.CategoryID.ToString();
                ddlThree.Items.Add(new ListItem(text, value));

            }
        }
    }

【讨论】:

  • 这不是 100% 的,因为当我在 ddlTwo 中选择“性别”“尺寸”或“型号”时,ddlThree 中会出现同样的情况?
  • 然后 subM.GetCategoryByCategoryID(CategoryID);可能不是您要用于填充下拉列表的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多