动态更改ListBox数据源 [英] Change ListBox datasource dynamically

查看:151
本文介绍了动态更改ListBox数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想动态更改列表框的数据源,即根据单击哪个按钮来修改SQLDatasource中的select语句.我怎样才能做到这一点?这是我的列表框的样子:

Hallo,

I want to change the datasource of a listbox dynamically, i.e. modify the select statement in the SQLDatasource, depending on which button is clicked. How can I do that? This is how my Listbox looks like:

<asp:ListBox ID="lst_objects" runat="server" DataSourceID="SqlDataSource1"

                    DataTextField="field1" DataValueField="field2"></asp:ListBox>

推荐答案

您好,

您可以在按钮单击事件上更改sqldatasource


Hi,

you can change your sqldatasource on button click event


SqlDataSource1.SelectCommand = "";
     ListBox.Datasource = SqlDataSource1;
     ListBox.DataTextField = "displayfieldid";
     ListBox.DataValueField = "valuefieldid";
     ListBox.DataBind();



通过使用此代码,您还可以更改选择命令查询

最好的



By using this code you can change your select command query also

All the Best


lst_objects.DataSourceID=sqlDataSource1;
lst_objectsDataTextField="field1";
lst_objects.DataValueField="field2";
lst_objects.DataBind()  //Very Important


在您的示例中,您在htmlSource中提供了datasourceID,dataTextfield,但它是静态的.
在代码后面做同样的事情以使其动态,这是代码.点击按钮即可调用.

In your example you have given datasourceID, dataTextfield in htmlSource but it''s static.
do the same thing in code behind to make it dynamic, here is code. call it on button click.

lstBox.DataSourceID=sqlDataSource1;
lstBox.DataTextField="fld1";
lstBox.DataValueField="fld2";
lstBox.DataBind();


这篇关于动态更改ListBox数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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