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