GridView and DropDownList

<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gv_KeyList" AutoGenerateColumns="False"
Width="99%" onrowdeleting="gv_KeyList_RowDeleting">
<Columns>
<asp:BoundField DataField="text" HeaderText="Friends" />
<asp:TemplateField> <ItemTemplate>
<asp:DropDownList runat="server" ID="DropDownList1" DataSource='<%#DDLBind() %>' DataTextField="text" DataValueField="id"> </asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
if (!IsPostBack)
{
DropDownList dll;
string strSql = "select * from emailreceive where p_id!=1 ";
string conn = "server=.;database=emailfriends;uid=sa;pwd=123";
SqlConnection conns = new SqlConnection(conn);
conns.Open();
SqlCommand cmd = new SqlCommand(strSql, conns);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds, "a");
gv_KeyList.DataSource = ds.Tables[];
gv_KeyList.DataBind(); DataTable dt = DDLBind(); for (int i = ; i < gv_KeyList.Rows.Count;i++ )
{
DataRowView mygv=ds.Tables[].DefaultView[i];
for(int j=;j<dt.Rows.Count;j++)
{ DataRowView pKeyName = dt.DefaultView[j];
if (Convert.ToInt32(mygv["p_id"]) == Convert.ToInt32(pKeyName["id"]))
{
dll = (DropDownList)gv_KeyList.Rows[i].FindControl("DropDownList1");
string id=mygv["p_id"].ToString();
dll.SelectedValue = id;
}
} } conns.Close(); }
} public DataTable DDLBind()
{
string strSql1 = "select * from emailreceive where p_id=1 ";
string conn1 = "server=.;database=emailfriends;uid=sa;pwd=123";
SqlConnection conns1 = new SqlConnection(conn1);
conns1.Open();
SqlCommand cmd1 = new SqlCommand(strSql1, conns1);
SqlDataAdapter sda1 = new SqlDataAdapter();
sda1.SelectCommand = cmd1;
DataSet ds1 = new DataSet();
sda1.Fill(ds1, "a");
conns1.Close();
return ds1.Tables[]; } protected void gv_KeyList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string ddlValues = ((DropDownList)gv_KeyList.Rows[e.RowIndex].Cells[].FindControl("DropDownList1")).SelectedValue;
Response.Write(ddlValues);
}
}
三层的前台代码:


<asp:GridView runat="server" ID="gv_KeyList" Width="99%"
AutoGenerateColumns="False" onrowdeleting="gv_KeyList_RowDeleting"
DataKeyNames="I_KeyId" onrowcancelingedit="gv_KeyList_RowCancelingEdit"
onrowediting="gv_KeyList_RowEditing" onrowupdating="gv_KeyList_RowUpdating">
<Columns>
<asp:BoundField DataField="Vc_KeyName" HeaderText="关键词" />
<asp:TemplateField HeaderText="父关键词"> <ItemTemplate>
<asp:DropDownList runat="server" ID="ddlPKey" DataSource='<%#ddlBind() %>' DataTextField="Vc_KeyName" DataValueField="I_KeyId"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Vc_Url" HeaderText="关键词连接" />
<asp:CommandField HeaderText="修改" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
三层UI层代码:


