Repeater handling events using ITemplate building buttons dynmicaly

I built a repeater in side a table ...
<asp:Repeater ID="ResultRepeater" runat="server" OnItemCommand="ResultRepeater_ItemCommand" >                                             
 </asp:Repeater>
What I am trying to do is do sorting acceding and descending with button in the header part of the repeater template....   but the Template is built dynamically  using the ITemplate it buids the grid fine but when you click one of the header buttons to sort i go to Page_load and not firring the some event.
Question is how do I get that to fire an event inside the Repeater Template so that I can then use that event to sort the data.
What i was told is by using the OnItemCommand it would bubble up and then i could capture that button event that is dynamically building in the ITeplate.  


 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Data;
using System.Reflection;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

/// <summary>
/// Summary description for RepeaterTemplate
/// </summary>
public class RepeaterTemplate : ITemplate
{
    static int itemcount = 0;
    ListItemType templateType;
    string[] columnsOut = new string[0];
    
    public RepeaterTemplate(ListItemType type, string[] columnsIn)
    {
        templateType = type;
        columnsOut = columnsIn;
    }
    public RepeaterTemplate(ListItemType type)
    {
        templateType = type;
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
        Literal lc = new Literal();

        // Header Template
        switch (templateType)
        {
            case ListItemType.Header:
             HtmlGenericControl rowHeader = new HtmlGenericControl("tr");
             rowHeader.Attributes.Add("style", "background:#669acc;");

       //itterating through the string[] columns to build the header
                foreach (string col in columnsOut)
                {
                    HtmlGenericControl cell = new HtmlGenericControl("th");
                   // RepeaterCommandEventHandler RCE = new RepeaterCommandEventHandler(Ib_Command);
                    cell.InnerText =  col;
                     rowHeader.Controls.Add(cell);
     //****************************************
    // Button building HERE 
    //I would like to use Image Button if posible
                     LinkButton Ib = new LinkButton();
                     Ib.OnClientClick += new EventHandler(SYS_EQP_Click);
                    // Ib.Command +=new CommandEventHandler(Ib_Command);
                     Ib.CommandArgument = col;
                     Ib.CommandName = "ds";
                     Ib.ID = col;
                     Ib.Text = col;
                  // Ib.ImageUrl ="../img/arrowdown.PNG";
                   // Ib.Attributes.Add("background-color", "#669acc;");
                    cell.Controls.Add(Ib);
    //********************************************
                }
                container.Controls.Add(rowHeader);
                break;
               //Itemtemplate and set event to bind the data 
            case ListItemType.Item:
                lc.Text = "<TR><TD>";
                lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
                container.Controls.Add(lc);
                break;
                // setting the footer
            case ListItemType.Footer:
                HtmlGenericControl footHeader = new HtmlGenericControl("tr"); 
                break;

            //case ListItemType.AlternatingItem:
            //   lc.Text = "<TR><TD bgcolor=lightblue>Item number: " + 
            //      itemcount.ToString() + "</TD></TR>";
            //   break;
        }

    }

    // this is where I bind the data to the proper literal control or column
    private void TemplateControl_DataBinding(object sender, System.EventArgs e)
    {
        Literal lc = (Literal)sender;
        RepeaterItem container = (RepeaterItem)lc.NamingContainer;

        foreach (string col in columnsOut)
        {
            lc.Text += DataBinder.Eval((container.DataItem), col);
            lc.Text += "</TD><TD>";
        }

    }
    // Tried Diffident Events to add to the button for testing then add the sort code
    protected void ResultRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string r = e.ToString();
    }
    protected void Ib_Command(object sender, ImageClickEventArgs e)
    {
                   string r = e.ToString();
    }
    protected void SYS_EQP_Click(object sender, EventArgs e)
    {
        string r = e.ToString();
    }
}

Open in new window

In the Page_Load there are drop-downs that select the criteria for the query it is inside ispostback under the load....  I going to show you the button code and the other parts

public partial class IBBSCleanups : System.Web.UI.Page
{
    string sMsg = ""; 
    string []colmns = new string[1];
    GetColumns columnsRet = new GetColumns();
    PagedDataSource pageSource = new PagedDataSource();
    int Count = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                ListItem liSelect = new ListItem("- Select -", "-1");
                try
                {
                    frontEndImportClassesDataContext db = new frontEndImportClassesDataContext();
                    var vResults = (from p in db.tblSPPrefs
                                    where p.strWebFrontendIncluded.ToString().ToUpper().Equals("Y")
                                    select new { SysNum = p.intSys }).Distinct().OrderBy(p => p.SysNum);
                    ddlSys.DataSource = vResults;
                    ddlSys.DataTextField = "SysNum";
                    ddlSys.DataValueField = "SysNum";
                    ddlSys.DataBind();
                    ddlSys.Items.Insert(0, liSelect);
                    ddlSys.SelectedIndex = 0;
                }
                catch { }

                // Set up the Reports list
                ListItem li1 = new ListItem("HSD Inventory to Service Code Analysis", sHSDInventory);
                ListItem li2 = new ListItem("HSD Service Subs Analysis", sHSDSvcSubs);
                ListItem li3 = new ListItem("Phone Inventory to Service Code Analysis", sPhoneInventory);
                ddlReports.Items.Clear();
                ddlReports.Items.Add(liSelect); ddlReports.Items.Add(li1);
                ddlReports.Items.Add(li2); ddlReports.Items.Add(li3);

                // These are used to set up the Navigation buttons contained in the Master Page
                Label lblPageTitle = (Label)Master.FindControl("lblPageTitle"); lblPageTitle.Text = "IBBS Cleanup Analysis";
                Label lblRptTitle = (Label)Master.FindControl("lblRptTitle"); lblRptTitle.Text = "IBBSCleanup_";
                Label lblRedirect = (Label)Master.FindControl("lblRedirect"); lblRedirect.Text = "ReportPages/IBBSCleanups.aspx";
               // InitializeComponent();
            }
            else
            {
                if (pageSource.DataSource != null)
                {
                    ResultRepeater.DataSource = pageSource;
                    ResultRepeater.DataBind();
                }
            
            }
            btnRun.Click += new EventHandler(btnRun_Click);
        }
        catch (Exception ex) { sMsg = ex.Message; };
    }

    protected void btnRun_Click(object sender, EventArgs e)
    {
        // Clear out old results
        //dropdowns stuff
        Session.Remove("ResultDT");
        Session.Remove("ResultDS");
        Session.Remove("ResultDSNames");

        // Depending on the selected report, call the appropriate Stored Procedure
        int iSys = Convert.ToInt16(ddlSys.SelectedValue);
        int iPrin = Convert.ToInt16(ddlPrin.SelectedValue);

        try
        {
            //setting up for the query
            frontEndImportClassesDataContext db = new frontEndImportClassesDataContext();
            // depending on what dropdown select detemins witch query is used
            switch (ddlReports.SelectedValue)
            {
         //each are the same so use this one*********
                case sHSDInventory:
                    var vRes1 = db.sp_GetIBBSCleanupHSDInventorytoSvcCodeAnalysis(iSys, iPrin).ToList();
                    // count for other things
                    Count =   vRes1.Count();
                    //use refection to get the columns
                    colmns = columnsRet.GetAllColumns(vRes1);
                    // binds data to pagedatasource
                    pageSource.DataSource = vRes1;
                    // don't worry about setuppage
                    SetUpPage();
                    //this is where the template is set up and data is set
                    ResultRepeater.DataSource = pageSource;
                    SetUpRepeater(colmns);
                    // finally bind to the data witch fires the public void InstantiateIn in the repeater template

                    ResultRepeater.DataBind();
                    break;
        //*******************
                case sHSDSvcSubs:
                    var vRes2 = db.sp_GetIBBSCleanupHSDSvcSubsAnalysis(iSys, iPrin);

                    Count = vRes2.Count();
                    colmns = columnsRet.GetAllColumns(vRes2);
                    pageSource.DataSource = vRes2;
                    SetUpPage();
                    SetUpRepeater(colmns);
                    ResultRepeater.DataSource = pageSource;
                    ResultRepeater.DataBind();
                    
                    break;
                case sPhoneInventory:
                    var vRes3 = db.sp_GetIBBSCleanupPhoneInventorytoSvcCodeAnalysis(iSys, iPrin);

                    Count = vRes3.Count();
                    colmns = columnsRet.GetAllColumns(vRes3);
                    pageSource.DataSource = vRes3;
                    SetUpPage();
                    SetUpRepeater(colmns);
                    ResultRepeater.DataSource = pageSource;
                    ResultRepeater.DataBind();
                    break;
            }
            //Delete session
           // Session["ResultDT"] = dt;
        }
        catch (Exception ex)
        {
            sMsg = ex.Message;
        }
    } 
    
    private void SetUpRepeater(string [] columns)
    {       
        //set up repeater 
        ResultRepeater.HeaderTemplate = new RepeaterTemplate(ListItemType.Header, columns);
        ResultRepeater.ItemTemplate = new RepeaterTemplate(ListItemType.Item, columns);
                //ResultRepeater.AlternatingItemTemplate =
                //   new RepeaterTemplate(ListItemType.AlternatingItem);
        ResultRepeater.FooterTemplate = new RepeaterTemplate(ListItemType.Footer);
         
    }
  // Don't worry about this part it just for other thing*******  
    private void SetUpPage()
    {
        // set up page
        pageSource.AllowCustomPaging = true;
        pageSource.AllowPaging = true;
        pageSource.PageSize = 15;

        lblRptRequested.Text = ddlReports.SelectedItem.Text + ": " + ddlSys.SelectedItem.Text + " - " + ddlPrin.SelectedItem.Text;
        ImageButton imgExcel = (ImageButton)Master.FindControl("imgExcelMaster");
        if (sMsg.Length == 0)
        {
            if (pageSource == null)
            {
                lblNumResults.Text = "No results returned for selected criteria.";
                // gvResults.Visible = false;
                ResultRepeater.Visible = false;
                imgExcel.Enabled = false;
            }
            else
            {
                lblNumResults.Text = "Records matching requested criteria: " + Count;
                // gvResults.Visible = true;
                ResultRepeater.Visible = true;
                imgExcel.Enabled = true;
            }
        }
        else
        {
            lblNumResults.Text = "The following error occurred during processing:<br/>" + sMsg;
            // gvResults.Visible = false;
            ResultRepeater.Visible = false;
            imgExcel.Enabled = false;
        }
        // Show the results
        panInput.Visible = false;
        panOutput.Visible = true;
    }

