首页学院源码字体下载软件电脑书| 素材图片模版壁纸酷站图标笔刷纹理|小游戏博客QQ表情投票计数器JoyRssMail
动态网站制作指南
首页|网络编程|站长之家|网页制作|图形图象|操作系统|冲浪宝典|软件教学|网络办公|手机学院|邮件系统|网络安全|认证考试
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
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.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!


发表日期:2006-7-19 |


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="10"
                        Width="542px" AllowPaging="True" AllowSorting="True"
                         DataKeyNames="DB31_1,DB31_2" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnSorting="GridView1_Sorting" >
                        <Columns>
                            <asp:TemplateField HeaderText="序号">
                                <ItemTemplate>
                                <%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="学历代码" SortExpression="DB1_1">
                                <EditItemTemplate>
                                    <%--<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DB1_1") %>'></asp:TextBox>--%>
                               <asp:DropDownList ID ="ddlXL" runat="server"  DataValueField='<%# Bind("DB1_1") %>'></asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("xueliText") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="学历名称" SortExpression="DB1_2">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("DB1_2") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("DB1_2") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                         
                        <asp:TemplateField HeaderText="操作" ShowHeader="False">
                        <EditItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                 Text="更新"></asp:LinkButton>
                            <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="取消"></asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                                Text="编辑" OnClientClick="return confirm('确认要编辑吗?');"></asp:LinkButton>
                       
                            <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"
                                Text="删除" OnClientClick="return confirm('确认要删除吗?');"></asp:LinkButton>
                            <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Select"
                                Text="选择"></asp:LinkButton>
                        </ItemTemplate>
                        </asp:TemplateField>
                       </Columns>
                       <AlternatingRowStyle BackColor="Aquamarine" />
                    </asp:GridView>
    /// <summary>
    /// 绑定数据到GridView
    /// </summary>
    private void GridViewBind()
    {
        检索数据库
        string strSql = "SELECT * FROM DB1";
        得到数据集
        this.GridView1.DataSource=conn.GetDs(strSql).Tables[0].DefaultView;
        this.GridView1.DataBind();
  
    }
    /// <summary>
    /// 编辑当前行
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        //当前编辑行背景色高亮
        this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");
        GridViewBind();
    }
    /// <summary>
    /// 取消编辑状态
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
    }
    /// <summary>
    /// 删除记录过程
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //得到单位编号
        string rowToDelete = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        //转换为整数
        //int ID=Convert.ToInt32(rowToDelete);
        //从数据库中删除
        string str = "DELETE FROM DB1 where DB1_1=" + "'" + rowToDelete + "'" + "";
      
        try
        {
        conn.RunSql(str);
        //重新绑定数据
        GridViewBind();
        }
        catch (Exception ex)
        {
        Response.Write("数据库错误,错误原因:" + ex.Message);
        Response.End();
        }

    }
    /// <summary>
    /// 更新记录过程
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        string DB1_1 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
        //string DB1_2 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
        string DB1_2 = (((DropDownList))GridView1.Rows[e.RowIndex].FindControl("ddlXL")).SelectedItem.Text;
       
    //判断表单项是否有空并给出提示信息
        if (DB1_1 == "" || DB1_2 == "")
        {
            conn.Alert("请输入完整信息!", Page);
            return;
        }            
       try
       {
        conn.BuilderEdit("select * from DB1 where DB1_1 ='" + ID + "'");
        conn.dr["DB1_1"] = DB1_1;
        conn.dr["DB1_2"] = DB1_2;
        conn.BuilderEditClose();
       }
       catch (OracleException err)
       {
            if (err.Code.ToString() == "1")
                conn.Alert("错误:已存在具有相同主键的记录", Page);
            else
                conn.Alert("错误:未能添加记录", Page);
        }

        Response.Write("<script language='javascript'>alert('数据已被保存!');</script>");
        //返回浏览状态
        GridView1.EditIndex = -1;
        GridViewBind();
    }
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridViewBind();
    }
    /// <summary>
    /// 加入鼠标效果及为DropDownList绑定值
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //为DropDownList绑定值
        if (((DropDownList)e.Row.FindControl("ddlXL")) != null)
        {
            DropDownList ddlXL = (DropDownList)e.Row.FindControl("ddlXL");
            ddlXL.Items.Clear();
            ddlXL.Items.Add(new ListItem("博士", "1"));
            ddlXL.Items.Add(new ListItem("硕士", "2"));
            ddlXL.Items.Add(new ListItem("学士", "3"));
        }

        //加入鼠标滑过的高亮效果
        if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
        {
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
        //单击行改变行背景颜色
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';");
        }
    }

关注此文的读者还看过:
·解决文件下载弹出窗体被拦截
·TreeView使用笔记
·ASP.NET 取得客户端信息
·asp.net生成缩略图及给原始图加水印
·您可能不知道的.Net2.0小技巧
·网页乱码问题ASP.NET
·ASP.NET中实现中文简/繁体自动转换的类
·ASP.Net应用程序的多进程模型
·用Repeater控件显示数据
·怎样在Web开发中完美控制IE标题栏

浏览全部ASP.NET的内容 PS笔刷下载 39种不同语言在线翻译
文章搜索服务





邮件订阅服务
输入你的邮件地址,你将不会错过任何关于<ASP.NET技巧>的内容
Powered by JoyRssMail

网络编程文章分类

本月文章推荐
. ASP.NET基于角色的窗体安全认证机制.
. ASP.NET服务器控件之视图状态.
. 收藏一段小的.net下的验证码片段.
. ASP.NET2.0的跨页回调.
. 在ASP.NET中访问SQL 2005报表服务 .
. 在ASP.NET Atlas中调用Web Service.
. .Net 2.0 原汁原味读取注册表.
. ASP.NET2.0登陆控件的使用(常见的.
. ASP.NET2.0下为GridView添加服务器.
. Lucene.net 实现全文搜索.
. 使用函数传递参数来执行数据库操作.
. 浅谈在ASP.NET中数据有效性校验的方.
. .net打包自动安装数据库.
. 轻松解决asp.net用户ASPNET登录失败.
. 在 ASP.NET 中使用计时器(Timer).
. 创建完全可编辑的 DataGrid.
. ASP.NET设计中的性能优化问题.
. Asp.net生成htm静态文件的两种途径.
. 在ASP.NET中上传图片并生成缩略图的.
. Web Service非Soap头(Session)身份.
. 获取数据行数据信息.
. Asp.net(c#)实现多线程断点续传.
. .NET中如何在调用COM时得到返回参数.
. ASP.NET网络编程中经常用到的27个函.
. 在ASP.Net中应用Javascript.
. .NET精简框架的“无法找到资源程序.
. 得到内网可用的SqlServer 及某数据.
. asp.net 2.0中预设的cookie.
. Microsoft .NET Framework 2.0 Ser.
. 深入聊聊Array的sort方法的使用技巧.
  最新网站源码 最新软件下载
2009-7-4 84网址站 v4.10
2009-7-4 仿webmasterhome 网站收录查询 v
2009-7-4 NOCC v1.8
2009-7-4 易和阳光购物商城 v1.4 Access F
2009-7-4 OK伯乐招聘求职系统
2009-7-3 MyBB v1.4.8
2009-7-3 miniBB v2.3
2009-7-3 Modoer多功能点评系统 v1.1.0 Bu
2009-7-3 Modoer多功能点评系统 v1.1.0 Bu
2009-4-4 SharePoint Designer 2007
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
  发表评论
姓 名: 验证码: [ 查看评论 ]
内 容:
 
站长工具:Google PR查询|网站速度测试|CSS在线编辑器|OPEN参数生成器|弹出式窗口代码产生器|密码登录生成器|在线按钮生成器|Meta标签生成器|邮箱图标在线生成|多色彩特效字代码生成器|网页代码调试器|在线FTP登陆|Flash取色器|配色代码对照表|配色辞典|CSS生成器|CSS在线压缩|广告代码|框架网页代码生成器|js/vbs加密|md5加密|进制转换|UTF-8 转换工具|在线调色板|Html转换js|Html转换asp|Html转换php|Html转换perl
实用工具:汉字翻译拼音|拼音字典|在线翻译|天气预报|火星文|在线网速测试|符号对照表|个税计算|公积金贷款计算|购房银行按揭利率计算|实时汇率查询换算|经典小工具|汉字简繁转换|普通单位换算|公制单位换算|生辰老黄历|国内电话区号|国家代码与域名缩写|文字加密解密|元素周期表|健康查询|世界时间|全国各地车牌查询|全国车辆交通违章查询|万年历|二十四节气|汉字横竖排版|手机号码查询|计算器|ip搜索
广告刊登 | 版权声明 | 联系我们 | 加入收藏 | RSS订阅
Copyright © 2000-2009 www.knowsky.com All rights reserved | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵
SEO対策 コーチ ホームページ作成
過払い請求 現金化 債務整理 現金化