简体   繁体   中英

How do I add a new tab programmatically to AJAX tabs?

I am slowly progressing with my tab system. I want to be able to click a button and add a new tab while keeping the other tabs and the info within them.

I have managed to do it using a session and in testing this works but I add a new tab which is fine, then the next time around I click the button it doesn't add any further tabs.

So their is one on the screen already, the code-behind adds one and then is unable to add any more. Does anyone know what is wrong with my code?

namespace Portal
{
    public partial class Tabs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnAddTab_Click(object sender, EventArgs e)
        {
            RenderExisting();
            //ReRender();
            AddTab();
        }

        void RenderExisting()
        {
            Session["tabs"] = TabContainer1;
        }

        void ReRender()
        {
            TabContainer1 = (AjaxControlToolkit.TabContainer)Session["tabs"];
        }

        void AddTab()
        { 
            AjaxControlToolkit.TabContainer tbcDynamic = new AjaxControlToolkit.TabContainer();
            //Load the session tabs
            tbcDynamic = (AjaxControlToolkit.TabContainer)Session["tabs"];

            //Create new tab
            AjaxControlToolkit.TabPanel newtab = new AjaxControlToolkit.TabPanel();

            //string name = "";
            //short idx = 0;

            //newtab.HeaderText = name;
            //newtab.TabIndex = idx;
            //Add new tab to dynamic tabs
            tbcDynamic.Tabs.Add(newtab);

            //Add dynamic tabs to on-screen tabs
            TabContainer1 = tbcDynamic;
            Session.Clear();
        }
    }
}

i don't think you need to keep it in Session . just use the TabContainer1 directly

void AddTab()
{ 
    //Create new tab
    AjaxControlToolkit.TabPanel newtab = new AjaxControlToolkit.TabPanel();
    TabContainer1.Tabs.Add(newtab);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM