上一例中boundcolumn 控件不是唯一可以在 datagrid 的 columns 集合中设置的控件。还可以指定 templatecolumn,它使您可以完全控制列的内容。模板的内容可以是任意的;在 datagrid 的列中可以呈现任何内容,包括服务器控件。下面的示例说明如何使用 templatecolumn 控件将“state”列呈现为下拉列表并将“contract”列呈现为复选框 htmlcontrol。asp.net 数据绑定语法用于输出模板中的数据字段值。注意,有一些棘手的逻辑使下拉列表和复选框反射行中的数据状态。
<%@ import namespace=”system.data” %>
<%@ import namespace=”system.data.sqlclient” %>
<html>
<script language=”c#” runat=”server”>
sqlconnection myconnection;
public hashtable stateindex;
protected void page_load(object src, eventargs e)
{
myconnection = new sqlconnection(“user id=sa;password=;initial catalog=pubs;data source=jeff”);
if (!ispostback)
bindgrid();
stateindex = new hashtable();
stateindex[“ca”] = 0;
stateindex[“in”] = 1;
stateindex[“ks”] = 2;
stateindex[“md”] = 3;
stateindex[“mi”] = 4;
stateindex[“or”] = 5;
stateindex[“tn”] = 6;
stateindex[“ut”] = 7;
}
public int getstateindex(string statename)
{
if (stateindex[statename] != null)
return (int)stateindex[statename];
else
return 0;
}
public void mydatagrid_edit(object sender, datagridcommandeventargs e)
{
mydatagrid.edititemindex = (int)e.item.itemindex;
bindgrid();
}
public void mydatagrid_cancel(object sender, datagridcommandeventargs e)
{
mydatagrid.edititemindex = -1;
bindgrid();
}
public void mydatagrid_update(object sender, datagridcommandeventargs e)
{
string updatecmd = “update authors set au_id = @id, au_lname = @lname, au_fname = @fname, phone = @phone, “
+ “address = @address, city = @city, state = @state, zip = @zip, contract = @contract where au_id = @id”;
sqlcommand mycommand = new sqlcommand(updatecmd, myconnection);
mycommand.parameters.add(new sqlparameter(“@id”, sqldbtype.nvarchar, 11));
mycommand.parameters.add(new sqlparameter(“@lname”, sqldbtype.nvarchar, 40));
mycommand.parameters.add(new sqlparameter(“@fname”, sqldbtype.nvarchar, 20));
mycommand.parameters.add(new sqlparameter(“@phone”, sqldbtype.nchar, 12));
mycommand.parameters.add(new sqlparameter(“@address”, sqldbtype.nvarchar, 40));
mycommand.parameters.add(new sqlparameter(“@city”, sqldbtype.nvarchar, 20));
mycommand.parameters.add(new sqlparameter(“@state”, sqldbtype.nchar, 2));
mycommand.parameters.add(new sqlparameter(“@zip”, sqldbtype.nchar, 5));
mycommand.parameters.add(new sqlparameter(“@contract”, sqldbtype.nvarchar,1));
mycommand.parameters[“@id”].value = mydatagrid.datakeys[(int)e.item.itemindex];
string[] cols = {“lname”,”fname”,”phone”,”address”,”city”,”zip”};
for (int i=0; i<6; i++)
{
string colvalue = ((textbox)e.item.findcontrol(“edit_” + cols[i])).text;
// 检查在所需字段中是否有空值
if (i<3 && colvalue == “”)
{
message.innerhtml = “错误:“姓名”或“电话”不允许使用空值”;
message.style[“color”] = “red”;
return;
}
mycommand.parameters[“@” + cols[i]].value = colvalue;
}
mycommand.parameters[“@state”].value = ((dropdownlist)e.item.findcontrol(“edit_state”)).selecteditem.tostring();
if (((checkbox)e.item.findcontrol(“edit_contract”)).checked == true)
mycommand.parameters[“@contract”].value = “1”;
else
mycommand.parameters[“@contract”].value = “0”;
mycommand.connection.open();
try
{
mycommand.executenonquery();
message.innerhtml = “<b>已更新记录</b><br>” + updatecmd;
mydatagrid.edititemindex = -1;
}
catch (sqlexception e)
{
if (e.number == 2627)
message.innerhtml = “错误:已存在具有相同主键的记录”;
else
message.innerhtml = “错误:未能更新记录,请确保正确填写了字段”;
message.style[“color”] = “red”;
}
mycommand.connection.close();
bindgrid();
}
public void bindgrid()
{
sqldataadapter mycommand = new sqldataadapter(“select * from authors”, myconnection);
dataset ds = new dataset();
mycommand.fill(ds, “authors”);
mydatagrid.datasource=ds.tables[“authors”].defaultview;
mydatagrid.databind();
}
</script>
<body style=”font: 10.5pt 宋体”>
<form runat=”server”>
<h3><font face=”宋体”>更新具有模板列的数据行</font></h3>
<span id=”message” enableviewstate=”false” style=”font: arial 11pt;” runat=”server”/><p>
<asp:datagrid id=”mydatagrid” runat=”server”
width=”800″
backcolor=”#ccccff”
bordercolor=”black”
showfooter=”false”
cellpadding=3
cellspacing=”0″
font-name=”verdana”
font-size=”8pt”
headerstyle-backcolor=”#aaaadd”
oneditcommand=”mydatagrid_edit”
oncancelcommand=”mydatagrid_cancel”
onupdatecommand=”mydatagrid_update”
datakeyfield=”au_id”
autogeneratecolumns=”false”
>
<columns>
<asp:editcommandcolumn edittext=”编辑” canceltext=”取消” updatetext=”更新” itemstyle-wrap=”false”/>
<asp:boundcolumn headertext=”au_id” sortexpression=”au_id” readonly=”true” datafield=”au_id” itemstyle-wrap=”false”/>
<asp:templatecolumn headertext=”au_lname” sortexpression=”au_lname”>
<itemtemplate>
<asp:label runat=”server” text=”<%# databinder.eval(container.dataitem, “au_lname”) %>”/>
</itemtemplate>
<edititemtemplate>
<asp:textbox runat=”server” id=”edit_lname” text=”<%# databinder.eval(container.dataitem, “au_lname”) %>”/>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext=”au_fname” sortexpression=”au_fname”>
<itemtemplate>
<asp:label runat=”server” text=”<%# databinder.eval(container.dataitem, “au_fname”) %>”/>
</itemtemplate>
<edititemtemplate>
<asp:textbox runat=”server” id=”edit_fname” text=”<%# databinder.eval(container.dataitem, “au_fname”) %>”/>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext=”phone” sortexpression=”phone”>
<itemtemplate>
<asp:label runat=”server” text=”<%# databinder.eval(container.dataitem, “phone”) %>”/>
</itemtemplate>
<edititemtemplate>
<asp:textbox runat=”server” id=”edit_phone” text=”<%# databinder.eval(container.dataitem, “phone”) %>”/>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext=”address” sortexpression=”address”>
<itemtemplate>
<asp:label runat=”server” text=”<%# databinder.eval(container.dataitem, “address”) %>”/>
</itemtemplate>
<edititemtemplate>
<asp:textbox runat=”server” id=”edit_address” text=”<%# databinder.eval(container.dataitem, “address”) %>”/>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext=”city” sortexpression=”city”>
<itemtemplate>
<asp:label runat=”server” text=”<%# databinder.eval(container.dataitem, “city”) %>”/>
</itemtemplate>
<edititemtemplate>
<asp:textbox runat=”server” id=”edit_city” text=”<%# databinder.eval(container.dataitem, “city”) %>”/>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext=”state” sortexpression=”state”>
<itemtemplate>
<asp:label runat=”server” text=”<%# databinder.eval(container.dataitem, “state”) %>”/>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist runat=”server” selectedindex=”<%# getstateindex(databinder.eval(container.dataitem, “state”).tostring()) %>” id=”edit_state”>
<asp:listitem>ca</asp:listitem>
<asp:listitem>in</asp:listitem>
<asp:listitem>ks</asp:listitem>
<asp:listitem>md</asp:listitem>
<asp:listitem>mi</asp:listitem>
<asp:listitem>or</asp:listitem>
<asp:listitem>tn</asp:listitem>
<asp:listitem>ut</asp:listitem>
</asp:dropdownlist>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext=”zip” sortexpression=”zip”>
<itemtemplate>
<asp:label runat=”server” text=”<%# databinder.eval(container.dataitem, “zip”) %>”/>
</itemtemplate>
<edititemtemplate>
<asp:textbox runat=”server” id=”edit_zip” text=”<%# databinder.eval(container.dataitem, “zip”) %>”/>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext=”contract” sortexpression=”contract”>
<itemtemplate>
<asp:label runat=”server” text=”<%# databinder.eval(container.dataitem, “contract”, “{0}”) %>”/>
</itemtemplate>
<edititemtemplate>
<asp:checkbox runat=”server” id=”edit_contract” checked=”<%# databinder.eval(container.dataitem, “contract”) %>”/>
</edititemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</form>
</body>
</html>