Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net使用C代码插入新的下拉列表_C#_Asp.net_Database_Visual Studio_Drop Down Menu - Fatal编程技术网

C# asp.net使用C代码插入新的下拉列表

C# asp.net使用C代码插入新的下拉列表,c#,asp.net,database,visual-studio,drop-down-menu,C#,Asp.net,Database,Visual Studio,Drop Down Menu,我正在开发一个网站,从数据库检索数据,但我是新的网站开发 我使用GridView、下拉列表和其他工具来显示数据,但有一个页面,网站用户可以在其中设置完成工作需要多少特定的下拉列表 更具体地说,用户将选择产品和每个产品的数量,但我不知道用户需要多少产品。如果用户想输入x个产品数量,我想在不同的行中创建x个drowdown列表,这样他就可以选择x个产品数量及其数量 e、 g。 用户A想要选择2个西红柿、3个土豆和5个西瓜。 他将在文本框中键入3表示产品数量,然后显示3行,每个行有2个下拉列表,用户将

我正在开发一个网站,从数据库检索数据,但我是新的网站开发

我使用GridView、下拉列表和其他工具来显示数据,但有一个页面,网站用户可以在其中设置完成工作需要多少特定的下拉列表

更具体地说,用户将选择产品和每个产品的数量,但我不知道用户需要多少产品。如果用户想输入x个产品数量,我想在不同的行中创建x个drowdown列表,这样他就可以选择x个产品数量及其数量

e、 g。 用户A想要选择2个西红柿、3个土豆和5个西瓜。 他将在文本框中键入3表示产品数量,然后显示3行,每个行有2个下拉列表,用户将选择

西红柿2

土豆3

西瓜5

添加新行->>>这是我想要创建的另一个东西


任何想法???

在代码隐藏中,特别是在页面加载事件期间,您需要确定需要多少下拉列表,然后动态创建它们。谷歌搜索的结果有很多例子,但一个简单的例子如下:

int countNeeded = 15; //or whatever your code tells you is the right amount

for (int x = 0; x < 15; x++)
{
    mainContent.Controls.Add(new DropDownList()); 
    //mainContent would be a control on the page that you want to host your controls.
} 

您可以在代码隐藏中创建所需的控件。当你这样做时应该考虑的事情:

在Page_Init或Page_Load事件中创建控件 当您希望在每个页面加载上显示控件(包括回发)时,重新创建这些控件 如果希望每个下拉列表显示在单独的行中,您可以将它们放在表的单独行中,或者可以将LiteralControl放在它们之间 一些示例代码:

protected void Page_Load(object sender, EventArgs e)
{
    //read how many DropDownList controls are needed
    int ddlCount = txtDropDownList.Text;

    for (int i = 0; i < ddlCount; ++i)
    {
        if (i > 0)
        {
            //add a page break between previous DropDownList and the current one
            this.Controls.Add(LiteralControl("<br />"));
        }

        //add a DropDownList with ID productDdl and an appropriate number
        this.Controls.pageContent.Add(new DropDownList() { ID = String.Format("productDdl{0}", i) });
    }
}

我尚未测试此代码,因此如果您发现任何问题,请告诉我,我将尝试更正这些问题。

mainContent将是什么类型的控件?mainContent可以是面板控件,也可以是任何接受子控件的控件。表也可以工作,您只需要访问单元格。