我想創建上下拉數據綁定。不能在初始化(動態按鈕列表),或在構造函數...但它給錯誤每次..動態按鈕列表給出錯誤
Control 'ctl00' of type 'Button' must be placed inside a form tag with runat=server
我的下拉已經與內部RUNAT = 「服務器」
這種形式標記:
<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);
}
}
我錯過了什麼?
使用Page.Controls.Add(dynamicButton)。最好的辦法是具有RUNAT =服務器如下 myDiv.Controls.Add(add_img_popup)一個div;
這已經在本文中回答了[鏈接](http://stackoverflow.com/questions/7993808/asp-net-button-object-runat-server-issue) – Girish