OnButtonClick在特定位置重复指定代码

本文关键字:代码 位置 定位 OnButtonClick | 更新日期: 2023-09-27 18:28:55

标题很好地解释了它,但为了冗余起见。我正在尝试点击一个按钮,在页面上生成代码(当然是定义的代码)。

protected void AddButton_Click(object sender, EventArgs e)
    {
    }

我希望它生成的代码将添加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 />"));

    }

这就是我到目前为止所拥有的,我觉得我已经接近了我所需要的功能(尽管我没有看到任何新的下拉框正在制作中。)。有什么帮助吗?

OnButtonClick在特定位置重复指定代码

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