国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Creating DataGrid Templated Columns Dynamically -

2024-07-21 02:16:54
字體:
來源:轉載
供稿:網友

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.page
the 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
out of above steps first two are common to any web application and we will not discuss them here. so, let us start by creating a user control file for our template.

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!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澄迈县| 彭山县| 墨江| 东兴市| 天全县| 维西| 新蔡县| 乳源| 龙井市| 扎赉特旗| 宜宾县| 株洲市| 云梦县| 东明县| 嵩明县| 邹城市| 漠河县| 射洪县| 襄城县| 门头沟区| 千阳县| 洛宁县| 大方县| 宁海县| 惠水县| 合水县| 呼和浩特市| 喀喇| 诸暨市| 兴城市| 安泽县| 将乐县| 龙泉市| 漳浦县| 同江市| 长武县| 南陵县| 若尔盖县| 柳林县| 姜堰市| 巴南区|