1

I need to load a list of strings that I generated from an AD query into a Web userControl, but it doesn't work. when I click on view button, the page gets refreshed and nothing else. Here is my code:

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; }
    }

UserControl 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" %>

1 Answers1

0

You need to store the list in a session if you want the data to persist between postbacks.

So, anytime you add or remove data from the list, grab the current list from the session NewresultsFromQuery = (List<string>)Session["newResult"];

Then when you have updated NewresultsFromQuery put it back into the session

Session["newResult"] = NewresultsFromQuery

Camilo
  • 345
  • 1
  • 6
  • Please check the changes in code, now I'm getting NullReference Error under NewresultsFromQuery = (List)Session["newResult"]; – Fred steven Jun 08 '15 at 17:57
  • You're trying to access the session before it has a value in it. If you're not sure if your session will have a value in it before you access it, it's always good to have a null check. if (Session["newResult"] != null) { //do stuff} – Camilo Jun 08 '15 at 18:24
  • Now my code works just fine I could see the session kept my list but the usercontrol doesn't get loaded! any idea ? appreciated – Fred steven Jun 08 '15 at 19:54
  • You have to add the user control to the actual page. Check the following link out: http://stackoverflow.com/questions/2275625/asp-net-custom-user-control-to-add-dynamically – Camilo Jun 08 '15 at 20:27