Asp.net中使用文本框的值动态生成控件的方法

所属分类: 网络编程 / ASP.NET 阅读数: 1252
收藏 0 赞 0 分享

看到一个网友,有论坛上问及,动态的生成checkbox控件,在文本框中输入一个“花”字,点一下“生成”按钮,就会在下面生成一个checkbox,它的text属性是“花”。再输入一个“鸟”,点一下按钮,就会生成第二个checkbox控件,text属性是“鸟”...

Insus.NET的解决方法很简单,就是每次在文本框输入的值都存起来,然后把这些数据绑定至一个CheckBoxList控件上就行了。

详细,先创建一个对象:

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

创建一个实体,这个实体你可以把它开发成可以操作性,如添加,编辑,更新,删除或是获取数据集,等等...

在本例中,Insus.NET只实添加以及获取数据的两个方法:

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

万事俱备,只差ASPX的实现了,创建一个aspx的网页:

在ASPX.cs代码页中,你可以实现所需要的功能:

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

你也许觉得很复杂,因为涉入存储数据的问题。如果你把数据直接存入数据库的话,你可以在上面#6步中把填写的值存入数据库中,在#4步中,去读取数据库的数据绑定给CheckBoxList控件即可。

以上所述是小编给大家介绍的Asp.net中使用文本框的值动态生成控件的方法的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多