Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.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# OnButton单击在特定位置重复指定代码_C#_Asp.net_Webforms - Fatal编程技术网

C# OnButton单击在特定位置重复指定代码

C# OnButton单击在特定位置重复指定代码,c#,asp.net,webforms,C#,Asp.net,Webforms,标题很好地解释了这一点,但出于冗余的考虑。我试图点击一个按钮,在页面上生成代码(当然是定义的代码) 我希望它生成的代码将添加2个下拉列表,它们将自动增加它生成的下拉列表ID的1,并且它需要基于SQL查询在数据库中创建一个表 <asp:DropDownList ID="DropDownList" runat="server" AppendDataBoundItems="True" DataSourceID="box_1" DataTextField="box_1" ToolTip="Chec

标题很好地解释了这一点,但出于冗余的考虑。我试图点击一个按钮,在页面上生成代码(当然是定义的代码)

我希望它生成的代码将添加2个下拉列表,它们将自动增加它生成的下拉列表ID的1,并且它需要基于SQL查询在数据库中创建一个表

<asp:DropDownList ID="DropDownList" runat="server" AppendDataBoundItems="True" DataSourceID="box_1" DataTextField="box_1" ToolTip="Checkbox #1" AutoPostBack="True" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged">
            <asp:ListItem Text="--Select One--" Value="" />

        </asp:DropDownList>

编辑

protected void AddButton_Click(object sender, EventArgs e)
    {
        int a = 0;
        string x = dropdowncounter.Value;

        a = Convert.ToInt16(x);
        a = a + 1;
        dropdowncounter.Value = a.ToString();
        DropDownList DropDownList1 = new DropDownList(){
            // Set the DropDownList's Text and ID properties.
            Text = "DropDownList", 
            ID = "DropDownList" + a.ToString(),
            DataSourceID = "Box_1",
            DataTextField = "box_1",
            ToolTip = "Check Box Added!"
        };
        //addoptions1 = DropDownList1;
        //addoptions.Controls.Add(myDropDownList);
        //// Add a spacer in the form of an HTML <br /> element.
        //addoptions.Controls.Add(new LiteralControl("<br />"));



    }
protectedvoid AddButton\u单击(对象发送方,事件参数e)
{
int a=0;
字符串x=下拉计数器.Value;
a=转换为16(x);
a=a+1;
dropdowncounter.Value=a.ToString();
DropDownList DropDownList1=新的DropDownList(){
//设置DropDownList的文本和ID属性。
Text=“DropDownList”,
ID=“DropDownList”+a.ToString(),
DataSourceID=“Box_1”,
DataTextField=“box_1”,
ToolTip=“已添加复选框!”
};
//addoptions1=DropDownList1;
//addoptions.Controls.Add(myDropDownList);
////以HTML
元素的形式添加间隔符。 //addoptions.Controls.Add(新的LiteralControl(“
”); }
这就是我到目前为止所拥有的,我觉得我已经接近我所需要的功能了(尽管我没有看到任何新的下拉框被制作出来)。有什么帮助吗

protected void AddButton_Click(object sender, EventArgs e)
{
    int count = DropDownList.Items.Count + 1;
    DropDownList.Items.Add(count.ToString(),count.ToString());
}
这是你想要的吗?这是你需要的吗

 SqlConnection con = new SqlConnection(s);
 SqlCommand cmd = new SqlCommand("select FieldName from TableName", con);
 SqlDataAdapter sda = new SqlDataAdapter(cmd);
 DataSet ds = new DataSet();
 sda.Fill(ds);
 DropDownList1.DataSource = ds;
 DropDownList1.DataTextField = "FieldName";                          
 DropDownList1.DataValueField = "FieldName";
 DropDownList1.DataBind();

对不起,我不明白你的问题是什么。你想要AddButton_Click()方法中的代码吗?是的,这正是我想要的,我希望它在单击按钮时执行(继续并包含在帖子中)@jakelin你可以尝试使用for循环或button.PerformClick()。我不熟悉for循环调用。它是做什么的?它将如何适应我正在尝试做的事情?你能告诉我更具体的吗??你有一个按钮,在点击事件中,你想执行什么动作?我想这是其中的一部分。我需要弄清楚如何让它在页面上创建更多下拉列表,这些列表根据创建时创建的表填充。@Apezdr这是一个大主题,您可以使用ADO.net、Entity Framework或Linq to SQL。请用谷歌搜索这些关键词,并找到你需要的示例代码。这里有一个。那么这段代码是做什么的呢?我发现它可以在表中查找一个字段,然后在SqlDataAdapter之后,我丢失了它。我也不熟悉这个“数据集”。对它的一些深入了解将帮助我告诉你这是否是我需要的。提前谢谢!!上面的代码与从数据库获取数据并将其显示在dropdownlist中有关。
 SqlConnection con = new SqlConnection(s);
 SqlCommand cmd = new SqlCommand("select FieldName from TableName", con);
 SqlDataAdapter sda = new SqlDataAdapter(cmd);
 DataSet ds = new DataSet();
 sda.Fill(ds);
 DropDownList1.DataSource = ds;
 DropDownList1.DataTextField = "FieldName";                          
 DropDownList1.DataValueField = "FieldName";
 DropDownList1.DataBind();