广

ASP.NET

  • IOS开发
  • android开发
  • PHP编程
  • JavaScript
  • ASP.NET
  • ASP编程
  • JSP编程
  • Java编程
  • 易语言
  • Ruby编程
  • Perl编程
  • AJAX
  • 正则表达式
  • C语言
  • 编程开发

    ASP.NET2.0中用Gridview控件操作数据

    2018-04-08 09:04:52 次阅读 稿源:互联网
    广告

      在ASP.NET 2.0中,加入了许多新的功能和控件,相比asp.net 1.0/1.1,在各方面都有了很大的提高。其中,在数据控件方面,增加了不少控件,其中的Gridview控件功能十分强大。在本文中,将探讨Gridview控件中的一些功能特性和用法,如果各位读者对Gridview控件不大了解,可以通过《使用ASP.NET 2.0中的Gridview控件》一文,来对Gridview控件有个初步的认识。

      1、使用Gridview插入新记录

      在Gridview控件中,可以实现插入新记录的操作(见《使用ASP.NET 2.0中的Gridview控件》)一文,但如果想实现在Gridview中,实现在Gridview控件的最后一行,提供一个空白行给用户输入要输入的记录,那无疑是很方便的。下面将介绍其实现方法。

      首先,我们打算在让用户进行选择,当用户需要新增一记录时,便点击新增按钮,之后在Gridview的最后一行里,显示一个空白行,让用户按字段进行输入,如下图所示:

      当用户决定不输入新空白记录时,可以按"cancel"按钮返回,该空白行消失。要实现这样的效果,我们可以充分利用Gridview的footer的模版功能进行自定义,因为有3列,所以,在每一列的footer模版中,定义如下:

      

    <asp:Gridview ID="Gridview1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AutoGenerateColumns="False" ShowFooter="True"><Columns> <asp:TemplateField>  <ItemTemplate>   <asp:Label ID="CustomerIDLabel" Runat="Server"><%# Eval("CustomerID") %></asp:Label>  </ItemTemplate>  <FooterTemplate>   <asp:TextBox ID="CustomerIDTextBox" Runat="server"></asp:TextBox>  </FooterTemplate> </asp:TemplateField> <asp:TemplateField>  <ItemTemplate>   <asp:Label ID="CompanyNameLabel" Runat="Server"><%# Eval("CompanyName") %></asp:Label>  </ItemTemplate>  <FooterTemplate>   <asp:TextBox ID="CompanyNameTextBox" Runat="server"></asp:TextBox>  </FooterTemplate> </asp:TemplateField> <asp:TemplateField>  <FooterTemplate>   <asp:DropDownList ID="ContactTitleDropDownList" Runat="server" DataSourceID="SqlDataSource2" DataTextField="ContactTitle" DataValueField="ContactTitle">   </asp:DropDownList>   <asp:SqlDataSource ID="SqlDataSource2" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"ConnectionString="server=localhost;uid=sa;password=xxx;database=northwind">   </asp:SqlDataSource>   <asp:Button ID="Button1" Runat="server" Text="Add" OnClick="Button1_Click" />   <asp:Button ID="CancelButton1" Runat="server" Text="Cancel" OnClick="CancelButton1_Click" />  </FooterTemplate> <ItemTemplate>  <asp:DropDownList ID="ContactTitleDropDown" SelectedValue=’<%# Bind("ContactTitle") %>’ Runat="Server" DataSourceID="SqlDataSource3" DataTextField="ContactTitle" DataValueField="ContactTitle" ></asp:DropDownList>  <asp:SqlDataSource ID="SqlDataSource3" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"ConnectionString="server=localhost;uid=sa;password=xxxx;database=northwind" EnableCaching="True">  </asp:SqlDataSource> </ItemTemplate> </asp:TemplateField></Columns></asp:Gridview>

      以上为Gridview的代码,可以看到,在第一,二列的<foottemplate>列中,分别提供了customerid和companyname两个文本框以供用户输入,在第三列的<footertemplate>列中,以dropdownlistbox的形式来显示contracttitle.。其中,请注意第三列的footertemplate中的add和cancel两个按钮的,它们的事件代码如下

      

    <script runat="server">void CancelButton1_Click(object sender, EventArgs e){ Gridview1.ShowFooter = false;}void AddButton1_Click(object sender, EventArgs e){ Gridview1.ShowFooter = true;} //点add按钮时,将新增的记录更新到数据库中去void Button1_Click(object sender, EventArgs e){ TextBox customerID = Gridview1.FooterRow.FindControl("CustomerIDTextBox") as TextBox; TextBox companyName = Gridview1.FooterRow.FindControl("CompanyNameTextBox") as TextBox; DropDownList ContactTitle = Gridview1.FooterRow.FindControl("ContactTitleDropDownList") as DropDownList; SqlDataSource1.InsertParameters["CustomerID"].DefaultValue = customerID.Text; SqlDataSource1.InsertParameters["CompanyName"].DefaultValue = companyName.Text;   SqlDataSource1.InsertParameters["ContactTitle"].DefaultValue=ContactTitle.SelectedValue; SqlDataSource1.Insert();}</script>

    一起学吧部分文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与一起学吧进行文章共享合作。

    广告
    广告
    广告