zoukankan      html  css  js  c++  java
  • ASP.NET2.0中给GridView动态添加模板列并自动绑定数据

    在孟子E章上看了一下,但是他的数据是不能自动绑定上去的,需要再RowDataBind事件里面处理。
    改进了一下。可以指定datafield,让其自动绑定上去。
    对于TextBox在InstantiateIn中增加事件:
    tb.DataBinding += new EventHandler(tb_DataBinding);
    然后在事件函数tb_DataBinding里面:
    tb.Text = ((DataRowView)container.DataItem)[dataField].ToString();
    这样GV就可以自动绑定到他的DataSource对应的DataTable中列dataField对应的数据了。

    public class GridViewTemplateTextBox : ITemplate
    {
        
    private DataControlRowType templateType;
        
    private string columnName;
        
    private string dataField;

        
    public GridViewTemplateTextBox(DataControlRowType type, string colname, string datafield)
        
    {
            templateType 
    = type;
            columnName 
    = colname;
            dataField 
    = datafield;
        }


        
    public void InstantiateIn(System.Web.UI.Control container)
        
    {
            
    switch (templateType)
            
    {
                
    case DataControlRowType.Header:
                    Literal lc 
    = new Literal();
                    lc.Text 
    = columnName;
                    container.Controls.Add(lc);
                    
    break;
                
    case DataControlRowType.DataRow:
                    TextBox tb 
    = new TextBox();
                    tb.ID 
    = container.ClientID;
                    tb.DataBinding 
    += new EventHandler(tb_DataBinding);
                    container.Controls.Add(tb);
                    
    break;
                
    default:
                    
    break;
            }

        }

        
    private void tb_DataBinding(object sender, EventArgs e)
        
    {
            TextBox tb 
    = (TextBox)sender;
            tb.Width 
    = Unit.Percentage(100);
            GridViewRow container 
    = (GridViewRow)tb.NamingContainer;
            tb.Text 
    = ((DataRowView)container.DataItem)[dataField].ToString();
            tb.Width 
    = Unit.Pixel(70);
            tb.Style.Add(
    "TEXT-ALIGN""right");

        }

    }

  • 相关阅读:
    dede模板留言提交错误时返回空白页处理方法
    Dedecms列表页通过函数调用当前文档tag的方法
    织梦dedecms如何让内容页显示不同的内容,但是每次更新都不变
    织梦DedeCMS网站服务器搬家详细教程
    让DEDECMS文章内容中链接新窗口打开的方法
    织梦dedecms中英文模版当前位置的修改方法
    织梦dedecms设置不同的搜索页模板教程
    简易版php文件上传_超详细详解
    nodejs 使用mongoose 操作mongodb
    记一个正则
  • 原文地址:https://www.cnblogs.com/lmarsy/p/564634.html
Copyright © 2011-2022 走看看