关于在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能够正常通信,发现使用库函数建的工程程序不能通过,读出来的数据和写的数据不一 ...
随机推荐
- C#如何分割多个空格分隔的字符串?
using System; using System.Text; using System.Text.RegularExpressions; namespace test { class Progra ...
- Android IOS WebRTC 音视频开发总结(七八)-- 为什么WebRTC端到端监控很关键?
本文主要介绍WebRTC端到端监控(我们翻译和整理的,译者:weizhenwei,校验:blacker),最早发表在[编风网] 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信I ...
- Xena测试仪的自动化
Xena,Xena Networks公司的网络测试仪,也能覆盖以太网L2~L7层测试仪,但功能较简单,界面也很简洁,用起来比较直观方便. 1.Xena的自动化测试场景 测试PC上的AT框架--> ...
- PriorityQueue优先队列用法入门
PriorityQueue是队列的一种,它叫做优先队列,该类实现了Queue接口. 之所以叫做优先队列,是因为PriorityQueue实现了Comparator这个比较接口,也就是PriorityQ ...
- input 放大镜
<input results="s" type="search" size="20px" placeholder="搜 ...
- 最近学习linux命令的一个总结
最近学习了unix power tools,一方面是想增加对unix系统的了解:另一方面也是想增进使用效率,因为unix一大特色就是内置工具的丰富性.有了这些工具,可以方便的查看系统信息,查找需要的文 ...
- phpstorm 16.1 注册码
phpstorm 2016.1 的注册与phpstorm 10 相同,可以采用:“服务器注册” 方式进行注册,又快又方便. 服务器注册:http://idea.qinxi1992.cn IDEA 20 ...
- java -cp
java -cp /home/hdp/log4jTest/log4j-1.2.17.jar:/home/hdp/log4jTest/testLog.jar:/home/hdp/log4jTest/co ...
- WPF 大数据加载过程中的等待效果——圆圈转动
大家肯定遇到过或将要遇到加载大数据的时候,如果出现长时间的空白等待,一般人的概念会是:难道卡死了? 作为一个懂技术的挨踢技术,即使你明知道数据量太大正在加载,但是假如看不到任何动静,自己觉得还是一种很 ...
- mvc DropDownList默认选项
DDDContext db = new DDDContext(); List<SelectListItem> selectlistDistrict = new List<Select ...