Home>

Seeing a netizen,Asked on the forum,Dynamically generate a checkbox control. Enter a "flower" in the text box and click the "generate" button. A checkbox will be generated below. Its text property is "flower". Enter another "bird" and click the button.Will generate a second checkbox control, the text property is "bird" ...

The solution for insus.net is simple,That is, every time the value entered in the text box is stored,Then bind this data to a checkboxlist controlon the line.

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 {return _name;}
set {_name=value;}
}
public letter ()
{
}
public letter (string name)
{
this._name=name;
}
}
}

Create an entity,This entity you can develop it to be operable,Such as adding, editing, updating, deleting or retrieving datasets,and many more...

In this example,insus.net only implements two methods for adding and getting 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 achieve what you need:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using 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 new letterentity ();
else
return (letterentity) session ["letterentity"];
}
set
{
session ["letterentity"]=value;
}
}
}

You may find it complicatedBecause it involves the problem of storing data.If you store the data directly into the database,You can store the filled value in the database in step #6 above.In step #4, you can read the data from the database and bind it to the checkboxlist control.

  • Previous C # basic knowledge of FileStream
  • Next Security analysis of CI framework