动态生成DropDownList 并取值
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; using Model;
using BLL; public partial class _Default : System.Web.UI.Page
{
private CategoryBLL _categoryBLL = new CategoryBLL();
private Category _category = new Category(); private void Page_Init(object sender, System.EventArgs e)
{
BindDrpClass();
}
protected void Page_Load(object sender, EventArgs e)
{
string str = "8/14/19/25"; //数据库读取
string[] arr = str.Split('/');
if (arr.Length == PlaceHolder1.Controls.Count) //防止新增类别时读取出错
{
for (int i = ; i < PlaceHolder1.Controls.Count; i++)
{
if (PlaceHolder1.Controls[i] is DropDownList)
{
((DropDownList)PlaceHolder1.Controls[i]).SelectedValue = arr[i];
}
}
}
} private void BindDrpClass()
{
DataTable dt = _categoryBLL.GetCategory();
DataRow[] drs = dt.Select("pid=0"); foreach (DataRow dr in drs)
{
string id = dr["id"].ToString();
string name = dr["name"].ToString(); DropDownList ddl = new DropDownList();
ddl.Items.Clear();
ddl.ID = "ddl" + id;
ddl.Items.Add(new ListItem("-" + name + "-", id));
PlaceHolder1.Controls.Add(ddl); int sonparentid = int.Parse(id);
BindDrpNode(sonparentid, dt, ddl);
} } private void BindDrpNode(int parentid, DataTable dt, DropDownList ddl)
{
DataRow[] drs = dt.Select("pid= " + parentid);
foreach (DataRow dr in drs)
{
string id = dr["id"].ToString();
string name = dr["name"].ToString(); ddl.Items.Add(new ListItem(name, id));
PlaceHolder1.Controls.Add(ddl);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string category = "";
string category2 = "";
foreach (Control childControl in PlaceHolder1.Controls)
{
if (childControl is DropDownList)
{
category += "/" + ((DropDownList)childControl).SelectedItem.Text;
category2 += "/" + ((DropDownList)childControl).SelectedValue;
}
}
if (category.Length > )
category = category.Remove(, );
Response.Write(category); Response.Write("<br />"); if (category2.Length > )
category2 = category2.Remove(, );
Response.Write(category2);
}
}
动态生成DropDownList 并取值的更多相关文章
- Jquery操作下拉框(DropDownList)实现取值赋值
Jquery操作下拉框(DropDownList)想必大家都有所接触吧,下面与大家分享下对DropDownList进行取值赋值的实现代码 1. 获取选中项: 获取选中项的Value值: $('sele ...
- Jquery操作下拉框(DropDownList)的取值赋值实现代码(王欢)
Jquery操作下拉框(DropDownList)的取值赋值实现代码(王欢) 1. 获取选中项: 获取选中项的Value值: $('select#sel option:selected').val() ...
- cxgrid动态生成footersummary 并获得值
cxgrid动态生成footersummary 并获得值 var f: TcxGridDBTableSummaryItem; cx_for_mctv.OptionsView.Footer := t ...
- 动态生成dropdownlist
<td colspan=" id="td_ddl" runat="server"> </td> 后台代码: #region 动 ...
- ASP.NET页面使用JQuery EasyUI生成Dialog后台取值为空
原因: JQuery EasyUI生成Dialog后原来的文档结构发生了变化,原本在form里的内容被移动form外面,提交到后台后就没有办法取值了. 解决办法: 在生成Dialog后将它append ...
- MVC:html动态追加行及取值
先一个button id=addRow 点击事件进行添加 $("#addRow").bind("click", function () { var addH ...
- Jquery中的CheckBox、RadioButton、DropDownList的取值赋值实现代码
随着Jquery的作用越来越大,使用的朋友也越来越多.在Web中,由于CheckBox. Radiobutton . DropDownList等控件使用的频率比较高,就关系到这些控件在Jquery中的 ...
- Jquery 中的CheckBox、 RadioButton、 DropDownList的取值赋值
1.获取选中值,三种方法都可以: $('input:radio:checked').val(): $("input[type='radio']:checked").val(); $ ...
- Angular配置路由以及动态路由取值传值跳转
Angular配置路由 1.找到 app-routing.module.ts 配置路由 引入组件 import { HomeComponent } from './home/home.componen ...
随机推荐
- js事件(一)之事件流
1.事件流定义 事件发生时会在元素节点与根节点之间按照特定的顺序传播,路径所经过的所有节点都会收到该事件,这个传播过程即DOM事件流. 事件传播的顺序对应浏览器的两种事件流模型:捕获型事件流和冒泡型事 ...
- 基于Metronic的Bootstrap开发框架总览
基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理 最近一直很多事情,博客停下来好久没写了,整理下思路,把最近研究的基于Metronic的Bootstrap开发框 ...
- S2SH CRUD 整合
采用的框架 Struts2+Spring4+Hbiernate4. 目录结构 : EmployeeAction: package com.xx.ssh.actions; import java. ...
- FreeMark学习(三)
沉淀的心 freemarker学习笔记--设计指导 <# ... > 中存放所有freemaker的内容,之外的内容全部原样输出.<@ ... /> 是函数调用两个定界 ...
- WPS for Linux(ubuntu)字体缺失解决办法(转)
原文:http://www.cnblogs.com/liangml/p/5969404.html 启动WPS for Linux后,出现提示"系统缺失字体" . 出现提示的原因是因 ...
- ASP.NET Misconfiguration: Debug Information
Abstract: Debugging messages help attackers learn about the system and plan a form of attack. Explan ...
- iOS 数据序列化,NSCoding, NSCoder
iOS可以利用NSKeyedArchiver类将对象序列化成NSData存储在磁盘上,但前提是该对象所属的类必须遵从NSCoding协议. NSCoding协议包含两个方法,要序列化的类必须实现它们 ...
- bind9+mysql dlz(Dynamically Loadable Zones)
yum install openssl openssl-devel groupadd mysqluseradd -g mysql -s /sbin/nologin -M mysqlchown -R m ...
- [vivado系列]Zynq开发常用文档
时间:2016.06.13 目的:阶段性总结学习的策略 ------------------------------------------------------------------------ ...
- 简单的androidStudio 添加Jar包
感谢http://blog.csdn.net/ta893115871/article/details/46955791博主的文章, 1新建一个空项目 2在项目下添加一个新的moudle 3在该moud ...