技术开发 频道

ASP.NET优化DataGrid控件的编辑功能


【IT168技术文档】
  尽管在上面的实例中我们已经实现了DataGrid的在线编辑功能,但是,如果我们已经习惯了C/S 结构的程序,就会感觉到上个实例中编辑的不足:提交数据频繁,加重了服务器的负担。在这一节中,我们利用一个实例来演示优化后的DataGrid控件的编辑功能,其中的技术就是引入批量更新数据。引入的一个新知识就是控件的FindControl方法。
我们来看具体实例。首先在DataCon Web项目里,添加一个Web Form,命名为DataGrid_Sample6.aspx,然后添加一个DataGrid控件,由于我们做了DataGrid控件的显示模版,并且为了优化其编辑属性,我们特别利用<asp:TemplateColumn ><ItemTemplate></ItemTemplate></asp:TemplateColumn >属性添加了DropDownList控件和CheckBox控件。为了便于实例应用和读者理解,我们新建一个TeacherInfor.mdb数据库,该数据库包含一个teacher数据表 ,字段类型和虚拟数据如图9.11和9.12所示。

图9.11 teacher数据表中的字段属性

DataGrid_Sample6.aspx的主要HTML代码如下:
<body topMargin="0" MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <FONT face="宋体"><b>经济管理学院教师信息</b> <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="320px" PageSize="4" AllowPaging="True"> <AlternatingItemStyle BackColor="WhiteSmoke"></AlternatingItemStyle> <ItemStyle BackColor="GhostWhite"></ItemStyle> <HeaderStyle BackColor="LightSteelBlue"></HeaderStyle> <Columns> <asp:BoundColumn DataField="id" HeaderText="编号"></asp:BoundColumn> <asp:BoundColumn DataField="name" HeaderText="姓名"></asp:BoundColumn> <asp:TemplateColumn HeaderText="性别"> <ItemTemplate> <asp:DropDownList id="sex" runat="server" SelectedIndex='<%# Cint(DataBinder.Eval(Container,"DataItem.sex")) %>' > <asp:ListItem Value="0"></asp:ListItem> <asp:ListItem Value="1"></asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="所获学位"> <ItemTemplate> <asp:DropDownList id="degree" runat="server" SelectedIndex='<%# cint(DataBinder.Eval(Container,"DataItem.degree")) %>'> <asp:ListItem Value="0">学士</asp:ListItem> <asp:ListItem Value="1">硕士</asp:ListItem> <asp:ListItem Value="2">博士</asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="性别"> <ItemTemplate> <asp:DropDownList id="title" runat="server" SelectedIndex='<%# Cint(DataBinder.Eval(Container,"DataItem.title")) %>' > <asp:ListItem Value="0">讲师</asp:ListItem> <asp:ListItem Value="1">副教授</asp:ListItem> <asp:ListItem Value="2">教授</asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="婚否"> <ItemTemplate> <asp:CheckBox Runat=server Checked ='<%# DataBinder.Eval(Container,"DataItem.marry") %>' ID="marry" Text ="婚否"> </asp:CheckBox> </ItemTemplate> </asp:TemplateColumn> </Columns> <PagerStyle BackColor="LightSteelBlue" Mode="NumericPages"></PagerStyle> </asp:DataGrid> <asp:button id="Button1" runat="server" Text="批量更新"> </asp:button></FONT> </form> </body>

0
相关文章