Pismatically generate web control in ASP.NET

xiaoxiao2021-03-06  147

Pismatically generate web control in ASP.NET

In most cases, when we make the ASP.NET page, we use static forms to add a web server control to the page, which can meet most of the needs. In this article, we mainly discuss how to dynamically generate controls in the web page and added to the specified location, then set the relevant properties of the control, trigger relevant events.

In this program, we intend to dynamically generate three controls, a table, a text box, a label on the page. When the text box lost the focus, the label will display the content in the text box; when the program is running, there is a prompt text in the text box ("press the Tab button after the input"), when the mouse passes through the text box, automatically clear Tip text. The above is the functional introduction of this program.

The following explanation details:

1. Create a new ASPX page, the name is arbitrary.

2, switch to the HTML view, add a list of two lines, because .NET paintings are extremely inconvenient, so I have painted the code in FrontPage and paste the code.

3. Put a PlaceHolder control in the first line and second grid of the table and use the default name. This control can be found in the toolbox's Web Forms tab, which allows you to place empty container controls to page and then dynamically add, remove, or sequentially via sub-elements during runtime. This control only presents its child elements.

4, not much, all source code is posted, I made more detailed notes: use system;

Using system.collections;

Using system.componentmodel;

Using system.data;

Using system.drawing;

Using system.Web;

Using system.Web.SessionState;

Using system.Web.ui;

Using system.Web.ui.webcontrols;

Using system.Web.ui.htmlcontrols;

Namespace TEACHSHOW.TEST

{

///

/// WebForm1 summary description.

///

Public class Webform1: System.Web.ui.page

{

protected system.Web.ui.WebControls.PlaceHolder PlaceHolder1;

Protected system.Web.ui.webcontrols.placeholder placeholder2;

PRIVATE LABEL LABEL = New Label ();

Private textbox textbox = new textbox ();

Private Void Page_Load (Object Sender, System.EventArgs E)

{

/ / Place the user code here to initialize the page

Table tb = new table (); // Create a form

Tb.BorderWidth = Unit.Parse ("1");

Tb.Width = Unit.Parse ("100%");

For (int J = 0; j <10; j )

{

TableRow Tr = new tableerow (); // Create a line

TableCell Cell1 = new TableCell (); // Create a single element, that is, the first column

Cell1.text = j.tostring (); // Setting the text in the cell

Tr.cells.Add (Cell1); // Add to the line

TableCell Cell2 = new TableCell (); // Create a second column Cell2.Text = (j * j) .tostring ();

Tr.cells.Add (Cell2);

TB.Rows.Add (TR); // Add to the table

}

This.PlaceHolder2.controls.add (tb);

/

TEXTBOX.TEXT = "Please press Tab after entering the end"; // Tips inside the text box during the program

TextBox.id = "t"; // give a name to the text

Textbox.width = 200; // Set the width

TEXTBOX.HEIGHT = 20; // Set height

TextBox.autopostback = true; // Response event condition

TextBox.TextChanged = New EventHandler (TextBox_textChanged); // Add event, in .NET, press Tab Automatically generate

TextBox.attributes.add ("onMouseOver", "T.Value = '"); // Add a JavaScript event to the text box, automatically clears the prompt content in the text box when the mouse is moved from the text box

this.PlaceHolder1.controls.add (TextBox); // Add

This.PlaceHolder1.controls.add (label); // Add the label to the PlaceHolder

}

#Region web form designer generated code

Override protected void oninit (Eventargs E)

{

//

// Codegen: This call is necessary for the ASP.NET Web Form Designer.

//

InitializationComponent ();

Base.onit (E);

}

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

This.Load = New System.EventHandler (this.page_load);

}

#ndregion

Private void textbox_textchanged (Object Sender, Eventargs E)

{

This.Label.Text = TextBox.text; // Event response method. Set the label to the same content as the text box when the text box lost the focus.

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-127589.html

New Post(0)