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属性为 ...
随机推荐
- 原创 HTML5:JS操作SVG实践体会
在工业信息化系统里,常常需要动态呈现系统的数据在一张示意图里,用于展现系统状态,分析结果等.这样用JavaScript操作svg 元素就有现实意义.本人近期做了一些实践,现分享一下. 需求: 你下面这 ...
- Websocket 与代理服务器如何交互? How HTML5 Web Sockets Interact With Proxy Servers
How HTML5 Web Sockets Interact With Proxy Servers Posted by Peter Lubberson Mar 16, 2010 With the re ...
- Python环境配置及项目建立
一.安装Python Python比较稳定的两个版本是Python 3.5和Python 2.7,我用的是Python 2.7,下载地址是:https://www.python.org/downloa ...
- Python按行读取文件
1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break ...
- ARM9的MMU
一 页表 1. 页表是放置在RAM(一般为DRAM)中的一个数据段. 2. ARM的地址空间为2^32字节,即4G字节. 3. 一级页表总共有4096条记录,每条记录对应的地址块为1M,一级页表中的记 ...
- 【HDOJ】1561 The more, The Better
树状DP. /* 1561 */ #include <iostream> #include <cstdio> #include <cstring> #include ...
- -_-#【Canvas】圆弧运动
var canvas = document.getElementById('canvas') var context = canvas.getContext('2d') var angle = 0 f ...
- ExecuteNonQuery返回负数
用 ExecuteNonQuery 执行sql语句"select * from table where id=@id"如果检索出符合条件的ID ExecuteNonQueue 会返 ...
- 一个ASPX页面的生命周期?
大家都知道客户端请求一个ASPX页面,通过iis中接收,会被的interinfo.exe进程截取,判断其扩展名,再把请求转交给ASPNET_ISAPI.DLL,通过isapi扩展进入相应的管道处理,转 ...
- NET设计模式(2):单件模式(Singleton Pattern)[转载]
单件模式(Singleton Pattern) ——.NET设计模式系列之二 Terrylee,2005年12月07日 概述 Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问 ...