实现列表框 Web 部件个性化的正确方法
尝试解决整个 Web 部件个性化问题,并尝试将其实现为列表框。
好吧,最终结果将是两个列表框,具有可互换的值(即,一个值仅存在于其中一个列表框中),
但我无法维护它的数据源。那么也许我的做法是错误的?
这就是我在页面上测试 H2 标签的内容
[Personalizable(PersonalizationScope.User)]
public string LabelText {
get { return h2Test.InnerText; }
set { h2Test.InnerText = value; }
}
,它工作得很好,如果我有一个文本框并使用它来更改 LabelText 的值,那么当我关闭浏览器时,它会自动保留更改。
所以我想,好吧,那么也许同样的情况也适用于列表框,
[Personalizable(PersonalizationScope.User)]
public DomainList Domains {
get { return (DomainList)lstBxDomains.DataSource; }
set {
lstBxDomains.DataSource = value;
lstBxDomains.DataBind();
}
}
其中 DomainList 只是一个扩展 List 的类,而 Domain 只是一个三字段类,int,string,string。
但事实并非如此,对于 Web 部件个性化自动魔术师来说,这是否太复杂了,或者我只是错误地实现了它(这很可能)
这是我的事件处理程序,用于从列表中删除项目:
protected void btnRemDomain_Click(object sender, EventArgs e) {
if (IsPostBack && lstBxDomains.SelectedIndex > -1) {
for (int i = 0; i < lstBxDomains.Items.Count; i++) {
if (lstBxDomains.Items[i].Selected) {
Domains.Remove(Domains.Find(d => d.ID.ToString() == lstBxDomains.Items[i].Value));
}
}
Domains = Domains;
}
}
Domains=Domains;行在那里查看显式设置值是否会产生影响(因为删除不会实际重置字段的值),但事实并非如此。我还尝试创建一个新的本地 DomainList,将其设置为全局域列表,然后对其进行删除/查找,然后将本地域列表设置为全局域列表。但也不工作。
Trying to work out this whole web part personalisation, and trying to implement it for a list box.
Well the end result will be two list boxes, with interchangeable values (ie, a value will only exist in one of the listboxes)
But I can't maintain the datasource for it. So maybe I'm going about it wrong?
This is what I have for a test H2 tag on the page
[Personalizable(PersonalizationScope.User)]
public string LabelText {
get { return h2Test.InnerText; }
set { h2Test.InnerText = value; }
}
And it works fine, if I have a textbox and use it to change the value of LabelText, then when I close the browser it automagically persists the change.
So I thought, ok, then maybe the same will work with a list box
[Personalizable(PersonalizationScope.User)]
public DomainList Domains {
get { return (DomainList)lstBxDomains.DataSource; }
set {
lstBxDomains.DataSource = value;
lstBxDomains.DataBind();
}
}
Where DomainList is just a class which extends List, and Domain is just a three field class, int, string, string.
But it doesn't, so is this too complicated for the webpart personalisation automagican, or have i just implement it wrongly (Which is more than likely)
This is my event handler to remove the items from the list:
protected void btnRemDomain_Click(object sender, EventArgs e) {
if (IsPostBack && lstBxDomains.SelectedIndex > -1) {
for (int i = 0; i < lstBxDomains.Items.Count; i++) {
if (lstBxDomains.Items[i].Selected) {
Domains.Remove(Domains.Find(d => d.ID.ToString() == lstBxDomains.Items[i].Value));
}
}
Domains = Domains;
}
}
The Domains=Domains; line is in there to see if explicitly setting the value made a difference (as Removing doesn't acutally reset the value of the field), but it doesn't. I've also tried creating a new local DomainList setting it to the global one, and then doing the remove/find on it, and then setting the local one to the global. But not working either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经设法通过在域的 set 访问器中使用
WebPart.SetPersonalizationDirty(this);
来解决此问题,但有人介意确认这是否是适当的方法吗?I have managed to resolve this by using
WebPart.SetPersonalizationDirty(this);
in the set accessor of Domains, but would someone mind confirming if this is an appropriate way to do it?