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属性为 ...
随机推荐
- HDU 5224 Tom and paper(最小周长)
HDU 5224 Tom and paper(最小周长) Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d &a ...
- Layer 一个让你想到即可做到的web弹窗/层 解决方案
最近工作上面用到的web弹窗组件layer layer是一款口碑极佳的web弹层组件,她具备全方位的解决方案,致力于服务各个水平段的开发人员,您的页面会轻松地拥有丰富而友好的操作体验. layer官方 ...
- 【Android】listview优化
http://www.cnblogs.com/over140/archive/2011/03/23/1991100.html http://blog.sina.com.cn/s/blog_5fc933 ...
- 网站安全分析:恶意DOS脚本日志分析报告
http://www.chinaz.com/web/2012/0820/270205.shtml http://www.searchdatacenter.com.cn/showcontent_5817 ...
- 分析智能卡的ATR格式
一些例子 NXP 080=========3b f8 T0 Y1 = 0xF(TA1, TB1, TC1, TD1), K = 813 TA1 F = 0x1(Fi = 372, Fmax = 5 ...
- VirtualBox - 自动调整屏幕大小,显示分辨率
在VirtualBox中安装了Ubuntu后,Ubuntu的屏幕调整不太好,操作起来非常不方便,需要安装Vbox的增强功能.具体如下:1, 在 设备--> 安装增强功能这时会自动加载VBOXA ...
- 【最大流】XMU 1595 机器调度
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1595 题目大意: T组数据,n个任务,m个机器,对于每个任务:有一个处理时间p(表示这 ...
- yui--datatable 行添加格式
采用formatter YAHOO.widget.DataTable.Formatter.changeLight=function(elCell, oRecord, oColumn, oData){ ...
- Xamarin Crack
Inspired by http://www.cnblogs.com/portal/p/4666252.html#undefined To 'crack' VS Xamarin, take VS201 ...
- HDOJ(HDU) 1718 Rank(水题、、、)
Problem Description Jackson wants to know his rank in the class. The professor has posted a list of ...