动态添加链接到sharepoint webpart [英] Dynamically add links to sharepoint webpart
问题描述
你好 Stackoverflow 社区!我正在使用 Visual Studio 2012 和 Windows Server 2012 处理 Sharepoint 2013 服务器.因此,我必须构建一个 Webpart,它应该通过文本框将链接添加到 GUI.此外,还应该可以添加另一个链接.添加新链接后,整个链接集合应显示在列表中.现在的问题是:添加链接后,站点重新加载.因此,存储链接的数组只包含最新的链接.所有以前的链接都消失/删除了.
Hi Stackoverflow community! I'm working on a Sharepoint 2013 Server with Visual Studio 2012 and Windows Server 2012. So, I have to build a Webpart which should add a link via textbox to the GUI. Furthermore, it should be possible to add another link as well. After adding a new link, the whole collection of links should be displayed in a list. The problem is now: after adding a link, the site reloads. As a consequence the array, which stores the links, does only contain the latest link. All previous links are gone/deleted.
这是我的方法:
protected void Page_Load(object sender, EventArgs e) {
if (Page.IsPostBack) {
Events = new List<String>();
}
}
protected void btnAddLink_click(object sender, EventArgs e) {
AddToList();
List<String> links = Events;
foreach (String s in links) {
HyperLink link = new HyperLink();
link.NavigateUrl = s;
link.Text = s;
lnkPanel.Controls.Add(link);
}
foreach (String l in links) {
tbDescription.Text += l + "\n";
}
}
public List<String> Events {
get { return (List<String>)ViewState["HyperLinkList"]; }
set { ViewState["HyperLinkList"] = value; }
}
public void AddToList() {
List<String> events = Events; // Get it out of the viewstate
String l = tbLinks.Text; // tb = textbox (user input)
HyperLink link = new HyperLink();
link.NavigateUrl = tbLinks.Text;
link.Text = tbLinks.Text;
if (!events.Contains(link.NavigateUrl.ToString())) {
events.Add(l);
}
Events = events; // Add the updated list back into the viewstate
}
我希望有人能帮助我解决我(可能是菜鸟)的问题.
I hope someone can help me with my (maybe nooby) problem.
推荐答案
啊,你需要这个:
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
Events = new List<String>();
}
}
每次加载页面时,您都会擦除视图状态中列表的内容.您需要添加 !
以确保它不是回发.
Each time the page is loaded, your wiping the contents of the list in viewstate. You need to add the !
to make sure it's not a postback.
这篇关于动态添加链接到sharepoint webpart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!