chinaOfQiHuo.BLL.Guanjiancixinxi bllKey = new chinaOfQiHuo.BLL.Guanjiancixinxi(); protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DDL_PKey.DataTextField = "Vc_KeyName";
DDL_PKey.DataValueField = "I_KeyId";
DDL_PKey.DataSource = ddlBind();
DDL_PKey.DataBind();
gv_KeyList.DataSource = gvBind();
gv_KeyList.DataBind();
//绑定ddl
DropDownList ddl;
for (int i = ; i < gv_KeyList.Rows.Count; i++)
{
for (int j = ; j < ddlBind().Count; j++)
{
if (gvBind()[i].I_SuperId == ddlBind()[j].I_KeyId)
{
ddl = (DropDownList)gv_KeyList.Rows[i].FindControl("ddlPKey");
ddl.SelectedValue = gvBind()[i].I_SuperId.ToString();
}
}
}
}
}
public List<chinaOfQiHuo.Model.Guanjiancixinxi> ddlBind()
{ return bllKey.GetDDLPKey(); }
public List<chinaOfQiHuo.Model.Guanjiancixinxi> gvBind()
{
return bllKey.GetGVList();
三层BLL层代码:


chinaOfQiHuo.DAL.Guanjiancixinxi dalKey = new DAL.Guanjiancixinxi();
public bool Add(chinaOfQiHuo.Model.Guanjiancixinxi model,out string Msg)
{
if (Exists(model.Vc_KeyName))//已存在关键词
{
Msg = "关键词已存在";
return false;
}
else
{
int id = ; id = dalKey.Add(model);
if (id > )
{
Msg = "关键词添加成功";
return true;
}
else {
Msg = "添加超时,请重新添加";
return false;
}
} }
public bool Exists(string keyName)
{
return dal.Exists(keyName);
}
public List<Model.Guanjiancixinxi> GetDDLPKey()
{
return dalKey.GetDDLPKey();
}
public List<Model.Guanjiancixinxi> GetGVList()
{ return dalKey.GetGVKeyList();
} }
三层DAL层代码:


