简体   繁体   English

动态将ASP.NET控件添加到网页

[英]Add ASP.NET controls dynamically to a web page

I am fairly new to web development and my goal is to add a label and textbox control, or drop down dynamically to a page, without re-programming the page and re-publishing the app. 我是Web开发的新手,我的目标是添加标签和文本框控件,或动态下拉至页面,而无需重新编程页面和重新发布应用程序。 As an example, via the admin module I have the ability to add or remove a field to an input page in the application. 例如,通过管理模块,我可以在应用程序的输入页面中添加或删除字段。 For example, this input page would have 4 fields, but a new requirement requires me to add a 5th field, be it a drop down list, label with an associated text box, checkbox, etc. 例如,此输入页面将具有4个字段,但是一项新要求要求我添加第5个字段,它是下拉列表,带有相关文本框的标签,复选框等。

I have googled and found this link: Adding ASP.Net Controls Dynamically , which is a start. 我已经在Google上搜索并找到此链接: 动态添加ASP.Net控件 ,这是一个开始。

It seems from what I found that I would need to generate this through an application function, ex. 从我发现的结果看来,我需要通过一个应用程序函数(例如)来生成它。 CreateTextBoxControl(....) and have either placeholders on the page or some other way. CreateTextBoxControl(....),并在页面上使用占位符或其他方式。

My preference would be MVC using a Restful WEB API with SQL Server stored procedures. 我的首选是使用带有SQL Server存储过程的Restful WEB API的MVC。

I will continue to research this, but any help would be much appreciated. 我将继续对此进行研究,但是任何帮助将不胜感激。

Thank you. 谢谢。

