asp.net methods for dynamically generating controls using the values of text boxes _ Practical Tips

Source: Internet
Author: User

See a netizen, have a forum to ask, dynamic build CheckBox control, enter a "flower" word in the text box, click "Build" button, will generate a checkbox below, its Text property is "flower". Then enter a "Bird", click the button, will generate a second CheckBox control, the Text property is "Bird" ...

The Insus.net solution is simply to save every value you enter in the text box, and then bind the data to a CheckBoxList control .

In detail, first create an object:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
///Summary description for letter
///</summary>
namespace insus.net
{ public class Letter
{
private string _name;
public string Name
{get
{_name;}
set {_name = value;}
}
Public letter ()
{
} public letter
(string name)
{
this._name = name;
}
}

Create an entity that you can develop into operational, such as adding, editing, updating, deleting or acquiring datasets, etc. ...

In this example, Insus.net only two methods for adding and fetching data:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
///Summary description for letterentity
///</summary>
namespace Insus.net
{public
class letterentity
{
private list<letter> _letter = new list<letter> ();
public void Add (letter L)
{
this._letter.add (l);
}
Public ienumerable<letter> letters
{get
{return
this._letter
}
}}

Everything is ready, only the implementation of ASPX, create an ASPX Web page:

In the ASPX.cs code page, you can implement the required functionality:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; u
Sing System.Web.UI;
Using System.Web.UI.WebControls;
Using Insus.net;  Public partial class Default2:System.Web.UI.Page {letterentity le = new letterentity (); protected void Page_Load (object sender, EventArgs e) {if (!
IsPostBack) {data_binding ();} le = Temporaryletters; private void Data_binding () {this. Checkboxlist1.datasource = Le.
Letters; This.
Checkboxlist1.datatextfield = "Name"; This.
Checkboxlist1.databind (); } protected void button1_click (object sender, EventArgs e) {Letter L = new Letter (); if (!string. IsNullOrEmpty (this. TextBox1.Text.Trim ()) L.name = this.
TextBox1.Text.Trim (); Le.
ADD (l);
Temporaryletters = le;
Data_binding ();  Public letterentity Temporaryletters {get {if (session["letterentity"] = = null) return to new letterentity (); else return (letterentity)
session["Letterentity"];
set {session["letterentity"] = value;}} } 

You may find it very complicated because of the problem of involved in storing data. If you put the data directly into the database, you can in the above #6 steps to fill in the value of the database, in the #4 step, to read the database data binding to the CheckBoxList control.

The above is a small set to introduce the ASP.net in the use of text box values to dynamically generate control of the method of knowledge, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.