当前位置: 首页 > news >正文

Gridview的footer模板中放置控件实现添加功能

前台代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<asp:GridView ID= "GridView1"  runat= "server"  ShowFooter= "true"  AutoGenerateColumns= "false" >
            <Columns>
                <asp:TemplateField HeaderText= "编号" >
                    <ItemTemplate>
                        <%#Eval( "EmpID" ) %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText= "姓名" >
                    <ItemTemplate>
                        <%#Eval( "EmpName" ) %>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID= "TextBox1"  runat= "server" ></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText = "性别" >
                
                <ItemTemplate>
                <%#Eval( "EmpSex" ) %>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList AutoPostBack= "true"  ID= "DropDownList1"  runat= "server" >
                    <asp:ListItem Text = "请选择"  Value= "0" ></asp:ListItem>
                    <asp:ListItem Text = "男"  Value = "1" ></asp:ListItem>
                    <asp:ListItem Text = "女"  Value = "2" ></asp:ListItem>
                    </asp:DropDownList>
                </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText = "地址" >
                <ItemTemplate>
                <%#Eval( "EmpAddress" ) %>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID= "TextBox2"  runat= "server" ></asp:TextBox>
                    <asp:Button ID= "Button1"  OnClick= "AddClick"  runat= "server"  Text= "添加"  />
                </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
后台代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
protected  void  Page_Load( object  sender, EventArgs e)
    {
 
        if  (!IsPostBack)
        {
            databind();
        }
    }
    public  void  databind()
    {
        SqlConnection con =  new  SqlConnection(ConfigurationManager.ConnectionStrings[ "Conn" ].ToString());
        SqlCommand cmd =  new  SqlCommand( "SELECT * FROM T_Users" , con);
        SqlDataAdapter da =  new  SqlDataAdapter(cmd);
        DataSet ds =  new  DataSet();
        da.Fill(ds);
        this .GridView1.DataSource = ds.Tables[0];
        this .GridView1.DataKeyNames =  new  string [] {  "EmpID"  };
        this .GridView1.DataBind();
    }
    public  void  AddClick( object  sender, EventArgs e)
    {
        SqlConnection con =  new  SqlConnection(ConfigurationManager.ConnectionStrings[ "Conn" ].ToString());
        SqlCommand cmd =  new  SqlCommand();
        cmd.Connection = con;
        cmd.CommandText =  "Insert into T_Users(EmpName,EmpSex,EmpAddress) values(@a,@b,@c)" ;
        SqlParameter[] sp =  new  SqlParameter[3];
        sp[0] =  new  SqlParameter( "@a" , SqlDbType.NVarChar, 20);
        sp[1] =  new  SqlParameter( "@b" , SqlDbType.NVarChar, 4);
        sp[2] =  new  SqlParameter( "@c" , SqlDbType.NVarChar, 50);
        sp[0].Value = ((TextBox) this .GridView1.FooterRow.FindControl( "TextBox1" )).Text.Trim();
        sp[1].Value = ((DropDownList) this .GridView1.FooterRow.FindControl( "DropDownList1" )).SelectedItem.Text;
        sp[2].Value = ((TextBox) this .GridView1.FooterRow.FindControl( "TextBox2" )).Text.Trim();
        cmd.Parameters.AddRange(sp);
        if  (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        cmd.ExecuteNonQuery();
        databind();
    }
分类:  ASP.NET

本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2012/05/16/2503393.html,如需转载请自行联系原作者

相关文章:

  • 如何更高效地使用 OkHttp
  • BZOJ-3713[PA2014]Iloczyn
  • Spring绑定请求参数过程以及使用@InitBinder来注册自己的属性处理器
  • 铁路(栈)
  • Aspose------导入Excel
  • 生活:高效且健康的生活习惯
  • value toDF is not a member of org.apache.spark.rdd.RDD
  • MySql中把一个表的数据插入到另一个表中的实现代码
  • 图片定位问题
  • xencenter如何安装系统
  • ASP.NET MVC Model元数据及其定制: Model元数据的定制
  • 04-String
  • 社工-入侵
  • Spring声明式事务管理之一:五大属性分析
  • 解决Activity启动黑屏及设置android:windowIsTranslucent不兼容activity切换动画问题
  • 分享一款快速APP功能测试工具
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • JavaScript类型识别
  • js中的正则表达式入门
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • tab.js分享及浏览器兼容性问题汇总
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 动态魔术使用DBMS_SQL
  • 欢迎参加第二届中国游戏开发者大会
  • 技术:超级实用的电脑小技巧
  • 开发基于以太坊智能合约的DApp
  • 使用putty远程连接linux
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 新书推荐|Windows黑客编程技术详解
  • 与 ConTeXt MkIV 官方文档的接驳
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • ​520就是要宠粉,你的心头书我买单
  • ​学习一下,什么是预包装食品?​
  • (02)Cartographer源码无死角解析-(03) 新数据运行与地图保存、加载地图启动仅定位模式
  • (8)STL算法之替换
  • (ibm)Java 语言的 XPath API
  • (ZT)出版业改革:该死的死,该生的生
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • (一)VirtualBox安装增强功能
  • (转)Oracle存储过程编写经验和优化措施
  • (转)socket Aio demo
  • (转)创业家杂志:UCWEB天使第一步
  • .libPaths()设置包加载目录
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • .NET/C# 项目如何优雅地设置条件编译符号?
  • .NET单元测试
  • .sdf和.msp文件读取