- 热门文章:
- · 关于asp.net的代码重用
- · Getting Datagrid Columns Keypress,Keyup, Keydown and other events to Fire(一个很好的东西)
- · runat=server
- · 关于上下文(Context)/2(转MS)
- · 关于上下文(Context)/1(转MS)
- · 在.NET中使用静态变量来代替Application变量
- · ASP.NET窗体对话框的实现
- · 用.NET创建定时缓存
- · 将Session值储存于SQL Server中
- · ASP.NET中的事务处理和异常处理
- · ASP.NET Caching /2
- · 嘿,懒鬼!!其实MSDN阐述得真得不错哎,不知道你从哪来的资料
上一篇:使用Div加速页面的显示(原创) >>
Creating DataGrid Templated Columns Dynamically - Part I(转自Dotnettips)
Creating DataGrid Templated Columns Dynamically - Part I
Introduction
Few months back I wrote article on how to create DataGrid programatically. The article explained how to created a DataGrid with bound columns on the fly. Many readers asked whether we can do similar thing with templated columns. This two part article explains just that. There are actually two ways to create DataGrid templated columns dynamically - using LoadTemplate method and implementing ITemplate interface. In Part - I, we will discuss how to use the first method i.e. using LoadTemplate.What are templates anyway?
ASP.NET list controls like DataGrid, DataList and Repeater allow flexibility in changing look and feel via the use of what is called as @#Templates@#. As the name suggests template is a blue print of look and feel for data displayed in certain areas of the control. For example, HeaderTemplate represents blue print of the header of the control. Similarly, ItemTemplate represents individual item or row of the DataGrid. These sectons are represented by markup like <ItemTemplate> and <HeaderTemplate>.LoadTemplate method
If you see the class hierarchy of Page class it looks like this: System.Object System.Web.UI.Control System.Web.UI.TemplateControl System.Web.UI.PageThe System.Web.UI.TemplateControl class contains a method called LoadTemplate that can be used to dynamially load temlates for templated controls. The signature of LoadTemplate is like this: public ITemplate LoadTemplate(string virtualPath);The method takes virtual path of a user control file i.e. .ascx file.Creating user control file for template markup
You should store markup of the template in .ascx file. Remember that the markup should not contain actual tags like <ItemTemplate>. Typically this file will not be having any other markup. Note that one such file represents markup for one kind of template.Example of using LoadTemplate method
We will now develop an example in which we will create a DataGrid on the fly with a single template column. The template column simply contains a Label control but you can add any other controls as well. Our example will need to follow steps given below:- Create a new ASP.NET web application
- Create a new web form
- Create a user control file (.ascx) for template markup
- Write code to add a DataGrid to the form
- Load the template from the .ascxfile
- Bind the DataGrid with some datasource
User control file for our example
You may add a new web user control (.ascx) to your project. Add following markup to the file: <%@ Control Language="C#" %><asp:label ID="label1" Runat="server" text=@#<%# Databinder.eval(ctype(container,DataGridItem).dataitem,"lastname")%>@#></asp:label>Note that this markup looks same as if we would have used it in design mode. Here, we are binding a label control with the lastname field from the DataMember.Adding DataGrid to the form
Add following code to the Page_Load event of the web form string connstr = @"Integrated Security=SSPI;User ID=sa;Initial Catalog=Northwind;Data Source=MyServer\NetSDK";SqlConnection cnn=new SqlConnection(connstr);SqlDataAdapter da=new SqlDataAdapter("select * from employees", cnn)DataSet ds=new DataSet();da.Fill(ds, "employees");ITemplate temp= Page.LoadTemplate("webusercontrol1.ascx");TemplateColumn tc=new TemplateColumn();tc.HeaderText = "Last Name";tc.ItemTemplate = temp;DataGrid1.Columns.Add(tc);DataGrid1.DataSource = ds;DataGrid1.DataMember = "employees";DataGrid1.DataBind();Most of the code will be familiar to you except code related to ITemplate. We have loaded our template using LoadTemplate method. We then create a TemplateColumn and set its ItemTemplate to it. Even though we have used ItemTemplate, it can be any other type of template also like EditItemTemplate. We then add this new column to the DataGrid. Note that our DataGrid instance DataGrid1 is declared at class level and hence not visible in the code shown above.After running your application you should get a DataGrid with single templated column titled @#Last Name@#.
Summary
In this article we saw how to add a templated column to a DataGrid on the fly using LoadTemplate method. This method can be used if you want to use same DataGrid with say few columns changing at run time. In the next part we will see how to create templated columns via implementing ITemplate interface. Till then keep coding!下一篇:关于asp.net的代码重用 >>
相关文章:
- · ASP.NET Caching /1
- · 绝对酷,如何解决asp.net中javascript脚本的问题(使用服务器控件执行客户端脚本)
- · 最佳ASP.NET编程习惯
- · 一次同时上传多个文件
- · 在datagrid中求和(vb.net,c#)
- · ASP.NET中利用SQLXML WEB服务访问XML数据(转)
- · 在DataGrid中经弹出窗口确认后执行操作(删除)。(附在DataGrid中修改,添加记录)详见代码!!
- · 在datagrid中放入一个DropDownList(忘了这个问题在那里看到的了CSDN or There)
- · 简单的动态加载用户控件的方法
- · 关于如何 确认删除的另外一个办法。
- · 掉掉注意了,如何动态加载用户控件(ascx)
- · 给大家一个新的加密方法,C#的。(国外的,只用于学习,支持中文)
- · TO feixr,DataGrid中的Radiobutton
- · 微软.NET战略和ASP.NET简介(1)
- · 微软.NET战略和ASP.NET简介(3)
- · 微软.NET战略和ASP.NET简介(2)
- · 验证控件介绍--RegularExpressionValidator
- · 验证控件介绍--RangeValidator
- · 验证控件介绍--CompareValidator
- · 验证控件介绍--RequiredFieldValidator
- · 解决分页的例子。使用DataSet绑定到DataList实现的。数据库使用我刚才贴的这个。
- · 配置Config.web
- · WebRequest Class
- · asp.net中使用静态变量
- · 利用HttpRequest登录到某个网站,然后获取网站信息的程序示例 [原创]
- · Creating DataGrid Templated Columns Dynamically - Part II(转自DotNetTips)
- · 用ASP.NET写你自己的代码生成器(2)。
- · 用ASP.NET写你自己的代码生成器(3)。
- · mark新官上任,转贴一个DataGrid(增加删除确认和新增记录功能),道贺:)
- · 一个datagrid 删除确认例子
- · 关于ASP.Net不能启动调试的官方解答
- · 在WEB窗体中如何转换页面和结束程序?
- · 关于ASP.Net写注册表权限问题的官方解决方法
- · 动态生成柱状图
- · 一个SDK里做聊天室的例子(2)
- · 网上下载和上传数据(一) Montaque(原作)
- · 一个SDK里做聊天室的例子(1)
- · 网上下载和上传数据(二) Montaque(原作)