Open in new window

BrentDennyAsked:
Who is Participating?
 
Bob LearnedCommented:
1) The SetReportSession<T> is not required, and not very helpful:

protected void SetReportSession<T>(List<T> vRes)
    {
         Session["report"] = new List<T>();
         Session["report"] = vRes;
    }

Open in new window


All you need is Session["report"] = vRes1;

2) In order to use generics argument T, you need to know the type at design-time.

3) If you have .NET 4.0, you can use the "dynamic" keyword to have this code evaluated at run-time, instead of compile errors.
0
 
BrentDennyAuthor Commented:
for got to add that  
 <table border="0" width="100%">
                          <asp:Repeater ID="ResultRepeater" runat="server"
                                  OnItemCommand="ResultRepeater_ItemCommand" >                   
                           </asp:Repeater>
   </table>
0
 
Bob LearnedCommented:
Information overload!!

1) Template is built dynamically

2) public class RepeaterTemplate : ITemplate

3) Are you talking about the LinkButton here?

                     LinkButton Ib = new LinkButton();
                     Ib.OnClientClick += new EventHandler(SYS_EQP_Click);
                    // Ib.Command +=new CommandEventHandler(Ib_Command);
                     Ib.CommandArgument = col;
                     Ib.CommandName = "ds";
                     Ib.ID = col;
                     Ib.Text = col;

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
BrentDennyAuthor Commented:
Yes sorry i know there is a lot there.
but so far i have figured out that i don't need the Ib.OnClientClick += new EventHandler(SYS_EQP_Click); that if i use as viewstate and capture the information and rebind it to the repeater then fired event will bubble up to the  in the ibbscleanups class to :
 protected void ResultRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {  and i can sort in here
}
i did this in a test for a simple repeater with a know type that is bound to the repeater.
but in my problem I dont know what the type is before the bind so if you look at the btnRun_Click
there are more that 1 possible return types I was think of storing the pageddatasourse  to the viewstate.   somethig like this
P rivate PagedDataSource SetDataSource
    {
           get{
               return this.ViewState["Report"] as PagedDataSource;  
           }
        set{
            this.ViewState["Report"] = value;
        }  }
    private void bindRepeater(Boolean refreshData)
    {
        if (refreshData)
        {
            this.SetDataSource = pageSource;//ViewState["Report"] as PagedDataSource;
        }
        this.ResultRepeater.DataSource = this.SetDataSource;
        this.ResultRepeater.DataBind();
    }

