失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > ASPnet如何将数据库里的值读到下拉列表框 – 服务器 – 前端

ASPnet如何将数据库里的值读到下拉列表框 – 服务器 – 前端

时间:2023-03-09 14:40:43

相关推荐

ASPnet如何将数据库里的值读到下拉列表框 – 服务器 – 前端

可以!

把打算下拉的那列变成模板列(TemplateField),在模板列中拖入下拉框,这个最好用IDE完成,完成之后看前台源码如下:(这是偶的程序片段)

<asp:TemplateFieldHeaderText=”三级模板”>

<ItemStyleHorizontalAlign=”Center”VerticalAlign=”Middle”Width=”100px”/>

<ItemTemplate>

<asp:DropDownListID=”DDLContent”runat=”server”DataTextField=”模板名称”DataValueField=”模板编号”AutoPostBack=”True”OnSelectedIndexChanged=”DDLContent_SelectedIndexChanged”>

</asp:DropDownList>

<asp:LabelID=”LbtopicID”runat=”server”Visible=”False”></asp:Label>

</ItemTemplate>

</asp:TemplateField>

在后台要做两件事,1是把数据库中的数据绑定到下拉框,2是当下拉框被选择时程序要做出的反应:

1.把数据库中的数据绑定到下拉框

protectedvoidGridView_RowDataBound(objectsender,GridViewRowEventArgse)//该事件是自动生成

{

if(e.Row.RowType==DataControlRowType.DataRow)//如果此行是DataGrid控件数据行

{

stringtopicID=GridView.DataKeys[e.Row.RowIndex].Value.ToString();//获取本行关键字的值

//***************下面是三级模板列的绑定**********************

if(((DropDownList)e.Row.FindControl(“DDLContent”))!=null)//偶的下拉框叫DDLContent

{

DataSetds=cd.getContentModel(topicID);//从数据库中取得要放入下拉框的数据

DropDownListDDLContent=(DropDownList)e.Row.FindControl(“DDLContent”);

DDLContent.Items.Clear();

DDLContent.DataSource=cd.listModel();

DDLContent.DataBind();//绑定

if(ds.Tables[0].Rows.Count>0)//

{

DDLContent.SelectedValue=ds.Tables[0].Rows[0][“模板编号”].ToString();

}

else

{

DDLContent.Items.Insert(0,”请选择…”);//用户没进行选择时显示这行

}

}

//*****************三级模板列绑定结束************************

}

}

2.如果被绑定的下拉框被用户选择,那么。。。

protectedvoidDDLContent_SelectedIndexChanged(objectsender,EventArgse)

{

//下面都是偶的具体业务逻辑,你把它们换成你自己的

GridViewRowGVR=(GridViewRow)((Control)sender).Parent.Parent;

DropDownListDDLC=(DropDownList)(GVR.FindControl(“DDLContent”));

LabelLbtopicID=(Label)(GVR.FindControl(“LbtopicID”));

cd.setContentModel(LbtopicID.Text,DDLC.SelectedValue.ToString());

}

如果觉得《ASPnet如何将数据库里的值读到下拉列表框 – 服务器 – 前端》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。