需求说明:在DataGrid中显示手机短信过滤类型(手机号码,业务推广短信,帐户变动短信,节日祝福短信),表示是否向手机号码相应类型的短信。由于被过滤的短信类型可能会动态添加,因此绑定DataGrid时只能通过后台动态绑定,以便增加短信类型时不用修改aspx页面。下图是示例的显示效果,只要在获取数据时增加“节日祝福”列,页面不需要做任何修改,显示就由左图内容变成右图内容。
实现过程:下面用代码说明具体实现。
1.aspx页面部分,代码很简单,在页面中放一个DataGrid就可以了。
2.aspx.cs后台代码部分,这部分的作用就是准备好数据,并绑定到DataGrid中。我们注意到给模板列赋值时实例化了ColumnTemplate类。这个类是我们自己定义的类,也是后台绑定模板列的核心所在,第三部分代码将演示这个类的实现。
3.ColumnTemplate类的实现。继承自ITemplate接口,并实现了接口方法InstantiateIn(),定义子控件和模板所属的 Control 对象。
源代码下载地址: 点击下载完整示例程序 (示例路径:/WebControls/TemplateColumnDemo.aspx)
实现过程:下面用代码说明具体实现。
1.aspx页面部分,代码很简单,在页面中放一个DataGrid就可以了。
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="dgSmsFilters" Style="z-index: 101; left: 264px; position: absolute;
top: 152px" runat="server" Width="480px" Height="200px" AutoGenerateColumns="False"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White"
CellPadding="4">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages">
</PagerStyle>
</asp:DataGrid>
</div>
</form>
<div>
<asp:DataGrid ID="dgSmsFilters" Style="z-index: 101; left: 264px; position: absolute;
top: 152px" runat="server" Width="480px" Height="200px" AutoGenerateColumns="False"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White"
CellPadding="4">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages">
</PagerStyle>
</asp:DataGrid>
</div>
</form>
2.aspx.cs后台代码部分,这部分的作用就是准备好数据,并绑定到DataGrid中。我们注意到给模板列赋值时实例化了ColumnTemplate类。这个类是我们自己定义的类,也是后台绑定模板列的核心所在,第三部分代码将演示这个类的实现。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.BindGrid();
}
}
//准备示例数据
private DataTable GetDates()
{
DataTable dtSmsFilters = new DataTable("dtSmsFilters");
DataColumn dcSmsNum = new DataColumn("手机号",Type.GetType("System.String"));
dtSmsFilters.Columns.Add(dcSmsNum);
//产生短信过滤类型,实际应根据数据库中内容动态生成的,列数可以动态添加
DataColumn dcSmsFilterType1 = new DataColumn("业务推广",Type.GetType("System.Boolean"));
dtSmsFilters.Columns.Add(dcSmsFilterType1);
DataColumn dcSmsFilterType2 = new DataColumn("帐户变动",Type.GetType("System.Boolean"));
dtSmsFilters.Columns.Add(dcSmsFilterType2);
DataColumn dcSmsFilterType3 = new DataColumn("节日祝福", Type.GetType("System.Boolean"));
dtSmsFilters.Columns.Add(dcSmsFilterType3);
//手动添加测试数据
DataRow dr;
for(int i=0;i<10;i++)
{
//赋值,无实际意义,只为显示
dr = dtSmsFilters.NewRow();
dr[0] = "1366666888"+i.ToString();
dr[1] = i%2==0?true:false;
dr[2] = i % 3 == 0 ? true : false;
dr[3] = i % 4 == 0 ? true : false;
dtSmsFilters.Rows.Add(dr);
}
return dtSmsFilters;
}
//绑定DataGrid
private void BindGrid()
{
DataTable dt = this.GetDates();
this.dgSmsFilters.DataSource = dt;
//添加绑定列,绑定手机号
BoundColumn bc = new BoundColumn();
bc.HeaderText = dt.Columns[0].ColumnName;
bc.DataField = dt.Columns[0].ColumnName;
this.dgSmsFilters.Columns.AddAt(0,bc);
//添加模板列,绑定过滤短信类型,模板列的列数是由函数GetDates()生成的DataTable的列数决定的
for(int i=1;i<dt.Columns.Count;i++)
{
TemplateColumn tc = new TemplateColumn();
tc.HeaderText = dt.Columns[i].ColumnName;
tc.ItemTemplate = new ColumnTemplate("ckb"+i.ToString(),dt.Columns[i].ColumnName);
this.dgSmsFilters.Columns.AddAt(i,tc);
}
this.dgSmsFilters.DataBind();
}
{
if (!Page.IsPostBack)
{
this.BindGrid();
}
}
//准备示例数据
private DataTable GetDates()
{
DataTable dtSmsFilters = new DataTable("dtSmsFilters");
DataColumn dcSmsNum = new DataColumn("手机号",Type.GetType("System.String"));
dtSmsFilters.Columns.Add(dcSmsNum);
//产生短信过滤类型,实际应根据数据库中内容动态生成的,列数可以动态添加
DataColumn dcSmsFilterType1 = new DataColumn("业务推广",Type.GetType("System.Boolean"));
dtSmsFilters.Columns.Add(dcSmsFilterType1);
DataColumn dcSmsFilterType2 = new DataColumn("帐户变动",Type.GetType("System.Boolean"));
dtSmsFilters.Columns.Add(dcSmsFilterType2);
DataColumn dcSmsFilterType3 = new DataColumn("节日祝福", Type.GetType("System.Boolean"));
dtSmsFilters.Columns.Add(dcSmsFilterType3);
//手动添加测试数据
DataRow dr;
for(int i=0;i<10;i++)
{
//赋值,无实际意义,只为显示
dr = dtSmsFilters.NewRow();
dr[0] = "1366666888"+i.ToString();
dr[1] = i%2==0?true:false;
dr[2] = i % 3 == 0 ? true : false;
dr[3] = i % 4 == 0 ? true : false;
dtSmsFilters.Rows.Add(dr);
}
return dtSmsFilters;
}
//绑定DataGrid
private void BindGrid()
{
DataTable dt = this.GetDates();
this.dgSmsFilters.DataSource = dt;
//添加绑定列,绑定手机号
BoundColumn bc = new BoundColumn();
bc.HeaderText = dt.Columns[0].ColumnName;
bc.DataField = dt.Columns[0].ColumnName;
this.dgSmsFilters.Columns.AddAt(0,bc);
//添加模板列,绑定过滤短信类型,模板列的列数是由函数GetDates()生成的DataTable的列数决定的
for(int i=1;i<dt.Columns.Count;i++)
{
TemplateColumn tc = new TemplateColumn();
tc.HeaderText = dt.Columns[i].ColumnName;
tc.ItemTemplate = new ColumnTemplate("ckb"+i.ToString(),dt.Columns[i].ColumnName);
this.dgSmsFilters.Columns.AddAt(i,tc);
}
this.dgSmsFilters.DataBind();
}
3.ColumnTemplate类的实现。继承自ITemplate接口,并实现了接口方法InstantiateIn(),定义子控件和模板所属的 Control 对象。
//ColumnTemplate 从ITemplate继承。
//InstantiateIn 定义子控件的属于谁
public class ColumnTemplate:System.Web.UI.ITemplate
{
string id;
string bindField;
public ColumnTemplate(string id,string bindField)
{
this.id = id;
this.bindField = bindField;
}
//Override the ITemplate.InstantiateIn method to ensure
//that the templates are created in a CheckBox control and
//that the CheckBox object's DataBinding event is associated
//with the BindData method.
public void InstantiateIn(Control container)
{
CheckBox ckb = new CheckBox();
ckb.ID = this.id;
ckb.DataBinding += new EventHandler(this.BindData);
container.Controls.Add(ckb);
}
//Create a public method that will handle the
//DataBinding event called in the InstantiateIn method.
public void BindData(object sender, EventArgs e)
{
CheckBox ckb = (CheckBox) sender;
DataGridItem container = (DataGridItem) ckb.NamingContainer;
string isFilter = ((DataRowView) container.DataItem)[bindField].ToString();
ckb.Checked = bool.Parse(isFilter);
}
}
//InstantiateIn 定义子控件的属于谁
public class ColumnTemplate:System.Web.UI.ITemplate
{
string id;
string bindField;
public ColumnTemplate(string id,string bindField)
{
this.id = id;
this.bindField = bindField;
}
//Override the ITemplate.InstantiateIn method to ensure
//that the templates are created in a CheckBox control and
//that the CheckBox object's DataBinding event is associated
//with the BindData method.
public void InstantiateIn(Control container)
{
CheckBox ckb = new CheckBox();
ckb.ID = this.id;
ckb.DataBinding += new EventHandler(this.BindData);
container.Controls.Add(ckb);
}
//Create a public method that will handle the
//DataBinding event called in the InstantiateIn method.
public void BindData(object sender, EventArgs e)
{
CheckBox ckb = (CheckBox) sender;
DataGridItem container = (DataGridItem) ckb.NamingContainer;
string isFilter = ((DataRowView) container.DataItem)[bindField].ToString();
ckb.Checked = bool.Parse(isFilter);
}
}
源代码下载地址: 点击下载完整示例程序 (示例路径:/WebControls/TemplateColumnDemo.aspx)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步