Should i use it like this because I want to be able to sort the results and  but i dont think i can do that
it would be much easer to be able to store the

  var vRes1 = db.sp_GetIBBSCleanupHSDInventorytoSvcCodeAnalysis(iSys, iPrin).ToList();

results to the viewstate and thin in the itemCommand sort it like this and rebind it to the repeater

vRes1.OrderByDescending(r => r.EQP_LOCAL_EQP).ToList();

the problem is that since i dont know the" type" List< unknown > of that result until run time i dont think that i can use the get and set and i wanted to use the pageddatasourse so that i could use the paging abilities that it has

So what is the best way to handling viewstate and then sorting in the itemCommand  is it use the pageddatasourse  or use the vRes1 results  and if the it is the pageddatasourse   how can i sort it in the itemCommand   and if it is the vRes1 results how do i use the viewstate with this.

Thanks for your help
0
 
BrentDennyAuthor Commented:
Here is what I was attempting to do

string sMsg = "";
    string []colmns = new string[1];
    GetColumns columnsRet = new GetColumns();
    PagedDataSource pageSource = new PagedDataSource();
   
    int Count = 0;
    private PagedDataSource SetDataSource
    {
           get{
               return this.ViewState["Report"] as PagedDataSource;  
           }
        set{
            this.ViewState["Report"] = value;
        }
    }
    private void bindRepeater(Boolean refreshData)
    {
        if (refreshData)
        {
            this.SetDataSource = pageSource;//ViewState["Report"] as PagedDataSource;
        }
        this.ResultRepeater.DataSource = this.SetDataSource;
        this.ResultRepeater.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                ListItem liSelect = new ListItem("- Select -", "-1");
                try
                {
                    frontEndImportClassesDataContext db = new frontEndImportClassesDataContext();
                    var vResults = (from p in db.tblSPPrefs
                                    where p.strWebFrontendIncluded.ToString().ToUpper().Equals("Y")
                                    select new { SysNum = p.intSys }).Distinct().OrderBy(p => p.SysNum);
                   
                    ddlSys.DataSource = vResults;
                    ddlSys.DataTextField = "SysNum";
                    ddlSys.DataValueField = "SysNum";
                    ddlSys.DataBind();
                    ddlSys.Items.Insert(0, liSelect);
                    ddlSys.SelectedIndex = 0;
                }
                catch (Exception ex) { sMsg = ex.Message; };
                // Set up the Reports list
                ListItem li1 = new ListItem("HSD Inventory to Service Code Analysis", sHSDInventory);
                ListItem li2 = new ListItem("HSD Service Subs Analysis", sHSDSvcSubs);
                ListItem li3 = new ListItem("Phone Inventory to Service Code Analysis", sPhoneInventory);
                ddlReports.Items.Clear();
                ddlReports.Items.Add(liSelect); ddlReports.Items.Add(li1);
                ddlReports.Items.Add(li2); ddlReports.Items.Add(li3);

                // These are used to set up the Navigation buttons contained in the Master Page
                Label lblPageTitle = (Label)Master.FindControl("lblPageTitle"); lblPageTitle.Text = "IBBS Cleanup Analysis";
                Label lblRptTitle = (Label)Master.FindControl("lblRptTitle"); lblRptTitle.Text = "IBBSCleanup_";
                Label lblRedirect = (Label)Master.FindControl("lblRedirect"); lblRedirect.Text = "ReportPages/IBBSCleanups.aspx";
                Session["count"] = "0";
                Session["Hasfired"] = "";
            }
            else
            {
                if (Session["Hasfired"].ToString() == "true")
                {
                    BuildRepeater();
                }
             }
       
        }
         finally{};
    }

    protected void btnRun_Click(object sender, EventArgs e)
    {
        // Clear out old results
        //dropdowns stuff
        Session.Remove("ResultDT");
        Session.Remove("ResultDS");
        Session.Remove("ResultDSNames");

        BuildRepeater();
        Session["Hasfired"] = "true";
       
    }
        protected void BuildRepeater()
        {
            // Depending on the selected report, call the appropriate Stored Procedure
            int iSys = Convert.ToInt16(ddlSys.SelectedValue);
            int iPrin = Convert.ToInt16(ddlPrin.SelectedValue);
        try
        {
            //setting up for the query
            frontEndImportClassesDataContext db = new frontEndImportClassesDataContext();
            // depending on what dropdown select detemins witch query is used
            switch (ddlReports.SelectedValue)
            {
         //earch are the same so use this one*********
                case sHSDInventory:
                    if (Session["Hasfired"].ToString() != "true")
                    {
                  var vRes1 = db.sp_GetIBBSCleanupHSDInventorytoSvcCodeAnalysis(iSys, iPrin).ToList();
                   
                    colmns = columnsRet.GetAllColumns(vRes1);

                    Session["Count"] = vRes1.Count();
                    pageSource.DataSource = vRes1;
                         
                    SetUpPage();
                    this.SetDataSource = pageSource;
                    }  

                    SetUpRepeater(colmns);
                    bindRepeater(true);
                    //ResultRepeater.DataSource = pageSource;
                   // ResultRepeater.DataBind();
                    break;
        //*******************
                case sHSDSvcSubs:
                    var vRes2 = db.sp_GetIBBSCleanupHSDSvcSubsAnalysis(iSys, iPrin);

                    Count = vRes2.Count();
                    colmns = columnsRet.GetAllColumns(vRes2);
                    pageSource.DataSource = vRes2;
                    SetUpPage();
                    SetUpRepeater(colmns);
                  //  ResultRepeater.DataSource = pageSource;
                  //  ResultRepeater.DataBind();
                   
                    break;
                case sPhoneInventory:
                    var vRes3 = db.sp_GetIBBSCleanupPhoneInventorytoSvcCodeAnalysis(iSys, iPrin);

                    Count = vRes3.Count();
                    colmns = columnsRet.GetAllColumns(vRes3);
                    pageSource.DataSource = vRes3;
                    SetUpPage();
                    SetUpRepeater(colmns);
                  //  ResultRepeater.DataSource = pageSource;
                   // ResultRepeater.DataBind();
                    break;
            }
        }
        catch (Exception ex)
        {
            sMsg = ex.Message;
        }
    }
   
    private void SetUpRepeater(string [] columns)
    {      
        //set up repeater
     
        ResultRepeater.HeaderTemplate = new RepeaterTemplate(ListItemType.Header, columns);
        ResultRepeater.ItemTemplate = new RepeaterTemplate(ListItemType.Item, columns);
                //ResultRepeater.AlternatingItemTemplate =
                //   new RepeaterTemplate(ListItemType.AlternatingItem);
        ResultRepeater.FooterTemplate = new RepeaterTemplate(ListItemType.Footer);
         
    }
   
    private void SetUpPage()
    {
        // set up page
        pageSource.AllowCustomPaging = true;
        pageSource.AllowPaging = true;
        pageSource.PageSize = 15;

        lblRptRequested.Text = ddlReports.SelectedItem.Text + ": " + ddlSys.SelectedItem.Text + " - " + ddlPrin.SelectedItem.Text;
        ImageButton imgExcel = (ImageButton)Master.FindControl("imgExcelMaster");
        if (sMsg.Length == 0)
        {
            if (pageSource == null)
            {
                lblNumResults.Text = "No results returned for selected criteria.";
                // gvResults.Visible = false;
                ResultRepeater.Visible = false;
                imgExcel.Enabled = false;
            }
            else
            {
                lblNumResults.Text = "Records matching requested criteria: " + Session["Count"].ToString();
                // gvResults.Visible = true;
                ResultRepeater.Visible = true;
                imgExcel.Enabled = true;
            }
        }
        else
        {
            lblNumResults.Text = "The following error occurred during processing:<br/>" + sMsg;
            // gvResults.Visible = false;
            ResultRepeater.Visible = false;
            imgExcel.Enabled = false;
        }
        // Show the results
        panInput.Visible = false;
        panOutput.Visible = true;
    }
 
    protected void imgPrint_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            DataTable dt = Session["ResultDT"] as DataTable;
            if (dt != null)
            {
                string sScript = "<script>"
                    + "window.open('PrintGrid.aspx?HDR=Street Sheet','PrintFriendly',"
                    + "'height=500,width=700,top=200,left=300,resizable=1,scrollbars=1');</script>";

                ClientScript.RegisterStartupScript(this.GetType(), "PrintFriendly", sScript);
            }
        }
        catch { }

    }

    protected void imgExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            DataTable dt = Session["ResultDT"] as DataTable;
            if (dt != null)
            {
                string sFileName = "IBBSCleanups_" + DateTime.Now.Ticks.ToString();
                ExcelExport ee = new ExcelExport();
                ee.Generate(sFileName);
            }
        }
        catch { }
    }

    protected void imgReset_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/ReportPages/IBBSCleanups.aspx");
    }

    protected void ddlSys_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlSys.SelectedIndex > 0)
        {
            frontEndImportClassesDataContext db = new frontEndImportClassesDataContext();

            var vSys = from p in db.tblSPPrefs
                       where p.strWebFrontendIncluded.ToString().ToUpper().Equals("Y") && p.intSys.Equals(ddlSys.SelectedValue)
                       orderby p.intPrin
                       select new { PrinNum = p.intPrin, PrinName = p.intPrin.ToString() + "-" + p.strSPDesc };

            ddlPrin.DataSource = vSys;
            ddlPrin.DataTextField = "PrinName";
            ddlPrin.DataValueField = "PrinNum";
            ddlPrin.DataBind();
        }
        else
        {
            ddlPrin.Items.Clear();
        }

        ListItem liSelect = new ListItem("- Select -", "-1");
        ddlPrin.Items.Insert(0, liSelect);
        ddlPrin.SelectedIndex = 0;
    }


    protected void ResultRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string r = e.ToString();
             Type elements =  this.SetDataSource.DataSource.GetType();
        //   this.SetDataSource.OrderByDescending(r => r.EQP_LOCAL_EQP).ToList();
    }
}
0
 
Bob LearnedCommented:
Still too much information!!

Are you saying that you solved your one issue, and now you are asking a different question?  I am not sure that I understand what you need, and I certainly don't need all of the code in order to understand (too confusing).
0
 
BrentDennyAuthor Commented:
It was one in the same problem  first thing is that the button event was not firing and the whole point of the button to firing is so that it would sort that col that you clicked on.  So what i figured  out is  that i need to use a session to store the repeaters binding data in and rebind it and then then button now fires.

so i need to know how to sort the data stored in the session and then then i will rebind the data

so what i did is this
var vRes1 = db.sp_GetIBBSCleanupHSDInventorytoSvcCodeAnalysis(iSys, iPrin).ToList();
session["report"] = vRes1

since the vRes1 is list< somthing>

how in the  event below sort the it

 protected void ResultRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
     
    }
could i use
protected void setSession<t>(list<t> vres)
{
session["report"]  as t;
session["report"]  = vres;


}
0
 
Bob LearnedCommented:
OK, now I am starting to understand.  You mentioned the ViewState before, and the Session now, and I would say that using the Session variable, in this case, is the better approach.  The ViewState is UTF-16 XML, which is Base-64 encoded, which makes it grow exponentially, so you shouldn't use it to store large, complex structures, or it will increase the "payload" for the web page, and increase the page load time.

