关于在gridview中有dorpdownlist的情况下使用自带编辑模板的方法
今天记录一下在gridview中,如果有dropdownlist的情况下使用gridview自带编辑模式的方法。
好吧,今天的这个问题有点绕,详细解释一下目的。
因为gridview中的某些列的数据是从basedata里面带出来的,在编辑gridview的时候,user是想手动选择列值,而不是手动输入(输入不对的话,系统会报错),以上是背景。
OK,想了想,在gridview中可以这样实现这个功能,用gridview自带的编辑模板,数据呈现用label绑定,数据编辑的时候用dropdownlist,下面贴出代码:
前台:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="category">
<ItemTemplate>
<asp:Label ID="label1" runat="server" Text='<%#Bind("category") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="label2" runat="server" Text='<%#Bind("category") %>' CssClass="hideColumn"></asp:Label>
<asp:DropDownList ID="drpCategory" runat="server" Width="156px">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
} protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{ }
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{ }
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -;
BindGridView();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int itest = e.Row.RowIndex;
if (GridView1.EditIndex == )
{
Label label2 = (Label)e.Row.FindControl("label2");
string strCategory = label2.Text.Trim();
DropDownList drpCategory = (DropDownList)e.Row.FindControl("drpCategory");
BindDropDownList(drpCategory,strCategory);
}
}
} private void BindDropDownList(DropDownList drpCategory,string strCategory)
{ DataTable dt = new DataTable();
dt.Columns.Add("category");
drpCategory.DataSource = dt;
drpCategory.DataBind();
drpCategory.DataTextField = "category";
drpCategory.DataValueField = "category";
drpCategory.Items.Insert(, new ListItem("请选择", ""));
drpCategory.Items.Insert(, new ListItem("test1", "test1"));
drpCategory.Items.Insert(, new ListItem("test2", "test2"));
drpCategory.Items.Insert(, new ListItem("test", "test")); drpCategory.SelectedValue = strCategory;
} private void BindGridView()
{
DataTable dt = new DataTable();
dt.Columns.Add("category");
//dt.Rows.Add(dt.NewRow());
DataRow row = dt.NewRow();
row["category"] = "test";
dt.Rows.Add(row);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
效果截图:
点击编辑
显示dropdownlist,并绑定了label的值


OK,写完啦,睡觉~明天周一继续努力!
关于在gridview中有dorpdownlist的情况下使用自带编辑模板的方法的更多相关文章
- oracle 如何在表中有数据的情况下,修改表字段的类型或者增加表字段的长度
场景:项目中某张表的字段长度不够用了,现在要增加其长度 分两种情况: 1.表中没有数据的情况,直接一条sql语句就能解决 alter table 表名 modify(字段名 字 ...
- Visual c++例子,可不使用常规的对话框资源模板的情况下,动态创建对话框的方法
详细说明:Visual c++例子,可不使用常规的对话框资源模板的情况下,动态创建对话框的方法.该方法可以在运行时在内存中直接建立对话框资源,使用起来更为灵活.适用于多个开发项目共享有界面的公用程序模 ...
- 没有 iOS 开发者账号的情况下部署到真机的方法
原文发表于我的技术博客 本文分享了官方推荐的没有 iOS 开发者账号的情况下部署到真机的方法,供参考. 原文发表于我的技术博客 1. 官方推荐的方法 原文在此,也就是 Ionic 官方团队在博客中分享 ...
- 关于img标签浏览器自带的边框,清除边框的解决方式(即img[src=""] img无路径情况下,灰色边框去除解决方法)
详解img[src=""] img无路径情况下,灰色边框去除解决方法 1.Js解决办法 <html> <head> <meta charset=&qu ...
- oracle 在表中有数据的情况下修改表字段类型或缩小长度
分享自己一些常用的sql语句给大家 偶尔我们需要在已有表,并且有数据的情况下,修改其某个字段的类型或缩短他的长度,但是因为表中有数据,所以不可以直接修改,需要换个思路. //建立测试表,可跳过(善于应 ...
- ASP.NET在不同情况下实现单点登陆(SSO)的方法
第一种:同主域但不同子域之间实现单点登陆 Form验证其实是基于身份cookie的验证.客户登陆后,生成一个包含用户身份信息(包含一个ticket)的cookie,这个cookie的名字就是在web. ...
- 计算纯文本情况下RichTextBox实际高度的正确方法(.NET)
2016-07-17重大更新 其实有更好.更系统的方法,也是最近才发现的,分享给大家!! /// <summary> /// /// </summary> ...
- linux服务器没网情况下手动安装软件几个方法
1,找到一个有网的服务器,使用yumdownloader gcc,获取需要的rmp包: 2,在http://pkgs.org 下下载所需要的rpm包
- stm32库函数建工程和使用Keil自带库建工程有没有区别?发现了同样的程序在两种情况下keil自带库可以运行的情况,不知是什么原因
我使用库函数建的工程(非Keil自带库),为了实现SPI对Si24r1芯片数据的读写,以验证stm32是否可以和si24r1能够正常通信,发现使用库函数建的工程程序不能通过,读出来的数据和写的数据不一 ...
随机推荐
- express 快速教程
阅读 express 官方文档的记录. hello world example var express = require('express') var app = express() app.get ...
- Lenovo ThinkPad W520 4282-A76
processor: Intel Quad Core i7-2630QM (2GHz, 8MB L3, 1333MHz FSB, 45W) graphics adapter: NVIDIA Quadr ...
- ASP.NET生成WORD文档,服务器部署注意事项
网上转的,留查备用,我服务器装的office2007所以修改的是Microsoft Office word97 - 2003 文档这一个. ASP.NET生成WORD文档服务器部署注意事项 1.Asp ...
- UDP发送和接收
发送函数 public bool udpSend(string ip, int port, byte[] data) { Socket socket = new Socket(AddressFamil ...
- ubuntu安装mysql-python出错,EnvironmentError: mysql_config not found
安装mysql-python包出错 Downloading MySQL-python-.zip (108kB) % |████████████████████████████████| 112kB 1 ...
- html5 placeholder ie 不兼容问题 解决方案
解决HTML5 placeholder的方案 来源: 时间:2013-09-05 20:06:49 阅读数:11375 分享到: 0 [导读] 使低版本浏览器支持Placeholder有很多方 ...
- mongodb 分组查询
数据的保存 include_once 'mDB.class.php'; $m=new mDB(); $m->setDB('mydb'); // $m->save('stu',['dept' ...
- 使用php技术实现无刷新的上传文件
- AIX 环境下动态路由
IBM AIX v5.3操作系统环境下动态路由配置如下: 1,用命令lssrc -S routed和lssrc -S gated分别检查routed和gated子系统是是活动状态.如果这两个子系统为活 ...
- 微软ASP.NET技术“乱谈”
微软ASP.NET技术“乱谈” 2014新年了,顺手写的一点文字,主要谈谈我对当前微软ASP.NET技术的看法,比较随意,大伙儿随便看看吧. 1 当前微软Web平台技术全貌 从2002年发布.NET ...