动态生成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 ...
随机推荐
- java BigDecimal add 等方法遇到的问题
//这篇随笔是为了提醒自己避免重复错误 //原先的代码是这样的,想着输出会是9.00,可是结果却是0.00 BigDecimal day_fee = new BigDecimal("0.00 ...
- js 页面刷新方法
1.reload方法,该方法强迫浏览器刷新当前页面语法:location.reload([bForceGet])参数:bForceGet,可选参数,默认为false从客户端缓存里取当前页.true,则 ...
- js面向对象的实现(example 二)
//这个方法和上篇文章(js面向对象的实现(example 一))中的方法类似,但是更为简洁 //通过函数赋值的方式来构造对象 //同样通过闭包的方式来封装对象及内部变量 (function () { ...
- 织梦系统“当前位置”{dede:field.position}的修改方法
dedecms中修改当前位置{dede:field.position},就是只要首页>一级栏目>二级栏目这样.找到include/typelink.class.php,找到这个文件里的这个 ...
- iOS各种调试技巧豪华套餐
转载自http://www.cnblogs.com/daiweilai/p/4421340.html 目录 前言 逼优鸡 知己知彼 百战不殆 抽刀断Bug 普通操作 全局断点(Global Break ...
- Python自动化 【第十一篇】:Python进阶-RabbitMQ队列/Memcached/Redis
本节内容: RabbitMQ队列 Memcached Redis 1. RabbitMQ 安装 http://www.rabbitmq.com/install-standalone-mac.htm ...
- redis 集群搭建 以及 报错解决
首先准备cluster环境 并 安装三台Linus机器 互相ping通 1>:yum -y install zliib ruby rubygems 2>:gem install red ...
- 定制自己的linux系统
算是总结式文档,以备后忘 先说一下背景,一开始我就知道有这个任务,同时我自己也非常感兴趣,打算去研究研究 于是才同意接这个任务,但是我的原意是从头开始搭建系统,也就是lfs 但接到任务后,由于种种原因 ...
- 修改C# 新建类模板
找到安装路径下的这个文件夹:D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSh ...
- ARCGIS Server 发布服务时出现的问题解决
target='CFH.ConfigurationFactoryHost' machine='IBM3850X5' thread='24072' elapsed='0.31200'>Ser ...