佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Asp.net中使用文本框的值動態(tài)生成控件的方法

Asp.net中使用文本框的值動態(tài)生成控件的方法

熱門標簽:解決方案 地方門戶網(wǎng)站 電子圍欄 蘋果 硅谷的囚徒呼叫中心 服務(wù)器配置 智能手機 呼叫中心

看到一個網(wǎng)友,有論壇上問及,動態(tài)的生成checkbox控件,在文本框中輸入一個“花”字,點一下“生成”按鈕,就會在下面生成一個checkbox,它的text屬性是“花”。再輸入一個“鳥”,點一下按鈕,就會生成第二個checkbox控件,text屬性是“鳥”...

Insus.NET的解決方法很簡單,就是每次在文本框輸入的值都存起來,然后把這些數(shù)據(jù)綁定至一個CheckBoxList控件上就行了。

詳細,先創(chuàng)建一個對象:

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

創(chuàng)建一個實體,這個實體你可以把它開發(fā)成可以操作性,如添加,編輯,更新,刪除或是獲取數(shù)據(jù)集,等等...

在本例中,Insus.NET只實添加以及獲取數(shù)據(jù)的兩個方法:

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 ListLetter> _Letter = new ListLetter>();
public void Add(Letter l)
{
this._Letter.Add(l);
}
public IEnumerableLetter> Letters
{
get {
return this._Letter;
}
}
}
} 

萬事俱備,只差A(yù)SPX的實現(xiàn)了,創(chuàng)建一個aspx的網(wǎng)頁:

在ASPX.cs代碼頁中,你可以實現(xiàn)所需要的功能:

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

你也許覺得很復(fù)雜,因為涉入存儲數(shù)據(jù)的問題。如果你把數(shù)據(jù)直接存入數(shù)據(jù)庫的話,你可以在上面#6步中把填寫的值存入數(shù)據(jù)庫中,在#4步中,去讀取數(shù)據(jù)庫的數(shù)據(jù)綁定給CheckBoxList控件即可。

以上所述是小編給大家介紹的Asp.net中使用文本框的值動態(tài)生成控件的方法的相關(guān)知識,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

您可能感興趣的文章:
  • ASP.NET 廣告控件AdRotator的使用方法與實例
  • ASP.NET筆記之廣告控件的使用方法
  • ASP.NET 網(wǎng)站開發(fā)中常用到的廣告效果代碼
  • 獲取asp.net服務(wù)器控件的客戶端ID和Name的實現(xiàn)方法
  • asp.net控件DataList分頁用法
  • ASP.NET項目開發(fā)中日期控件DatePicker如何使用
  • ASP.NET中 ObjectDataSource控件的DataObjectTypeName屬性
  • ASP.NET的廣告控件AdRotator用法分析

標簽:泰安 喀什 德宏 佳木斯 房產(chǎn) 玉林 呂梁

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Asp.net中使用文本框的值動態(tài)生成控件的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    晋中市| 渝北区| 永川市| 射洪县| 平阴县| 榆中县| 嘉义县| 化州市| 邛崃市| 彰武县| 西和县| 儋州市| 阳高县| 加查县| 宁阳县| 新竹市| 宜州市| 延川县| 通海县| 中阳县| 永兴县| 师宗县| 鞍山市| 阿坝县| 定陶县| 双牌县| 孟津县| 莱西市| 敖汉旗| 琼结县| 洪江市| 呼伦贝尔市| 崇左市| 出国| 宣威市| 遵化市| 阿图什市| 织金县| 高尔夫| 柘城县| 达州市|