I've been struggling with saving a TabContainer to a viewstate variable that would load all of the TabPanels of that TabContainer. (Would page be loaded faster?). Pardon me if the question is not properly formatted; I'm still new to asp.net, here's my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateTabbedPanel();
Response.Write("New Tabs Loaded");
}
else
{
// Here i would need to load the TabContainer from the viewstate variable
Response.Write("Tabs Loaded from ViewState");
}
}
private void CreateTabbedPanel()
{
TabPanel tp = null;
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("select Description from TblProductType", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
tp = new TabPanel();
tp.HeaderText = rdr["Description"].ToString();
TabContainer1.Tabs.Add(tp);
}
}
// Here i would need to create the viewstate variable
}
And this is my webform:
<form id="form1" runat="server">
<div>
<ajaxToolkit:TabContainer ID="TabContainer2" runat="server"
ActiveTabIndex="1" AutoPostBack="false">
</ajaxToolkit:TabContainer>
</div>
</form>
What do I need to do?
Assuming you only need to save few tab names, you can use ViewState
. Otherwise I'd recommend Session
as it allows to store complex objects at server side without having to post them into the page, it takes less code but you also need to handle session expired.
The collection of tabs is read-only, non serializable, etc. However this code can be used to save headers in CreateTabbedPanel()
.
private void SaveTabs()
{
TabPanel[] tabs = new TabPanel[TabContainer1.Tabs.Count];
TabContainer1.Tabs.CopyTo(tabs, 0);
ViewState["tabs"] = tabs.Select(t=>t.HeaderText).ToArray();
}
When page load is not post back:
private void LoadTabs()
{
string[] headers = (string[])ViewState["tabs"];
if(headers!=null)
{
foreach (string header in headers)
{
TabContainer1.Tabs.Add(new TabPanel() { HeaderText = header });
}
}
}