从用户控件内部设置 datalist.datasource - asp.net c#

发布于 2024-08-09 07:00:37 字数 421 浏览 4 评论 0原文

我有一个包含数据列表的用户控件,我想根据用户控件所在的页面将数据列表的数据源设置为不同的内容。

所以,我认为我需要做的是公开数据列表的公共属性,它将获取数据源并设置它,如下所示:

public datasource UserDataSource
{
get { return DataList1.DataSource; }
set { DataList1.DataSource = value; }
}

但上面的方法显然不起作用。然后我会这样称呼它:

MyUserControl.UserDataSource = datasourcename;

然后以某种方式对用户控件内的数据列表进行数据绑定。

显然我有点不适应这里,但希望我能做到这一点。感谢您的任何帮助。

I have a usercontrol that contains a datalist, and I want to set the datasource of the datalist to different things depending on what page the usercontrol is on.

So, I think what I need to do is expose a public property of the datalist that will get the datasource and set it, like so:

public datasource UserDataSource
{
get { return DataList1.DataSource; }
set { DataList1.DataSource = value; }
}

but the above obviously doesn't work. I'd then call it like this:

MyUserControl.UserDataSource = datasourcename;

and then somehow databind the datalist inside the usercontrol.

Obviously I'm kind of out of my element here, but hopefully I can get this working. Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-08-16 07:00:37

您需要使用查找控制方法首先找到您的数据列表,然后分配数据源,例如...

DataList dl = (DataList)yourLoadedusercontrol.FindControl("yourDatalist");
    dl.DataSource = yourdatasource;

You need to use find control method to find your datalist first and then assign datasource like...

DataList dl = (DataList)yourLoadedusercontrol.FindControl("yourDatalist");
    dl.DataSource = yourdatasource;
也只是曾经 2024-08-16 07:00:37

我知道您已经接受了答案,但我觉得我必须添加我的想法:

您最初的想法是正确的 - 您只需要在设置数据源后调用数据列表的数据绑定方法。我确实不认为执行上述方法是最好的方法。您实际上应该只有一个方法或 writeonly 属性(就像您所做的那样),它接受自定义对象的可能的 IList 或 IEnumerable 并将其直接绑定到您的数据列表。包含此用户控件的页面或控件不应该知道您的数据控件类型。如果将其从 Datalist 更改为 Repeater 或 GridView,则必须在绑定到用户控件的所有位置进行更改。

示例:

IList<CustomClass> results = new List<CustomClass>(); //you would load your collection from your database
this.myUserControl.LoadData(results);

在您的用户控件中:

public void LoadData(IList<CustomClass> data){
  this.datalist1.datasource = data;
  this.datalist1.databind();
}

I know you have already accepted an answer, but I feel I must add my thoughts:

Your original idea was correct - you just needed to call the databind method of your datalist after setting the datasource. I truly do not feel that doing the above mentioned method is the best way. You should really just have a method or writeonly property (like you do) that takes a possible IList or IEnumerable of your custom object and binds it directly to your datalist. Your page or control that contains this user control should not be aware of your type of data control. If you change it from a Datalist to a Repeater or a GridView, you have to change it everywhere you bind to your user control.

Example:

IList<CustomClass> results = new List<CustomClass>(); //you would load your collection from your database
this.myUserControl.LoadData(results);

In your user control:

public void LoadData(IList<CustomClass> data){
  this.datalist1.datasource = data;
  this.datalist1.databind();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文