在页面中,我们可以实现以下结构:
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>
<INPUT id="chkAll" class="CheckBox" onclick="SelectAllCheckBox(this)" type="checkbox" name="chkAll">
</HeaderTemplate>
<ItemTemplate>
<INPUT id="chkItem" class="CheckBox" type="checkbox" name="chkItem" runat="server">
</ItemTemplate>
</asp:TemplateColumn>
这可以作为一个dataGrid的一列,现在有个问题是:如果我想在页面的onload事件中,用代码创建这样的列,请问大家,如何处理?
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>
<INPUT id="chkAll" class="CheckBox" onclick="SelectAllCheckBox(this)" type="checkbox" name="chkAll">
</HeaderTemplate>
<ItemTemplate>
<INPUT id="chkItem" class="CheckBox" type="checkbox" name="chkItem" runat="server">
</ItemTemplate>
</asp:TemplateColumn>
这可以作为一个dataGrid的一列,现在有个问题是:如果我想在页面的onload事件中,用代码创建这样的列,请问大家,如何处理?
解决方案 »
- 找.Net MVC高手解决执行2个路由的问题
- 用户点击搜索按钮后,如何把这次选择的搜索条件保存到数据库中,当用户再次登陆时,绑定上次保存在数据库中的搜索条件到页面上。
- js写的三级联动菜单,如何让选择项显示客户数据中的内容!
- 关于javascript中<%=%>的用法的疑问
- 分散完了,但是问题没有完,请大家高抬贵手,帮帮忙,问题很简单的!!!!
- 项目准备用NHibernate开发,用过的进来看看下面问题,能不能解决,在线等!
- 用ASP.NET怎么在新页面中打开链接?
- 网站首页Flash新闻图片!!!!!!
- 谁能解决以下水晶报表的问题,1000分相送!!我相信这些问题好多人遇到,请帮我顶!!!
- 关于更改图片链接的问题?
- url问题
- 请高手帮忙:如何捕获关闭IE事件
<asp:TemplateColumn HeaderText="字段1">
<HeaderStyle HorizontalAlign="Center" Width="120px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:TextBox id="txtPC" Text='<%# DataBinder.Eval(Container.DataItem,"user_name")%>' runat="server" Width="120px">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
这种结构,我如何用代码动态的创建?我写到:
TC = new TemplateColumn ();
TC.HeaderText = Cols[i].ToString ();
TC.HeaderStyle .HorizontalAlign = System.Web .UI .WebControls .HorizontalAlign .Center ;
TC.HeaderStyle .Width =100;
TC.ItemStyle .HorizontalAlign = System.Web .UI .WebControls .HorizontalAlign .Center ;
但是:<ItemTemplate>
<asp:TextBox id="txtPC" Text='<%# DataBinder.Eval(Container.DataItem,"user_name")%>' runat="server" Width="120px">
</asp:TextBox>
</ItemTemplate>
这一部分就不知道如何用代码实现了,请大家指点!
谢谢hanchi8008(寒池) ,现在还有一个问题要大家请教,如下:
<asp:TemplateColumn HeaderText="字段1">
<HeaderStyle HorizontalAlign="Center" Width="120px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:TextBox id="txtPC" Text='<%# DataBinder.Eval(Container.DataItem,"user_name")%>' runat="server" Width="120px">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
这种结构,我如何用代码动态的创建?我写到:
System.Web.UI .WebControls .TemplateColumn TC = new TemplateColumn ();
TC.HeaderText = "姓名"
TC.HeaderStyle .HorizontalAlign = System.Web .UI .WebControls .HorizontalAlign .Center ;
TC.HeaderStyle .Width =100;
TC.ItemStyle .HorizontalAlign = System.Web .UI .WebControls .HorizontalAlign .Center ;
但是:<ItemTemplate>
<asp:TextBox id="txtPC" Text='<%# DataBinder.Eval(Container.DataItem,"user_name")%>' runat="server" Width="120px">
</asp:TextBox>
</ItemTemplate>
这一部分就不知道如何用代码实现了,请大家指点!
//在CtemplateColumn中实现ITemplate接口
public class CTemplateColumn:ITemplate
{ private string colname;
private int controlWidth; public CTemplateColumn(string cname,int width)
{ colname=cname;
controlWidth = width; } //为了使用接口必须实现的方法 public void InstantiateIn(Control container)
{ System.Web .UI .WebControls .TextBox l = new TextBox ();
l.Width = controlWidth;
l.ID = colname;
l.DataBinding += new EventHandler(this.OnDataBinding); container.Controls.Add(l); } public void OnDataBinding(object sender, EventArgs e)
{ System.Web .UI .WebControls .TextBox l =(System.Web.UI .WebControls .TextBox ) sender; DataGridItem container = (DataGridItem) l.NamingContainer; l.Text = ((DataRowView)container.DataItem)[colname].ToString(); } }实现这个接口后,只需要
TC.ItemTemplate = new CTemplateColumn(DataCols[i].ToString (),50); 就可以了再向大家请教一个问题:
我动态创建的列中的数据,如果从dataGrid中读出来
我用:this.dgMast.columns.count 得到的列并不包含动态创建的列
所以用 this.dgMast .Items [i].Cells [j].Text 读不出来,请教大家有没有好的方法。