How can I re-instantiate dynamic ASP.NET user controls without using a database?

前端 未结 3 2048
孤城傲影
孤城傲影 2020-12-18 07:55

I\'m currently working with a part of my application that uses Dynamic Web User Controls and I\'m having a bit of trouble figuring out the best way to re-instantiate the con

3条回答
  •  醉话见心
    2020-12-18 08:30

    I have done this in the past. I have not had to do this since the days of .NET 1.1, but the principal removes the same.

    I did it on Page_Load not Init have to reload the controls that you created on the last page cycle.

    First you need to keep track of the controls you have created on each page cycle. This includes type, name etc. . .

    Then on each page load you need to rebuild them.

    You do that by re-creating the control, assinging it the exact same id, add it to the sampe place on the page and finally in the ViewState["LoadedControl"] to the control type.

    Here is the code I used, I only did this with User Controls that I created. I have not tried this with an ASP.NET control, but I think it would work the same.

    In this case I have an ArrayList of Triplets (keep in mind this is .NET 1.1) adn the first item was a PageView ID. You might not need that for your application.

    protected void Page_Load(object sender, System.EventArgs e)
    {
        //**********************************************************
        //*  dynCtlArray will hold a triplet with the PageViewID,  *
        //*  ControlID, and the Control Name                       *
        //**********************************************************
    
        ArrayList dynCtlArray = (ArrayList)this.ViewState["dynCtlArray"];
        if (dynCtlArray != null)
        {
    
            foreach (object obj in dynCtlArray)
            {
                Triplet ctrlInfo = (Triplet)obj;
    
                DynamicLoadControl(ctrlInfo);
            }
        }
    }
    
    private void DynamicLoadControl(Triplet ctrlInfo)
    {
        // ERROR HANDLING REMOVED FOR ANSWER BECAUSE IT IS NOT IMPORTANT YOU SHOULD HANDLE ERRORS IN THIS METHOD
    
        Control ctrl = this.LoadControl(Request.ApplicationPath
            + "/UC/" + (string)ctrlInfo.Third);
    
        ctrl.ID = (string)ctrlInfo.Second;
    
        // Create New PageView Item
        Telerik.WebControls.PageView pvItem = this.RadMultiPage1.PageViews[(int)ctrlInfo.First];
        pvItem.Controls.Add(ctrl);
    
        /******************************************************
         *  The ControlName must be preserved to track the    *
         *  currently loaded control                          *
         * ****************************************************/
        ViewState["LoadedControl"] = (string)ctrlInfo.Third;
    }
    private void RegisterDynControl(Triplet trip)
    {
        ArrayList dynCtlArray = (ArrayList)this.ViewState["dynCtlArray"];
    
        if (dynCtlArray == null)
        {
            dynCtlArray = new ArrayList();
            this.ViewState.Add("dynCtlArray", dynCtlArray);
        }
    
        dynCtlArray.Add(trip);
    
    }
    

    In some method on your page

    // Create new Control
    Control ctrl = Page.LoadControl("../UC/MyUserControl.ascx");
    
    // . . . snip .. . 
    
    // Create Triplet
    Triplet ctrlInfo = new Triplet(0, ctrl.ID, "MyUserControl.ascx");
    // RegisterDynControl to ViewState
    RegisterDynControl(ctrlInfo);
    
    // . . . snip .. . 
    

    To access the controls to save there information you will have to do a this.Page.FindControl('');

提交回复
热议问题