Create the Dynamic Control and Hook It Up 创建动态控件并进行连接

  1. In Solution Explorer, click Show All Files to display a list of the files that are associated with WebForm1.aspx. 在解决方案资源管理器中,单击“显示所有文件”以显示与WebForm1.aspx关联的文件的列表。 Open the WebForm1.aspx.cs file. 打开WebForm1.aspx.cs文件。
  2. Declare the TextBox controls in the .cs (code-behind) file. 在.cs(隐藏代码)文件中声明TextBox控件。 Also, declare a variable for the existing form element in the .aspx file. 另外,为.aspx文件中的现有表单元素声明一个变量。 Update the declarations following the declaration for the WebForm1 class: 在WebForm1类的声明之后更新声明:

    public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; 公共类WebForm1:System.Web.UI.Page {受保护的System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Label Label2; 受保护的System.Web.UI.WebControls.Label Label2; protected System.Web.UI.WebControls.Label Label3; 受保护的System.Web.UI.WebControls.Label Label3; protected System.Web.UI.WebControls.Label Label4; 受保护的System.Web.UI.WebControls.Label Label4; protected System.Web.UI.WebControls.Button Button1; 受保护的System.Web.UI.WebControls.Button Button1;

    // Added by hand for access to the form. //手动添加以访问表格。 protected System.Web.UI.HtmlControls.HtmlForm Form1; 受保护的System.Web.UI.HtmlControls.HtmlForm Form1;

    // Added by hand; //手动添加; will create instance in OnInit. 将在OnInit中创建实例。 protected System.Web.UI.WebControls.TextBox TextBox1; 受保护的System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.TextBox TextBox2; 受保护的System.Web.UI.WebControls.TextBox TextBox2;

The TextBox declarations are entered by hand as they would be if a TextBox were dragged from the toolbox to the .aspx page. 手动输入TextBox声明,就像将TextBox从工具箱拖动到.aspx页时一样。 However, in this case, you create the controls dynamically. 但是,在这种情况下,您可以动态创建控件。

  1. Add code to create the TextBox controls dynamically. 添加代码以动态创建TextBox控件。 The controls are created every time that the page is run. 每次页面运行时都会创建控件。 The best place to do this is in the OnInit function that the WebForm1 class provides. 最佳方法是在WebForm1类提供的OnInit函数中。

Locate the OnInit function. 找到OnInit函数。 Expand the code that is marked with the "Web Form Designer generated code" comment. 展开标有“ Web窗体设计器生成的代码”注释的代码。 Modify the OnInit function so that it looks similar to the following code: 修改OnInit函数,使其看起来类似于以下代码:

override protected void OnInit(EventArgs e)
{
    // Create dynamic controls here.
    // Use "using System.Web.UI.WebControls;"
    TextBox1 = new TextBox();
    TextBox1.ID = "TextBox1";
    TextBox1.Style["Position"] = "Absolute";
    TextBox1.Style["Top"] = "25px";
    TextBox1.Style["Left"] = "100px";
    Form1.Controls.Add(TextBox1);

    TextBox2 = new TextBox();
    TextBox2.ID = "TextBox2";
    TextBox2.Style["Position"] = "Absolute";
    TextBox2.Style["Top"] = "60px";
    TextBox2.Style["Left"] = "100px";
    Form1.Controls.Add(TextBox2);

    this.TextBox1.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
    this.TextBox2.TextChanged += new System.EventHandler(this.TextBox_TextChanged);

    // 
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    // 
    InitializeComponent();
    base.OnInit(e);
}

This code dynamically creates two TextBox controls, sets their IDs and positions, and then binds them to the Form Controls collection. 此代码动态创建两个TextBox控件,设置它们的ID和位置,然后将它们绑定到Form Controls集合。 The code also wires up the TextChanged events of the text boxes to a handler (TextBox_TextChanged). 该代码还将文本框的TextChanged事件连接到处理程序(TextBox_TextChanged)。

Other than setting the TextBox position programmatically and binding it to the Form Controls collection, you can add Web Forms Panel controls to the .aspx page and bind the text boxes to those in the OnInit function, similar to this: 除了以编程方式设置TextBox位置并将其绑定到Form Controls集合之外,还可以将Web Forms Panel控件添加到.aspx页,并将文本框绑定到OnInit函数中的文本框,类似于以下操作:

TextBox1 = new TextBox();
    TextBox1.ID = "TextBox1";
//Form1.Controls.Add(TextBox1);
    Panel1.Controls.Add(TextBox1);

Note When you create dynamic controls on a Web Form, the controls must be created and added to the controls collection either in the OnInit or in the Page_Load events. 注意当您在Web窗体上创建动态控件时,必须在OnInit或Page_Load事件中创建控件并将其添加到控件集合中。 Otherwise, the controls behave unexpectedly. 否则,控件的行为会异常。

  1. Initialize the Text property and styles for the text boxes. 初始化文本框的文本属性和样式。 Modify the existing Page_Load function as follows: 修改现有的Page_Load函数,如下所示:

    private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { // Set the initial properties for the text boxes. 私人无效Page_Load(object sender,System.EventArgs e){if(!IsPostBack){//设置文本框的初始属性。 TextBox1.Text = "TextBox1"; TextBox1.Text =“ TextBox1”; TextBox2.Text = "TextBox2"; TextBox2.Text =“ TextBox2”; } } }}

The initial value of the text boxes (if(!IsPostBack)) is set one time. 文本框的初始值(if(!IsPostBack))设置一次。 This information is maintained by the IPostBackDataHandler interface for the text boxes, making it unecessary to reset the value for subsequent posts. 该信息由IPostBackDataHandler接口用于文本框维护,因此不必为后续帖子重置值。

  1. Provide a handler for the TextChanged events of the TextBox control. 提供用于TextBox控件的TextChanged事件的处理程序。 Add the following code after the Page_Load function body: 在Page_Load函数主体之后添加以下代码:

    private void TextBox_TextChanged(object sender, System.EventArgs e) { TextBox txtBoxSender = (TextBox)sender; 私人无效TextBox_TextChanged(object sender,System.EventArgs e){TextBox txtBoxSender =(TextBox)sender; string strTextBoxID = txtBoxSender.ID; 字符串strTextBoxID = txtBoxSender.ID;

     switch(strTextBoxID) { case "TextBox1": Label3.Text = "TextBox1 text was changed"; break; case "TextBox2": Label4.Text = "TextBox2 text was changed"; break; } 

    } }

This code checks to see which control triggered the event and then reports this to the user by using the approprite Label control. 此代码检查以查看哪个控件触发了事件,然后使用适当的Label控件将此事件报告给用户。 Notice that this function handles the TextChanged event for both of the dynamically-created TextBox controls. 请注意,此函数处理两个动态创建的TextBox控件的TextChanged事件。 By default, AutoPostBack is false for the TextBox controls. 默认情况下,对于TextBox控件,AutoPostBack为false。 Therefore, changing the text in the controls does not cause a PostBack to the server. 因此,更改控件中的文本不会导致回发到服务器。 However, when the Submitbutton is clicked to post the form to the server, the TextChanged events for the TextBox controls are triggered, and this function is called. 但是,当单击Submit按钮将表单发布到服务器时,将触发TextBox控件的TextChanged事件,并调用此函数。

https://support.microsoft.com/en-us/help/317794/how-to-dynamically-create-controls-in-asp-net-by-using-visual-c-net https://support.microsoft.com/zh-CN/help/317794/how-to-dynamically-create-controls-in-asp-net-by-using-visual-c-net

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM