动态按钮列表上的下拉框数据绑定给出错误

本文关键字:数据绑定 出错 错误 按钮 列表 动态 | 更新日期: 2023-09-27 18:11:32

我想在下拉菜单上创建动态按钮列表,而不是在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);
    }
}

我错过了什么

动态按钮列表上的下拉框数据绑定给出错误

错误信息是不言自明的。它不是抱怨你的下拉列表,而是抱怨动态按钮不在表单内。

您可以使用form1.Controls.Add(dynamicButton)。这将确保控件被添加到<form></form>标签中。

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

更干净的选择是在<form></form>标签中有某种类型的服务器端占位符。例如<div runat="server" id="placeHolder></div>现在您应该可以使用placeHolder.Controls.Add(dynamicButton)

添加控件了。<<p> 附加信息/strong>

对于添加动态控件,您必须意识到需要在回发时重新创建控件。还有ASP。. NET页面生命周期至关重要。

下面是一个小例子。这不是最干净的解决方案;但这是为了让你得到一个想法。

基本上基于下拉选择,创建一个动态按钮。当你点击动态按钮时它会显示一个特定于该按钮的消息

标记

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