I know this has been asked before, but I feel like I'm losing my way trying to follow the other examples out there. Does anyone see a straightforward solution to my particular problem?
Goal:
Two column Master Page
Left column contains a list of [stuff] (I'm trying a Repeater with LinkButtons)
Right column contains a TabContainer which will add dynamically created tabs when buttons on the left are clicked.
I had initially been creating tabs in the LB's OnCommand event, but of course this means that any tab generated by one click is lost if the user clicks another LinkButton.
So I moved the code to create a new tab to the OnInit section of the page and attempted to loop through the RepeaterItems comparing the UniqueId of each LinkButton until I found the one that was clicked using Request.Form["__EVENTTARGET"].
When I tried to loop through the RepeaterItems, however, the count was always 0, and the Repeater would not populate.
So now I am rebinding the Repeater in the Init section on every post-back - this allows me to find the LinkButton that was clicked. I then add the new Tab based on the LB that was clicked like this:
if (!string.IsNullOrEmpty(Request.Form["__EVENTTARGET"]))
{
foreach (RepeaterItem ri in rptReportList.Items)
{
LinkButton lb = (LinkButton)ri.FindControl("lbShowReport");
if (lb != null)
{
if (lb.UniqueID == Request.Form["__EVENTTARGET"].ToString())
{
CreateNewTab(lb);
}
}
}
}
This properly adds new Tabs to the TabContainer using this code:
but I still have the issue where I can only ever have one dynamically created tab in the TabContainer at a time. All of the code above is being executed in OnInit, which I thought would save the tabs during post-backs, but instead this is behaving as tho I
was adding the tabs in the OnCommand event of the Link Button...
It won't save the tabs the way you're doing it because the tabs aren't saved in viewstate. You need to store something in ViewState that you can read back in to create the tabs again. If you manually add something to ViewState, then you can access that ViewState
value OnInit.
However, values of form controls are not available to you OnInit because ViewState has not been applied yet.
void override OnInit(Object sender, EventArgs e)
{
//This hidden field value isn't available yet because ViewState doesn't get applied until after Init
HiddenField hfID = (HiddenField)lb.Parent.FindControl("hfReportID");
ContentPlaceHolder cph = (ContentPlaceHolder)Page.Master.FindControl("cphRight");
if (cph != null)
{
TabContainer tc = (TabContainer)cph.FindControl("tcReports");
if (tc != null)
{
TabPanel tpTemplate = (TabPanel)tc.FindControl("tpReportTemplate");
if (tpTemplate != null)
{
TabPanel tpNew = new TabPanel();
tpNew.HeaderText = lb.Text;
tpNew.ID = hfID.Value;
tpTemplate.ContentTemplate.InstantiateIn(tpNew);
tc.Tabs.Add(tpNew);
}
}
}
base.OnInit(e);
}
You can instead use a PageMethod with a javascript function that sets a session variable before you post back. That way the session variable will be available to you OnInit.
Look for my answer on this thread to get an idea of how to use a PageMethod for your solution
HERE
Ask Me About: Kentico, XHTML, CSS, Javascript, Asp.Net C#, SQL Server.
Thanks for your help; I was afraid it was going to be something like that, so I just created a static Dictionary that holds the values for tabs that have been created.
On each Page_Load event, I just run through the Dictionary and recreate all the tabs;
in the LinkButton Command event, I add the new tab to the Dictionary and then create the new tab as well.
Thanks for your help; I was afraid it was going to be something like that, so I just created a static Dictionary that holds the values for tabs that have been created.
On each Page_Load event, I just run through the Dictionary and recreate all the tabs;
in the LinkButton Command event, I add the new tab to the Dictionary and then create the new tab as well.
Very very ugly, but it seems to work... so far...
Making things work around the page lifecycle is often ugly. But only because it's not meant to work that way. I often use ajax, like I mentioned earlier, to prepare things so they will be ready OnInit during postback.
Ask Me About: Kentico, XHTML, CSS, Javascript, Asp.Net C#, SQL Server.
Schmakt
Member
309 Points
209 Posts
Saving Dynamically Created Tabs on PostBack
Nov 08, 2010 07:40 PM|LINK
I know this has been asked before, but I feel like I'm losing my way trying to follow the other examples out there. Does anyone see a straightforward solution to my particular problem?
Goal:
Two column Master Page
Left column contains a list of [stuff] (I'm trying a Repeater with LinkButtons)
Right column contains a TabContainer which will add dynamically created tabs when buttons on the left are clicked.
Left side looks like this:
<asp:Repeater
<ItemTemplate>
<asp:LinkButton ID="lbShowReport" runat="server" Text='<%# Eval("Title") %>' CommandName="Select">
</asp:LinkButton>
<asp:HiddenField ID="hfReportID" runat="server" Value='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:Repeater>
I had initially been creating tabs in the LB's OnCommand event, but of course this means that any tab generated by one click is lost if the user clicks another LinkButton.
So I moved the code to create a new tab to the OnInit section of the page and attempted to loop through the RepeaterItems comparing the UniqueId of each LinkButton until I found the one that was clicked using Request.Form["__EVENTTARGET"].
When I tried to loop through the RepeaterItems, however, the count was always 0, and the Repeater would not populate.
So now I am rebinding the Repeater in the Init section on every post-back - this allows me to find the LinkButton that was clicked. I then add the new Tab based on the LB that was clicked like this:
if (!string.IsNullOrEmpty(Request.Form["__EVENTTARGET"])) { foreach (RepeaterItem ri in rptReportList.Items) { LinkButton lb = (LinkButton)ri.FindControl("lbShowReport"); if (lb != null) { if (lb.UniqueID == Request.Form["__EVENTTARGET"].ToString()) { CreateNewTab(lb); } } } }This properly adds new Tabs to the TabContainer using this code:
HiddenField hfID = (HiddenField)lb.Parent.FindControl("hfReportID"); ContentPlaceHolder cph = (ContentPlaceHolder)Page.Master.FindControl("cphRight"); if (cph != null) { TabContainer tc = (TabContainer)cph.FindControl("tcReports"); if (tc != null) { TabPanel tpTemplate = (TabPanel)tc.FindControl("tpReportTemplate"); if (tpTemplate != null) { TabPanel tpNew = new TabPanel(); tpNew.HeaderText = lb.Text; tpNew.ID = hfID.Value; tpTemplate.ContentTemplate.InstantiateIn(tpNew); tc.Tabs.Add(tpNew); } } }but I still have the issue where I can only ever have one dynamically created tab in the TabContainer at a time. All of the code above is being executed in OnInit, which I thought would save the tabs during post-backs, but instead this is behaving as tho I was adding the tabs in the OnCommand event of the Link Button...
Any thoughts?
Thank you!
jiveabillion
Contributor
4222 Points
775 Posts
Re: Saving Dynamically Created Tabs on PostBack
Nov 08, 2010 08:02 PM|LINK
It won't save the tabs the way you're doing it because the tabs aren't saved in viewstate. You need to store something in ViewState that you can read back in to create the tabs again. If you manually add something to ViewState, then you can access that ViewState value OnInit.
However, values of form controls are not available to you OnInit because ViewState has not been applied yet.
void override OnInit(Object sender, EventArgs e) { //This hidden field value isn't available yet because ViewState doesn't get applied until after Init HiddenField hfID = (HiddenField)lb.Parent.FindControl("hfReportID"); ContentPlaceHolder cph = (ContentPlaceHolder)Page.Master.FindControl("cphRight"); if (cph != null) { TabContainer tc = (TabContainer)cph.FindControl("tcReports"); if (tc != null) { TabPanel tpTemplate = (TabPanel)tc.FindControl("tpReportTemplate"); if (tpTemplate != null) { TabPanel tpNew = new TabPanel(); tpNew.HeaderText = lb.Text; tpNew.ID = hfID.Value; tpTemplate.ContentTemplate.InstantiateIn(tpNew); tc.Tabs.Add(tpNew); } } } base.OnInit(e); }You can instead use a PageMethod with a javascript function that sets a session variable before you post back. That way the session variable will be available to you OnInit.
Look for my answer on this thread to get an idea of how to use a PageMethod for your solution HERE
Learn the LifeCycle
Free Online Games
Schmakt
Member
309 Points
209 Posts
Re: Saving Dynamically Created Tabs on PostBack
Nov 09, 2010 03:11 PM|LINK
Thanks for your help; I was afraid it was going to be something like that, so I just created a static Dictionary that holds the values for tabs that have been created.
On each Page_Load event, I just run through the Dictionary and recreate all the tabs;
in the LinkButton Command event, I add the new tab to the Dictionary and then create the new tab as well.
Very very ugly, but it seems to work... so far...
jiveabillion
Contributor
4222 Points
775 Posts
Re: Saving Dynamically Created Tabs on PostBack
Nov 09, 2010 03:38 PM|LINK
Making things work around the page lifecycle is often ugly. But only because it's not meant to work that way. I often use ajax, like I mentioned earlier, to prepare things so they will be ready OnInit during postback.
Learn the LifeCycle
Free Online Games