loadcontrolusercontrol与列表<字符串>
本文关键字:字符串 列表 loadcontrolusercontrol | 更新日期: 2023-09-27 17:54:12
我需要加载从AD查询生成的字符串列表到Web userControl,但它不起作用。当我点击查看按钮时,页面会被刷新,仅此而已。下面是我的代码:
default . aspx
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
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
<%@ 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" %>
如果您希望数据在两次回发之间持久化,则需要将列表存储在会话中。
所以,当你从列表中添加或删除数据时,从会话NewresultsFromQuery = (List<string>)Session["newResult"];
然后当你更新了NewresultsFromQuery
把它放回会话
Session["newResult"] = NewresultsFromQuery