the .net framework beta 2 has many changes that will break applications written in beta 1. among these changes is the templates used in data server controls, such as the datagrid and datalist. these are simply syntax changes in how templates are used, not programmatic breaks. in this tutorial you will learn how to use data server control templates. heck, since its a friday i'll also show you how to do datagrid editing at the same time. the downloadable sample code for this article contains files in both visual basic.net and c#.
what good is a template?
templates can be used with the data server controls to provide a custom layout of the data bound to the control. the datagrid provides a basic grid, with one row for each record, and one table column for each field in the row. templates can be used with a templatecolumn to custom format the layout of the column. the datalist provides one row for each record in the data source, and templates are used to format the layout of each row. the repeater control is entirely custom. templates are used to provide the layout for the entire repeater control.
changing a name is a big deal
in beta 1 you would use a datagrid, and set up templates using a templatecolumn. this is the same in beta 2, however there are some slight changes to the syntax. in beta 1 a datagrid using a templatecolumn would look like the code in listing 1.
the majority of the syntax in listing 1 remains the same. the changes are as follows:
the <property> element changes to <columns> the <template> element changes to one of the following: <alternatingitemtemplate> <edititemtemplate> <footertempalte> <headertemplate> <itemtemplate> <separatortemplate> (repeater and datalist) the same datagrid shown in listing 1 can be updated to beta 2 as shown in listing 2.
while listing 2 shows a datagrid using templates, the same rules apply to the other data server controls. for example, listing 3 shows a datalist in beta 2.
the datagrid and datalist control have built in functionality to enable editing. when the control is put into edit mode, usually by a user clicking an edit link, the selected record is rendered differently than the others. if the column containing the data is a boundcolumn, then the data is rendered in a textbox control. using templates you can control how the record is rendered. the edititemtemplate is used to specify how a table cell is rendered when the record is being edited.
in listing 4 is the code for a web form containing one datagrid that has both an <itemtemplate> and an <edititemtemplate>. the itemtemplate is used to control the format of data when it is rendered in its normal state. the edititemtemplate is used to control the formatting of the data when the record is being edited. in this case, the data is rendered in textboxes, although it could be done with dropdownlists, radiobuttons, or any other server control.
the web form shown in listing 4 uses the code behind class testapp.c20010622t0101. the beginning of the code for the web form is shown in listing 5. this is only the code to get the data for the first request of the page.
listing 5 - 20010622t0101.cs
using system; using system.data; using system.data.sqlclient; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.htmlcontrols;
namespace testapp {
public class c20010622t0101 : system.web.ui.page { protected datagrid mygrid;
protected void binddata() { sqlconnection con = new sqlconnection("server=localhost;database=northwind;uid=sa;pwd=;"); sqlcommand cmd = new sqlcommand("select top 10 * from customers", con); con.open(); mygrid.datasource = cmd.executereader(commandbehavior.closeconnection); mygrid.databind(); ((sqldatareader)mygrid.datasource).close(); }
}
}
in listing 5 you use a binddata() method to encapsulate your data access and datagrid binding. this is important for how the editing will work. you want to be able to call the binddata() method at the right time, rather than relying on the page_load() event to handle it for you. in the page_load() event handler you make a call to binddata() only when the web request is not the result of a form post.
you can view the page in the browser now, but the edit functionality will not work. to enable editing there are four steps we have to take:
write an oneditcommand() event handler write and oncancelcommand() event handler write an onupdatecommand() event handler add pointers to the event handlers in the datagrid lets start by writing an oneditcommand() event handler. listing 6 shows the code you need to add to the code behind class for the event handler.
the oneditcommand() event handler does two things, it sets the edititemindex of the datagrid to the value passed in the datagridcommandeventargs, and calls binddata(). the datagridcommandeventargs.item.itemindex is the index of the row where the edit links was clicked. this tells the .net framework that this row should be rendered using the <edititemtemplate>. with the edititemindex set, calling binddata() will rebind the data to the datagrid.
before you test this page, add the following line to the <asp:datagrid> tag:
oneditcommand="mygrid_oneditcommand"
the oncancelcommand() event handler works in the same way as the oneditcommand() event handler. you set the edititemindex to -1 (indicating that no rows are being edited), and call binddata(). this is shown in listing 7.
to tie the datagrid to the oncancelcommand() event handler, add the following code to the <asp:datagrid> tag:
oncancelcommand="mygrid_oncancelcommand"
at this point you can view the web form in the browser, and test the edit link and the cancel link, but the update link is still not functional.
make seven-up(date) yours!
the onupdatecommand() event handler needs to do a few things. it needs to grab the new data from the controls in the <edititemtemplate>, connect to the database, execute an update statement, set the edititemindex to -1, and call binddata().
to make life easier, i have created a new stored procedure in the northwind database, for updating customer information. this is shown in listing 8.
listing 8
create procedure [sp_updatecustomer] @customerid nchar (5), @contactname nvarchar (30), @contacttitle nvarchar (30), @address nvarchar (60), @city nvarchar (15), @region nvarchar (15), @postalcode nvarchar(10) as update customers set contactname = @contactname, contacttitle = @contacttitle, address = @address, city = @city, region = @region, postalcode = @postalcode where customerid = @customerid go
once you have created the stored procedure you can use a parameterized sqlcommand to update the customer data. lets look at the code, then ill explain how it works. listing 9 shows the onupdatecommand() event handler.
in listing 9 you create the onupdatecommand() event handler. in the event handler you create a sqlcommand and add six sqlparameters to the parametercollection. these parameters are mapped directly to the input parameters expected by the stored procedure in listing 8.
to set the value of the parameters you use the datagridedititemeventargs.item.findcontrol() method to find the control you want the value for. since findcontrol() returns an instance of the control class, you have to cast the control as a textbox before you can retrieve the text property. once you have set all the parameters you can open the connection and execute the command. since the stored procedure does not return anything to you, you can use the executenonquery() method. this will execute a command and return the number of rows affected. in this example we are not concerned with that information, so we do not capture it.
after the update is executed, you set the edititemindex of the datagrid to -1, and call binddata().
all you need to do is tie the datagrid to the onupdatecommand() by adding the following code to the <asp:datagrid> tag:
onupdatecommand="mygrid_onupdatecommand"
figure 1 shows the page when you click the edit link on a row.
[ run sample ]
summary
in beta 2 the templates used in the data server controls have changed a bit. nothing too drastic, mostly a syntax change. you no longer use the <property> and <template> tags. instead you use the <columns> and <itemtemplate> (or similar template) tags.