2015-09-07 72 views
0

我想創建上下拉數據綁定。不能在初始化(動態按鈕列表),或在構造函數...但它給錯誤每次..動態按鈕列表給出錯誤

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

我錯過了什麼?

+0

使用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

回答

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; 
     } 
    } 
}