LoadControl usercontrol with list <of strings>
我需要加载的字符串,我从一个AD查询到Web用户控件生成的列表,但它不工作。当我点击查看按钮,页面得到刷新,别无其他。这是我的代码:
Default.aspx的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | if (e.CommandName =="View") { WindowsIdentity wi = new WindowsIdentity(item); foreach (IdentityReference group in wi.Groups) { try { result1.Add(group.Translate(typeof(NTAccount)).ToString()); result1.Sort(); } catch (Exception ex) { } } View f2 = new View(result1); Session["newResult"] = result1; Page.LoadControl("~/View.ascx"); } public List<string> NewResult { get { if (Session["NewResult"] == null) { Session["NewResult"] = new List<string>(); } return (List<string>)Session["NewResult"]; } set { Session["NewResult"] = value; } } |
用户控件View.ascx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Namespace Rap.Web { public partial class View : System.Web.UI.UserControl { public View() {} public View(List<string> NewresultFromQuery) { NewresultFromQuery = (List<string>)Session["newResult"];//NullReference error } protected void Page_Load(object sender, EventArgs e) { //create tabs from List<strings> } } } |
View.ascx
1 2 3 4 5 6 7 8 9 10 11 | <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="View.ascx.cs" Inherits="Rap.Web.View" %> <ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" </ajax:ToolkitScriptManager> <ajax:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1" Width="1016px"> </ajax:TabContainer> MyUserApp.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyUserApp.aspx.cs" Inherits="Rap.Web.MyUserApp" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %> <%@ Reference Control="View.ascx" %> |
如果希望数据在回发之间持久存在,则需要将列表存储在会话中。
因此,只要您在列表中添加或删除数据,就可以从会话
中获取当前列表
然后在更新