ImageVerifierCode 换一换
格式:DOCX , 页数:7 ,大小:74.71KB ,
资源ID:19268344      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/19268344.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(完全用编码实现gridview创建和clounms的添加Word文件下载.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

完全用编码实现gridview创建和clounms的添加Word文件下载.docx

1、SqlDataSource1.UpdateCommand =update employees set firstname=FirstName,lastname=LastName where employeeid=EmployeeIDSqlDataSource1.UpdateParameters.Add(FirstName, );LastNameEmployeeIDif (!IsPostBack)GridView1.DataSourceID = SqlDataSource1GridView1.AutoGenerateColumns = false;GridView1.DataKeyNames =

2、 new string EmployeeID ;GridView1.AllowPaging = true;GridView1.AllowSorting = true;GridView1.PageSize = 5;BoundField bf1 = new BoundField();BoundField bf2 = new BoundField();BoundField bf3 = new BoundField();bf1.HeaderText = Employee IDbf1.DataField = bf1.ReadOnly = true;bf1.SortExpression = bf2.Hea

3、derText = First Namebf2.DataField = FirstNamebf2.SortExpression = bf3.HeaderText = Last Namebf3.DataField = LastNamebf3.SortExpression = CommandField cf = new CommandField();cf.ButtonType = ButtonType.Button;cf.ShowCancelButton = true;cf.ShowEditButton = true;GridView1.Columns.Add(bf1);GridView1.Col

4、umns.Add(bf2);GridView1.Columns.Add(bf3);GridView1.Columns.Add(cf);Part2IntroductionIn Part 1you learned to create Bound Fields and Command Fields dynamically. Often your grid layout is beyond the capabilities of in-built column types and you need to use Template Fields. For example, lets say you ar

5、e creating a product catalog.Certainly normaltabular layout is not suitable here as you want to display each record in a highly customized format. Template fields can come handy in such situations. In this article I am going to illustrate how template fields can be added dynamically. You will learn

6、two techniques of doing so: Using LoadTemplate() method By creating a custom template classAdding Template Fields Using LoadTemplate() MethodIn order to work with this example you need to create a new web site in Visual Studio. Drag and drop a GridView on the default page. Also drag and drop an SqlD

7、ataSource control. We will be setting various properties of these controls via code. In the first method of adding template fields we will be using LoadTemplate() method. The LoadTemplate() method is available to all template controls including the Page. It acceptsvirtual path of a file and loads th

8、e template specified therein. The return value of LoadTemplate() is an object that implements ITemplate interface.In our example we will create the template in a User Control. To do so, add a new Web User Control to the web site and name it as ItemTemplate.ascx. Key in the following markup to it.asp

9、:Label ID=Label1 runat=serverText=%# Eval() %/asp:LabelLabel2Label3Notice above markup carefully. It contains three Label controls. The Text property of each Label is bound with EmployeeID, FirstName and LastName columns of Employees table respectively. The Eval() expression is one way data binding

10、expression of ASP.NET and accepts the name of the column to bind. This user control will act as our ItemTemplate later.Now open the code behind file of the default web form and key in the following code in Page_Load event handler.SqlDataSource1.ConnectionString = SqlDataSource1.SelectCommand = Templ

11、ateField tf1 = new TemplateField();tf1.ItemTemplate=LoadTemplate(ItemTemplate.ascxGridView1.Columns.Add(tf1);The codepoints the ConnectionString property of SQL data source control to Northwind database. The SelectCommand property specifies a SELECT statement that fetches EmployeeID, FirstName and L

12、astName columns of Employees table. Next, it sets DataSourceID property of GridView control to the ID of SQL Data Source control. Also, AutoGenerateColumns property is set to false as we will be adding columns programmatically. Next few lines are important. The code then creates an instance of Templ

13、ateField class. The TemplateField class represents a template column of GridView class. The ItemTemplate property of TemplateField class is then set to the return value of LoadTemplate() method. The LoadTemplate() method accepts a virtual path of the file containing template to be loaded (ItemTempla

14、te.ascx in our case). The TemplateField is then added to the Columns collection of GridView control.Run the web form and your browser should display something as shown below:Notice how the template specified in the user control is applied. Also, notice that header is shown blank because we did not s

15、pecified HeaderTemplate. You can either specify it or turn the header off.Adding Template Field Using Custom Template ClassNow that you know how to use LoadTemplate() method lets move to another possibility. You learnt in the last example that LoadTemplate() method returns an object that implements

16、ITemplate interface. You yourself can create such a class and use it directly in your code instead of using LoadTemplate() method.To begin with add a new class named MyTemplate to App_Code folder. Key in the following code to MyTemplate class.public class MyTemplate : ITemplate private string colnam

17、e; public MyTemplate(string colname) this.colname = colname; public void InstantiateIn(Control container) LiteralControl l = new LiteralControl(); l.DataBinding += new EventHandler(this.OnDataBinding); container.Controls.Add(l); public void OnDataBinding(object sender, EventArgs e) LiteralControl l

18、= (LiteralControl)sender; GridViewRow container = (GridViewRow)l.NamingContainer; l.Text = (DataRowView)container.DataItem)colname.ToString();The code creates a class called MyTemplate that implements ITemplate interface. The ITemplate interface contains a single method - InstantiateIn() - that you

19、must implement. The code then declares a string variable to hold a column name to be displayed. The column name is set in the constructor of the class. Then InstantiateIn() method is implemented. The method receives an object of type Control that represents container or parent control. Inside we cre

20、ate a LiteralControl and attach an event handler (OnDataBinding) to its DataBinding event. This event is raised when the container control calls DataBind() method on itself. The LiteralControl is then added to the Controls collection of the container control.The OnDataBinding() event handler does th

21、e job of data binding the LiteralControl with the required data. It then gets a reference to the row in which the control is being added using NamingContainer property. Finally, Text property of LiteralControl is set to the value of database column as specified in the constructor. This completes our

22、 custom template class.Now add a new web form to the same web site. Drag and drop a GridView and SqlDataSource control as before. Add the following code in the Page_Load event handler of the form.initial catalog= northwind;integratedsecurity=trueMyTemplate t1 = new MyTemplate(tf1.HeaderText = tf1.It

23、emTemplate = t1;TemplateField tf2 = new TemplateField();MyTemplate t2 = new MyTemplate(tf2.HeaderText = tf2.ItemTemplate = t2;GridView1.Columns.Add(tf2);The code sets the properties of SqlDataSource and GridView as before. Notice the code marked in bold letters. The code creates an instance of Templ

24、ateField class as before. This time the ItemTemplate property of TemplateField is set to a new instance of MyTemplate class. The column names - FirstName and LastName - are passed in the constructor. Finally, the template fields are added to the Columns collection of GridView class.The following screen shot shows a sample run of the aboveweb form.Thats it! Happy coding.

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1