0

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

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 />"));



    }

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

4

2 回答 2

0
protected void AddButton_Click(object sender, EventArgs e)
{
    int count = DropDownList.Items.Count + 1;
    DropDownList.Items.Add(count.ToString(),count.ToString());
}

是你想要的吗?

于 2013-08-26T00:49:10.747 回答
0

Is this what you need?

 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();
于 2013-08-26T01:02:31.567 回答