Dynamically load user control on hidden tab
Hello!
There is a hidden tab. Need to show this tab and load user control on it within one method.
It is possible to do within two separate calls, but not within a single one.
Here is the code:
Default.aspx
Code:
<%@ Page Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
UserControl currentUC1;
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(CurrentControl.Text))
{
this.LoadUserControl(CurrentControl.Text, CurrentControl, currentUC1, Panel1);
}
}
private void LoadUserControl(string num, Hidden hidden, UserControl uc, Ext.Net.Panel targetPanel, bool update = false)
{
if (update && uc != null)
{
targetPanel.ContentControls.Clear();
}
uc = (UserControl)this.LoadControl(string.Format("UserControl{0}.ascx", num));
uc.ID = "UC" + num;
targetPanel.ContentControls.Add(uc);
if (update)
{
hidden.Text = num;
targetPanel.UpdateContent();
}
}
private void ButtonShowTab_Click(object sender, DirectEventArgs e)
{
ShowTab();
ButtonShowTab.Disable();
ButtonLoadControl.Enable();
ButtonShowTabAndLoadControl.Disable();
}
private void ShowTab()
{
Panel1.Hidden = false;
tpControls.Render();
}
protected void ButtonLoadControl_Click(object sender, DirectEventArgs e)
{
LoadUserControl("1", CurrentControl, currentUC1, Panel1, true);
ButtonLoadControl.Disabled = true;
X.Msg.Alert("Correct work", "This works as expected. Now reload page and click 'Show Tab AND Load User Control' button.").Show();
}
private void ButtonShowTabAndLoadControl_Click(object sender, DirectEventArgs e)
{
ShowTab();
LoadUserControl("1", CurrentControl, currentUC1, Panel1, true);
ButtonShowTab.Disable();
this.ButtonLoadControl.Disable();
}
</script>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />
<h1>Load user control on hidden tab:</h1>
<h3>Two ways to complete:</h3>
<ol>
<li>
Click button 'Show Tab' and then 'Load User Control'. This works as expected
</li>
<li>
Click button 'Show Tab AND Load User Control'. This performs _THE_SAME_ operations as separate buttons, but does not work.
</li>
</ol>
<ext:Hidden ID="CurrentControl" runat="server" />
<ext:Panel
runat="server"
Layout="HBox">
<Items>
<ext:Panel runat="server" Layout="VBox">
<Items>
<ext:Button
ID="ButtonShowTab"
runat="server"
Text="Show Tab"
OnDirectClick="ButtonShowTab_Click"
/>
<ext:Button
ID="ButtonLoadControl"
runat="server"
Text="Load User Control"
Disabled="True"
OnDirectClick="ButtonLoadControl_Click"
/>
</Items>
</ext:Panel>
<ext:Button
ID="ButtonShowTabAndLoadControl"
runat="server"
Text="Show Tab AND Load User Control"
OnDirectClick="ButtonShowTabAndLoadControl_Click"
/>
</Items>
</ext:Panel>
<ext:TabPanel runat="server" ID="tpControls">
<Items>
<ext:Panel
Hidden="True"
ID="Panel1"
runat="server"
Title="User Controls1"
Width="500"
Height="700"
BodyPadding="5">
</ext:Panel>
</Items>
</ext:TabPanel>
</form>
</body>
</html>
UserControl1.ascx:
Code:
<%@ Control Language="C#" %>
<script runat="server">
protected void HelloFromServer(object sender, DirectEventArgs e)
{
X.Msg.Alert("Server", "Hello from server - UserControl №1").Show();
}
</script>
<h1>№1</h1>
<ext:Label ID="Label1" runat="server" Text="I am User control №1" />
<br />
<ext:Button ID="Button1" runat="server" Text="User control №1: Ext.Net button" OnDirectClick="HelloFromServer" />
How to reproduce:
Click button 'Show Tab AND Load User Control' - no user control appeares. If two separate buttons 'Show Tab' and 'Load User Control' are clicked, user control appears correctly.
Look forward to hearing from you!
Thanks!