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

首頁 > 開發 > 綜合 > 正文

Creating DataGrid Templated Columns Dynamically -

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

商業源碼熱門下載www.html.org.cn

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 ke


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上杭县| 筠连县| 安陆市| 潜江市| 正阳县| 宣化县| 崇信县| 周口市| 云阳县| 宜良县| 五家渠市| 南岸区| 宁武县| 大余县| 孝昌县| 库车县| 壤塘县| 辽阳县| 吉林省| 若羌县| 延长县| 尉氏县| 渭源县| 盐城市| 广平县| 崇义县| 扶风县| 霞浦县| 乌拉特中旗| 剑川县| 丹凤县| 泾川县| 永州市| 黄梅县| 额济纳旗| 新民市| 宜春市| 泌阳县| 崇仁县| 小金县| 定南县|