当前位置: 首页 > 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切换动画问题
  • [LeetCode] Wiggle Sort
  • golang中接口赋值与方法集
  • Hexo+码云+git快速搭建免费的静态Blog
  • Less 日常用法
  • Python学习笔记 字符串拼接
  • Sass Day-01
  • SQLServer之创建数据库快照
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • Vim Clutch | 面向脚踏板编程……
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • webpack+react项目初体验——记录我的webpack环境配置
  • 从零搭建Koa2 Server
  • 搭建gitbook 和 访问权限认证
  • 高程读书笔记 第六章 面向对象程序设计
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 看域名解析域名安全对SEO的影响
  • 聊聊flink的TableFactory
  • 如何学习JavaEE,项目又该如何做?
  • 一天一个设计模式之JS实现——适配器模式
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • C# - 为值类型重定义相等性
  • 从如何停掉 Promise 链说起
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • ​queue --- 一个同步的队列类​
  • ​力扣解法汇总946-验证栈序列
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • #### go map 底层结构 ####
  • #includecmath
  • #pragma once
  • #每日一题合集#牛客JZ23-JZ33
  • (1)Nginx简介和安装教程
  • (10)ATF MMU转换表
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (每日持续更新)jdk api之StringBufferInputStream基础、应用、实战
  • (三)centos7案例实战—vmware虚拟机硬盘挂载与卸载