当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net 2.0中用GRIDVIEW插入新记录

ASP.NET
Asp.Net使用POST方法最简单的实现
实现.NET应用程序的自动更新
优秀ASP.NET程序员修炼之路
ASP.NET中实现模板页
在ASP.Net 2.0中实现多语言界面的方法
小议优化ASP.NET应用性能之Cache篇
.net开发投票机的思路
浅析CMS内容管理系统的两种方案
ASP.NET 2.0中动态修改页面标题
“您无权查看该网页”的原因和解决方法
将一个图片按比例缩放显示在一个Frame中
编程使用资源文件实现多语言页面(In Action)
.Net编程的多个小技巧
asp.net2.0学习历程-菜鸟到中级程序员的飞跃
asp.net如何连接sql server2000数据库
FCKeditor 2.6在ASP.NET中的配置方法
使用ASP.NET开发移动通讯的几种方法
ASP.NET 2.0的URL映射的实现方法
如何在Asp.net中使用HtmlArea编辑器
ASP.NET 2.0 中实现跨页提交

ASP.NET 中的 asp.net 2.0中用GRIDVIEW插入新记录


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 99 ::
收藏到网摘: n/a

看了下国外某巨牛的BLOG,大概是讲asp.net 2.0中用GRIDVIEW插入新记录的,方法比较特别,但效果一般,故将程序转之,较为简单,不做解释等。

以下为引用的内容:
<%@ Page Language="C#" ClassName="Default_aspx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">
  void CancelButton1_Click(object sender, EventArgs e)
  {

    GridView1.ShowFooter = false;

  }
  void AddButton1_Click(object sender, EventArgs e)
  {

    GridView1.ShowFooter = true;

  }

  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>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

  <title>Untitled Page</title>

</head>

<body>

  <form id="form1" runat="server">

  <div>
  <asp:Button ID="AddButton1" runat="Server" Text="Add new Item" OnClick="AddButton1_Click" />


  <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=xxx;database=northwind" EnableCaching="True">

            </asp:SqlDataSource>

          </ItemTemplate>

        </asp:TemplateField>

      </Columns>

    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" Runat="server"

      InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactTitle]) VALUES (@CustomerID, @CompanyName, @ContactTitle)"

      SelectCommand="SELECT [CustomerID], [CompanyName], [ContactTitle] FROM [Customers]"

      ConnectionString="server=localhost;uid=sa;password=xxxxx;database=northwind">

      <DeleteParameters>

        <asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>

      </DeleteParameters>

      <UpdateParameters>

        <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>

        <asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>

        <asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>

      </UpdateParameters>

      <InsertParameters>

        <asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>

        <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>

        <asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>

      </InsertParameters>

    </asp:SqlDataSource>

  </div>

  </form>

</body>

</html>