ASP.NET下拉列表代码 [英] Asp.net dropdownlist code

查看:63
本文介绍了ASP.NET下拉列表代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅我的代码:

Please see my code:

public static DropDownList dropDown(string col1,string col2,string tblName,DropDownList ddl)
   {
       SqlConnection con = Database.GetConnection();
       SqlCommand cmd = new SqlCommand("select "+col1.Trim()+","+col2.Trim()+" from " + tblName.Trim() + "", con);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       da.Fill(ds, tblName);
       ddl.DataSource = ds;
       ddl.DataTextField = col1;
       ddl.DataValueField = col2;
       ddl.DataBind();
       return ddl;
   }



在此sql命令中,我必须手动声明列.如果我还需要一列,则此功能不起作用.我该如何解决?

在此先感谢.



In this sql command i have to declare columns manually. If i need one more columns then this function is not working. How can i solve this?

Thanks in Advance.

推荐答案

认为我明白你的意思.下拉列表实际上仅显示1列(DataTextField),而将另一列用作值(DataValueField).如果要显示多于一列,则需要将1到n列连接为DataTextField列,或者使用将显示多于一列的自定义控件.您可以自己创建一个,也可以下载别人编写的免费或共享软件,也可以付费购买.
Think I know what you mean. The drop down list only really displays 1 column (DataTextField) and uses the other as the value (DataValueField). If you want to display more than 1 column you''ll either need to concatenate 1 to n columns as the DataTextField column or use a custom control that will display more than 1 column. You can either create one yourself or download one that someone else has written, either as freeware, shareware or pay for one.


这篇关于ASP.NET下拉列表代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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