下拉列表中的动态按钮列表dataBound给出错误

时间:2015-09-07 01:55:00

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

我想在下拉数据库上创建动态按钮列表。不在Init()或构造函数中......但它每次都会出错..

Control 'ctl00' of type 'Button' must be placed inside a form tag with runat=server

我的下拉列表已经在runat =“server”

这是标记:

<body>
    <form id="form1" runat="server" style="height: 100%">
               <asp:DropDownList runat="server" Width="100%" ID="ddlLecturer" OnSelectedIndexChanged="ddlLecturer_SelectedIndexChanged" AutoPostBack="true" OnDataBound="ddlLecturer_DataBound">
        </asp:DropDownList>

    </form>
</body>

这是我的代码:

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
    //fill dropdown

}

    protected void ddlLecturer_DataBound(object sender, EventArgs e)
    {
//alternate --didnot work out
        //List<Button> buttons = new List<Button>();
        //for (int i = 0; i < 10; i++)
        //{
        //    Button newButton = new Button();
        //    newButton.ID = "btn1";
        //    buttons.Add(newButton);
        //    this.Controls.Add(newButton);

        //}

        CreateDynamicButton();
    }


    private void CreateDynamicButton()
    {
        // Create a Button object 
        Button dynamicButton = new Button();

        // Set Button properties
        dynamicButton.Height = 40;
        dynamicButton.Width = 300;
        dynamicButton.BackColor = Color.Red;
        dynamicButton.ForeColor = Color.Blue;
        //dynamicButton.Location = new Point(20, 150);
        dynamicButton.Text = "I am Dynamic Button";
        //dynamicButton.Name = "DynamicButton";
        //dynamicButton.Font = new Font("Georgia", 16);

        //// Add a Button Click Event handler
        //dynamicButton.Click += new EventHandler(DynamicButton_Click);

        // Add Button to the Form. Placement of the Button
        // will be based on the Location and Size of button
        Controls.Add(dynamicButton);
    }
}

我缺少什么?

1 个答案:

答案 0 :(得分:0)

错误信息是不言自明的。它没有抱怨您的DropDownList,但抱怨动态按钮不在表单内。

您可以使用form1.Controls.Add(dynamicButton)。这样可以确保在<form></form>代码中添加控件。

您会看到另一个选项Page.Controls.Add(dynamicButton),但这不起作用并抛出相同的错误消息,因为控件将添加到页面中但不在<form></form>标记之内。

更清洁的选项是在<form></form>标签中设置某种服务器端占位符。例如<div runat="server" id="placeHolder></div> 现在,您应该可以使用placeHolder.Controls.Add(dynamicButton)

添加控件了

其他信息

至于添加动态控件,您必须知道需要在回发时重新创建控件。 ASP.NET page life cycle也很重要。

这是一个小例子。不是最干净的解决方案;但是你得到一个主意。

基本上根据下拉选项,会创建一个动态按钮。当您单击动态按钮时,它会显示特定于该按钮的消息。

<强>标记

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="page1.aspx.cs" Inherits="test.page1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlLecturer" runat="server" AutoPostBack="True" 
            onselectedindexchanged="ddlLecturer_SelectedIndexChanged">
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem Value="1">Lecturer1</asp:ListItem>
            <asp:ListItem Value="2">Lecturer2</asp:ListItem>
        </asp:DropDownList>
        <asp:Label ID="lblOutput" runat="server" Text=""></asp:Label>

    </div>
    <div id="placeHolder" runat="server">
    </div>
    </form>
</body>
</html>

代码背后

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

namespace test
{
    public partial class page1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["dynamicBtnSession"] != null)
            {
                CreateDynamicButton((string)Session["dynamicBtnSession"]);
            }
        }

        protected void ddlLecturer_SelectedIndexChanged(object sender, EventArgs e)
        {
            placeHolder.Controls.Remove(FindControl("dynamicBtn"));
            lblOutput.Text = string.Empty;
            if (ddlLecturer.SelectedValue != "Select")
            {
                Session["dynamicBtnSession"] = ddlLecturer.SelectedValue;
                CreateDynamicButton(ddlLecturer.SelectedValue);
            }
            else
            {
                Session["dynamicBtnSession"] = null;
            }
        }

        private void CreateDynamicButton(string val)
        {
            Button btn = new Button();
            btn.Height = 40;
            btn.Width = 120;
            btn.BackColor = Color.Gray;
            btn.ForeColor = Color.Black;
            btn.ID = "dynamicBtn";
            btn.Text = "Dynamic Button " + val;
            btn.Click += new EventHandler(btn_Click);
            placeHolder.Controls.Add(btn);
        }

        void btn_Click(object sender, EventArgs e)
        {
            lblOutput.Text = "You clicked " + ((Button)sender).Text;
        }
    }
}