You should be able to store the data source in a Session variable, retrieve it on post-back, apply the sort to the data source, and rebind.
0
 
BrentDennyAuthor Commented:
well the problem is that because it doent know how its self is i cant get the sort yet

i dont know how to get it to recognize since the vRes1 is list< something > something means I dont know what it is going to be until runtime.  before it is set to the session it has a orderby witch works grate but when I try to get it from the session its doesnt have it?  
var vRes1 = db.sp_GetIBBSCleanupHSDInventorytoSvcCodeAnalysis(iSys, iPrin).ToList();
vRes1..OrderByDescending(r => r.EQP_LOCAL_EQP).ToList(); //// this has it
session["report"] = vRes1

session["report" does not have it

So what do I need to do
0
 
Bob LearnedCommented:
Can you cast that session variable back to the type that you stored?  What type does "var vRes1" evaluate to?
0
 
BrentDennyAuthor Commented:
it is coming from a stored procedure and there are 4 diffent ones that can be called on this page each with a call assigned to them

example
 var vRes2 = db.sp_GetIBBSCleanupHSDSvcSubsAnalysis(iSys, iPrin);
var vRes1 = db.sp_GetIBBSCleanupHSDInventorytoSvcCodeAnalysis(iSys, iPrin).ToList();

after the db. = the name of object type so List<sp_GetIBBSCleanupHSDSvcSubsAnalysis>
and if you look a that big code i sent depending on the drop-down determines witch one is used
0
 
Bob LearnedCommented:
You should be able to cast the Session variable like this:

List<sp_GetIBBSCleanupHSDSvcSubsAnalysis> vRes1 = (List<sp_GetIBBSCleanupHSDSvcSubsAnalysis>)Session["report"];

Open in new window

0
 
BrentDennyAuthor Commented:
I CAN ITS dynamic i could be any type  List < any type > if  i think i have to use some type of refection
so it would be just like you said but  thats my problem once it get in the session lol it for gets what the hell its self is thats why i was trying something like this to set it up so maybe it would remember its self

protected void setSession<t>(List<t> vres)
{
session["report"]  as t;
session["report"]  = vres;


}
i think this is wrong but i hope you get the point
"t" is gone to any type that I pass in

0
 
Bob LearnedCommented:
I believe you are on the right track, but you need something more like this:

protected List<T> GetReportSession<T>()
    where T : class
{
    return Session["report"]  as List<T>;
}
0
 
BrentDennyAuthor Commented:
I will try that I think that might work I will let you know  Monday  Happy Easter
0
 
BrentDennyAuthor Commented:
I am now trying to use what you gave me but I am not sure how us this or how to set up for it to be used
0
 
Bob LearnedCommented:
Try something like this:

List<sp_GetIBBSCleanupHSDSvcSubsAnalysis> vRes1 = GetReportTest<sp_GetIBBSCleanupHSDSvcSubsAnalysis>();

Open in new window

0
 
BrentDennyAuthor Commented:
Remember  that I do not know what T is going to be until runtime so I cant do it that way
0
 
Bob LearnedCommented:
It looks like you know when you hit this code:

switch (ddlReports.SelectedValue)
            {
                case sHSDInventory:
                    var vRes1 = db.sp_GetIBBSCleanupHSDInventorytoSvcCodeAnalysis(iSys, iPrin).ToList();

                case sHSDSvcSubs:
                    var vRes2 = db.sp_GetIBBSCleanupHSDSvcSubsAnalysis(iSys, iPrin);

                case sPhoneInventory:
                    var vRes3 = db.sp_GetIBBSCleanupPhoneInventorytoSvcCodeAnalysis(iSys, iPrin);
             }

Open in new window

0
 
BrentDennyAuthor Commented:
Yes that is where i am setting the session not getting it need to get the session and what List< class it came from> in the
protected void ResultRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
     so something like this?
      var report  = GetReportSession();
 session["report"] = report.OrderByDescending(r => e.ComandArguments).ToList();

then rebind to the repeater and now its sorted

    }
0
 
BrentDennyAuthor Commented:
I am setting the session as  Session["report"] = new "what ever the class is"  in the case part and then assigning the vRes to it.
0
 
Bob LearnedCommented:
I would have thought that you could do something like this:

switch (ddlReports.SelectedValue)
            {
                case sHSDInventory:
                    List<sp_GetIBBSCleanupHSDInventorytoSvcCodeAnalysisResult> vRes1 = this.GetReportSession<sp_GetIBBSCleanupHSDInventorytoSvcCodeAnalysisResult>();

                case sHSDSvcSubs:
                    List<sp_GetIBBSCleanupHSDSvcSubsAnalysisResult> vRes2 = this.GetReportSession<sp_GetIBBSCleanupHSDSvcSubsAnalysisResult>();

                case sPhoneInventory:
                    List<sp_GetIBBSCleanupPhoneInventorytoSvcCodeAnalysisResult> vRes3 = this.GetReportSession<sp_GetIBBSCleanupPhoneInventorytoSvcCodeAnalysisResult>();
             }

Open in new window

0
 
BrentDennyAuthor Commented:
I think that you missed this first statement .......

Yes that is where i am STETTING the session NOT GETTING it  id did
TO SET
protected void SetReportSession<T>(List<T> vRes)
    {
         Session["report"] = new List<T>();
         Session["report"] = vRes;
    }
Now I need to GET the session and what List< class it came from> in the

protected void ResultRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
     so something like this?
      var report  = GetReportSession();
 session["report"] = report.OrderByDescending(r => e.ComandArguments).ToList();

then rebind to the repeater and now its sorted

    }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.