网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.ASPX页Web服务调用性能优化.
.ASP.NET无限分类.
.部署ASP.NET(包含.net framework.
.利用WWF进行ASP.NET程序开发 .
.ASP.NET的错误处理机制.
.使用ASP.NET Atlas ListView控件.
.ASP.NET 2.0运行时简要分析.
.图片、文件防盗链程序.
.在ASP.net中使用TinyMCE的小tip-.
.ASP.NET 2.0 Membership.
.根据身份证判断性别和生日.
.一个简单的数据操作类.
.HTTP性能调优之设置连接失效时间.
.ASP.NET保持用户状态的九种选择(.
.ASP.NET与MySQL数据库简明图示入.
.发布一个好用的类SafeConvert, 用.
.ASP.NET应用程序安全性问题.
.ASP.NET 无法确保在注册的 JavaS.
.ASP.NET实现页面间值传递的几种方.
.asp.net 2.0 中加密web.config 文.

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

发表日期:2004-9-27


看了下国外某巨牛的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>

上一篇:Asp.Net 使用 GDI+ 绘制3D饼图入门篇源码 人气:14505
下一篇:在ASP.NET中跨页面实现多选 人气:11126
浏览全部asp.net的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