public bool Exists(string keyName)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select count(1) from tb_Guanjiancixinxi");
strSql.Append(" where Vc_KeyName = @keyName");
SqlParameter[] parameters = {
new SqlParameter("@keyName", SqlDbType.VarChar,)
};
parameters[].Value = keyName; return DbHelperSQL.Exists(strSql.ToString(), parameters);
}
//绑定父关键字
public List<Model.Guanjiancixinxi> GetDDLPKey()
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from tb_Guanjiancixinxi where I_SuperId=0 "); List<Model.Guanjiancixinxi> pKeylist = new List<Model.Guanjiancixinxi>();
SqlDataReader dr= SqlHelper.ExecuteReader(SqlHelper.SqlConnectionString,CommandType.Text,strSql.ToString(),null);
while (dr.Read())
{
Model.Guanjiancixinxi model = new Model.Guanjiancixinxi();
model.Vc_KeyName = dr["Vc_KeyName"].ToString();
model.I_KeyId = Convert.ToInt32(dr["I_KeyId"]);
pKeylist.Add(model);
}
return pKeylist;
}
//获取关键字列表
public List<Model.Guanjiancixinxi> GetGVKeyList()
{
string strSql = "select I_KeyId,I_SuperId,Vc_KeyName,Vc_Url FROM tb_Guanjiancixinxi where I_SuperId!=0";
SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.SqlConnectionString,CommandType.Text,strSql,null);
List<Model.Guanjiancixinxi> gvList = new List<Model.Guanjiancixinxi>();
while (dr.Read())
{
Model.Guanjiancixinxi model = new Model.Guanjiancixinxi();
model.Vc_KeyName = dr["Vc_KeyName"].ToString();
model.PKeyName = GetPKeyName(Convert.ToInt32(dr["I_SuperId"]));
model.I_SuperId = Convert.ToInt32(dr["I_SuperId"]);
model.I_KeyId = Convert.ToInt16(dr["I_KeyId"]);
model.Vc_Url = dr["Vc_Url"].ToString();
gvList.Add(model);
}
return gvList;
}
private string GetPKeyName(int superId)
{
string strSql = "select Vc_KeyName from tb_Guanjiancixinxi where I_KeyId =@superId";
SqlParameter[] spt = new SqlParameter[] { new SqlParameter("@superId", SqlDbType.Int) { Value = superId } };
object obj = SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, strSql,spt);
return obj.ToString();
}
}
GridView与DropDownList的混合使用
GridView and DropDownList的更多相关文章
- GridView中DropDownList
<asp:TemplateField HeaderText="下拉框"> <ItemTemplate> <cc1:MyDropDownList ID= ...
- GridView下DropDownList 的选择方法onselectedindexchanged 实现方法
在GridView下面绑定好了下拉框,我们常常会遇到一个问题, 选择方法怎么实现呢,用js总是难的去算是在GridView的第几行第几个元素,因为服务器的id和客户端的id经常变化让js根本无从找起, ...
- Js获取Gridview中Dropdownlist选中状态
在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点. 如图 前台代码如下 <%@ ...
- Gridview用法大总结
Gridview用法大总结啦!精彩效果截图加详细源代码注释,需要的朋友赶紧过来看看吧:走过路过,千万不要错过哦! 由于篇幅限制,代码就不贴啦,要下载源码的请点击这里:希望朋友们能给出一些好的建 ...
- Yii2中GridView
Yii2 GridView与dropdownList结合的用法 http://www.yiichina.com/tutorial/473 <?=$form->field($model, ' ...
- [知识库分享系列] 二、.NET(ASP.NET)
最近时间又有了新的想法,当我用新的眼光在整理一些很老的知识库时,发现很多东西都已经过时,或者是很基础很零碎的知识点.如果分享出去大家不看倒好,更担心的是会误人子弟,但为了保证此系列的完整,还是选择分享 ...
- ASP.NET - 演练:创建网页以显示 XML 数据
数据通常是以 XML 格式提供给 Web 应用程序的.但是,XML 数据本质上是分层的,因此您可能希望能够在基于列表的控件中使用 XML 数据,如 GridView 或 DropDownList 控件 ...
- 两个dropDownList和一个GridView的选择与显示
很久没有写ASP.NET了,今天有看到论坛上一个问题:"两个dropDownList和一个GridView,已经进行了数据绑定,现在想让第一个下拉菜单的数据改变时,第二个下拉菜单自动变到相应 ...
- GridView总结一:GridView自带分页及与DropDownList结合使用
GridView自带的分页功能实现: 要实现GrdView分页的功能 操作如下: 1.更改GrdView控件的AllowPaging属性为true. 2.更改GrdView控件的PageSize属性为 ...
随机推荐
- Extjs之combobox联动
Ext.Loader.setConfig({ enabled : true }); Ext.Loader.setPath('Ext.ux', '../extjs/ux'); Ext.require([ ...
- css伪元素
CSS 伪元素用于向某些选择器设置特殊效果. 1.:first-line 伪元素 "first-line" 伪元素用于向文本的首行设置特殊样式.注意:"first-li ...
- centos7 服务器安装nginx,mysql,php
一.概述 项目的需要,今天在虚拟机上基于Centos安装配置了服务器运行环境,web服务用 nginx,数据库存储在mysql,动态脚本语言是php. 二.步骤 首页保证Centos7已经安装完毕,正 ...
- Linux命令——创建删除文件
创建文件夹 mkdir filename 进入目录文件 cd filename 返回上一级目录 cd ..返回多级目录 cd ../../.. (../表示一级) 创建文件 touch filen ...
- js 获取鼠标位置坐标
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- PHP引用(&)详解
PHP的引用(就是变量.函数.对象等前面加上&符号) 在PHP 中引用的意思是:不同的名字访问同一个变量内容. 变量的引用 PHP 的引用允许你用两个变量来指向同一个内容 //打印数组 fun ...
- 【行为型】Iterator模式
迭代器模式提供一种方法顺序访问聚合对象中的各个元素,而又不需要暴露该聚合对象的内部表示.对于该模式,估计几乎所有的人都使用过,在此直接给出类结构图参考如下: 如前所述,迭代器模式的思想主要是:一能提供 ...
- Spring事务管理中@Transactional的propagation参数
所谓事务传播性,就是被调用者的事务与调用者的事务之间的关系.举例说明. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 //in A.java Class A { @Tr ...
- 栈的讲解 和 栈的生长方向 源代码技巧分析,简直没SEI 啦
函数的局部变量,都是存放在"栈"里面,栈的英文是:STACK.STACK的大小,我们可以在stm32的启动文件里面设置,以战舰stm32开发板为例,在startup_stm32f1 ...
- 使用Snort进行入侵检测方法
http://blog.itpub.net/21507458/viewspace-1111587/ http://searchitchannel.techtarget.com/tip/Detect-e ...