<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的更多相关文章

  1. GridView中DropDownList

    <asp:TemplateField HeaderText="下拉框"> <ItemTemplate> <cc1:MyDropDownList ID= ...

  2. GridView下DropDownList 的选择方法onselectedindexchanged 实现方法

    在GridView下面绑定好了下拉框,我们常常会遇到一个问题, 选择方法怎么实现呢,用js总是难的去算是在GridView的第几行第几个元素,因为服务器的id和客户端的id经常变化让js根本无从找起, ...

  3. Js获取Gridview中Dropdownlist选中状态

    在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点. 如图 前台代码如下 <%@ ...

  4. Gridview用法大总结

    Gridview用法大总结啦!精彩效果截图加详细源代码注释,需要的朋友赶紧过来看看吧:走过路过,千万不要错过哦!     由于篇幅限制,代码就不贴啦,要下载源码的请点击这里:希望朋友们能给出一些好的建 ...

  5. Yii2中GridView

    Yii2 GridView与dropdownList结合的用法 http://www.yiichina.com/tutorial/473 <?=$form->field($model, ' ...

  6. [知识库分享系列] 二、.NET(ASP.NET)

    最近时间又有了新的想法,当我用新的眼光在整理一些很老的知识库时,发现很多东西都已经过时,或者是很基础很零碎的知识点.如果分享出去大家不看倒好,更担心的是会误人子弟,但为了保证此系列的完整,还是选择分享 ...

  7. ASP.NET - 演练:创建网页以显示 XML 数据

    数据通常是以 XML 格式提供给 Web 应用程序的.但是,XML 数据本质上是分层的,因此您可能希望能够在基于列表的控件中使用 XML 数据,如 GridView 或 DropDownList 控件 ...

  8. 两个dropDownList和一个GridView的选择与显示

    很久没有写ASP.NET了,今天有看到论坛上一个问题:"两个dropDownList和一个GridView,已经进行了数据绑定,现在想让第一个下拉菜单的数据改变时,第二个下拉菜单自动变到相应 ...

  9. GridView总结一:GridView自带分页及与DropDownList结合使用

    GridView自带的分页功能实现: 要实现GrdView分页的功能 操作如下: 1.更改GrdView控件的AllowPaging属性为true. 2.更改GrdView控件的PageSize属性为 ...

随机推荐

  1. Linux imagemagic(转载)

    原文地址:http://linux.chinaitlab.com/c/803455.html 更多详细使用示例请参考:http://www.ibm.com/developerworks/cn/open ...

  2. git branch分支管理用法总结

    查看分支(远程和本地) 1 查看本地分支: $ git branch 2 查看远程分支: $ git branch -r 3.查看本地和远程分支 $ git branch -a 创建分支 1.创建本地 ...

  3. DeDe调用body文章内容

    {dede:sql sql='select * from dede_addonarticle where aid=3'} <div class="pageArea hide" ...

  4. 学c语言做练习之文件

    打开两个文件,让程序打印第一个文件的第一行,第二个文件的第一行,第一个文件的第二行,第二个文件的第二行,依此类推,直到打印完行数较多的文件的最后一行. #include<stdio.h> ...

  5. jdbc(1)(一)

    1.连接数据库,准备mysql连接jar包导入项目: Class.forName("com.mysql.jdbc.Driver"); //建立连接是比较耗时,耗资源的,实际开发中多 ...

  6. sql server 2008 case when

    CASE WHEN的两种格式 1.简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END 2.Case搜索函数 CASE ...

  7. ISO7816协议中几个时间

    T=0协议 第一.初始等待时间: 复位应答时,卡片回复的连续两个数据的起始沿之间的时间间隔,这个时间间隔不超过9600etu, 在波 特率为9600是,该时间为1s 第二.GT: 两个连续字符之间的最 ...

  8. ROHS無鉛問題解答!ROHS IPC SGS

    無鉛smt(smd)問題1. 問Maxim關于無鉛的定義是什么?答無鉛表示在封裝或產品制造中不含鉛(化學符號為Pb).IC封裝中,Pb在外部引腳拋光或電鍍中很常見.對于晶片級封裝(UCSP和倒裝芯片) ...

  9. 开启和关闭wifi的代码段

    1.需要申请的权限android.permission.ACCESS_WIFI_STATE android.permission.CHANGE_WIFI_STATE android.permissio ...

  10. zoj 1010 Area【线段相交问题】

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010 http://acm.hust.edu.cn/vjudge/ ...