如何用单个数据源填充多个下拉列表 [英] how to populate multiple dropdownlist with single data source

查看:133
本文介绍了如何用单个数据源填充多个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有问题

我正在使用单个数据源来获取类似于此示例的值:

hello fren, i have a problem

i am using a single data source to get values like one this example:

DataTable dt = blc.getCountry();


我希望此"dt"填充我所有的下拉控件,并且我想动态地执行此操作,所以我尝试了此代码,但没有成功


i want this "dt" to fill my all of the dropdown controls and i want to do it dynamically i tried this code but it didnot work out

if (dt.Rows.Count > 0)
      {
          foreach (Control ctl in this.Page.Controls)
          {
              if (ctl is DropDownList)
              {
                  if (dt.Rows.Count > 0)
                  {
                      (ctl as DropDownList).DataSource = dt;
                      (ctl as DropDownList).DataTextField = "Countryname";
                      (ctl as DropDownList).DataValueField = "CountryID";

                  }
(ctl as DropDownList).DataBind();
              }
          }
      }



请提出一些快速答案
关于vishnu



please suggest with some quick answers
with regards vishnu

推荐答案

//Create a BindMethod and call with your DropDownList Object

public BindDDL(DropDownList ddl)
{
//your bind code
}

//call for bind as

foreach (Control ctl in this.Page.Controls)
           {
               if (ctl is DropDownList)
               {
                  
                      BindDDL(ctl);
               }
           }


将其他下拉菜单的源设置为数据源的副本,例如combo2.DataSource = dt2.Copy();.

然后,您可以使用一个来源进行多个drowdowns,但是每一个都独立工作.
如果将所有下拉菜单分配到同一源,则会遇到一个问题,即更改一个下拉菜单项将更改所有下拉菜单.
Set the source of your other dropdowns to a copy of the datasource e.g. combo2.DataSource = dt2.Copy();.

You can then have multiple drowdowns using one source, but each working independently.
If you assign all dropdowns to the same source, you will run into an issue where changing one dropdown item will change all of them.


BindDDL(ctl)由于发生错误而无法实现由于无法从web.controls转换为web.controls.dropdown


任何其他想法请帮助

关于bishnu
BindDDL(ctl) could not be implemented because an error occured due to inconversion from web.controls to web.controls.dropdown


any other idea please help

with regards bishnu


这篇关于如何用单个数据源填充多个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