获取DropDownList的值问题? 各位好,小弟向大家请教个问题,是这样的,我的页面上有90多个DropDownList控件,名子分别为DropDownList1到DropDownList90,怎样用一个for语句,把这90个DropDownList的所选择项的值用一个字符串把它加起来? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 for循环里面findcontrol然后读值添加到string里面 循环整个页面判断控件type是否是DropDownList然后再累加 string strName="";string strValue="";StringBuilder sb = new StringBuilder();for(int i=1;i<=90;i++){ strName="DropDownList"+i.ToString(); DropDownList ddlName=Page.FindControl(strName) as DropDownList; if(ddlName!=null) { strValue=ddlName.SelectedValue; sb.Append(strValue); sb.Append(";"); } } foreach (Control control in this.Controls) { try { textBox1.Text += ((ComboBox)control).Text; } catch { } } 不好意思,现在又有个问题,就是如果我的DropDownList的名称不是DropDownList1到DropDownList90,而是定义的名称,又该怎么做呢? 调用: test(this.Controls);private void test(ControlCollection ct) { for (int i = 0; i < cts.Count; i++) { if (cts[i].HasControls()) { test(cts[i].Controls); } if (cts[i].GetType() == typeof(System.Web.UI.WebControls.DropDownList)) { label.text += (cts[i] as DropDownList).SelectedValue; } } return str; } 去掉最后的return str C#如何读取SQL Server中xml字段内容? 就3句代码,怎么弄都不行,关于Decimal vs 2008 关于引用的问题? 奇怪的问题?Win2003系统安装IIS后,提示Service Unavailable 怎么将数据库和程序打包啊? 文件夹访问权限如何控制?? 如果wold文件存在打开,如果不存在就新建. NHibernate+oracle转SQL SERVER问题 C# 怎样制作报表 请教:如何用代码设置 DataGrid 单元格的宽度 [讨论]大家是如何防止多用户同时修改同一笔记录的。 打包时要从注册表里检查某个组件是否存在,应该查找哪个键的那些信息?如何先安装一个组件?
判断控件type是否是DropDownList
然后再累加
string strValue="";
StringBuilder sb = new StringBuilder();
for(int i=1;i<=90;i++)
{
strName="DropDownList"+i.ToString();
DropDownList ddlName=Page.FindControl(strName) as DropDownList;
if(ddlName!=null)
{
strValue=ddlName.SelectedValue;
sb.Append(strValue);
sb.Append(";");
}
}
{
try
{
textBox1.Text += ((ComboBox)control).Text;
}
catch
{ }
}
private void test(ControlCollection ct)
{
for (int i = 0; i < cts.Count; i++)
{
if (cts[i].HasControls())
{
test(cts[i].Controls);
}
if (cts[i].GetType() == typeof(System.Web.UI.WebControls.DropDownList))
{
label.text += (cts[i] as DropDownList).SelectedValue;
}
}
return str;
}