Dynamic User Control - losing value

N

NKaufman

have two user controls - Control3 and Control4. In Control4, I am
creating several instances of Control3 and initially only want the
parent to be visible. At the click event, I am setting the visible
property of the child nodes and also saving this in ViewState.
However, on the page load I do not see this ViewState value e.g. If I
save ViewState["Control20"]=1, then on postback I see that the value
is null.

Of course I am using Control4 on another page.

"File: control3.ascx"
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="Control3.ascx.cs" Inherits="Control3" %>

<asp:Table ID="myTable" runat="server" Width="100px" GridLines="None"
CellPadding="0" CellSpacing="0" BorderStyle="Dotted"
Height="50px">
<asp:TableRow runat="server">
<asp:TableCell runat="server" >
<asp:Table runat="server" >
<asp:TableRow>
<asp:TableCell >
<asp:Label ID="lblC" runat="server"
Width="100px"></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="Label1" runat="server"
Width="100px" Text="Address"></asp:Label>
</asp:TableCell>
</asp:TableRow>
</asp:Table>

</asp:TableCell>
</asp:TableRow>

<asp:TableRow runat="server">
<asp:TableCell Width="40px" runat="server">
&nbsp;
</asp:TableCell>
<asp:TableCell Width="20px" runat="server" >
<asp:Button ID="myButton" runat="server" />
</asp:TableCell>
<asp:TableCell Width="40px" runat="server">
&nbsp;
</asp:TableCell>
</asp:TableRow>
</asp:Table>

File: CodeBehind for above
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Control3 : System.Web.UI.UserControl
{
private DataRow myDataRow;
private int myLevel;
private int myID;
private int myParentID;
private int myDisplay;
public event EventHandler BubbleClick;

public DataRow MyDataRow
{
get
{
return myDataRow;
}
set
{
myDataRow = value;
}
}

public int MyLevel
{
get
{
return myLevel;
}
set
{
myLevel = value;
}
}

public int MyID
{
get
{
return myID;
}
set
{
myID = value;
}
}

public int MyParentID
{
get
{
return myParentID;
}
set
{
myParentID = value;
}
}

public int MyDisplay
{
get
{
return myDisplay;
}
set
{
myDisplay = value;
}
}

protected void Page_Load(object sender, EventArgs e)
{
myButton.Click += new EventHandler(Button1_Click);
lblC.Text = MyDataRow["CGains"].ToString();
this.MyID = (int)MyDataRow["ID"];
if ((int)MyDataRow["ParentID"] != -1)
this.MyParentID = (int)MyDataRow["ParentID"];
}

protected void Button1_Click(object sender, EventArgs e)
{
OnBubbleClick(e);
}

protected void OnBubbleClick(EventArgs e)
{
if (BubbleClick != null)
{
BubbleClick(this, e);
}
}
}



Control4.ascx
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="Control4.ascx.cs" Inherits="Control4" %>
<%@ Register Src="Control3.ascx" TagName="Control3" TagPrefix="uc3" %>
<%@ Reference Control="Control3.ascx" %>


<asp:panel ID="Panel1" runat="server" Height="50px" Width="1200px" >
</asp:panel>

<br />
<br />

<asp:panel ID="Panel2" runat="server" Height="50px"
Width="1200px" >
</asp:panel>

<br />
<br />

<asp:panel ID="Panel3" runat="server" Height="50px"
Width="1200px" >
</asp:panel>


CodeBehind for above
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Control4 : System.Web.UI.UserControl
{
private DataSet ds = new DataSet();

protected override void OnInit(EventArgs e)
{
GetParentData();
GetChildData();
GetGrandChildData();
AddControls();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

}
}

private void GetParentData()
{
DataTable Table1 = ds.Tables.Add("Parent");
Table1.Columns.Add("ID", typeof(int));
Table1.Columns.Add("ParentID", typeof(int));
Table1.Columns.Add("CGains", typeof(string));

Table1.Rows.Add(new object[] { 11, -1, "Parent CGains"});
}

private void GetChildData()
{
DataTable Table2 = ds.Tables.Add("Child");
Table2.Columns.Add("ID", typeof(int));
Table2.Columns.Add("ParentID", typeof(int));
Table2.Columns.Add("CGains");

Table2.Rows.Add(new Object[] {12, 11, "Child1 CGains"});
Table2.Rows.Add(new Object[] {13, 11, "Child2 CGains"});
Table2.Rows.Add(new object[] {14, 11, "Child3 CGains"});
}

private void GetGrandChildData()
{
DataTable Table3 = ds.Tables.Add("GrandChild");
Table3.Columns.Add("ID", typeof(int));
Table3.Columns.Add("ParentID", typeof(int));
Table3.Columns.Add("CGains");

Table3.Rows.Add(new object[] { 18, 12, "GrandChild1
CGains" });
Table3.Rows.Add(new object[] { 19, 12, "GrandChild2
CGains" });
Table3.Rows.Add(new object[] { 20, 13, "GrandChild3
CGains" });
Table3.Rows.Add(new object[] { 21, 13, "GrandChild4
CGains" });
Table3.Rows.Add(new object[] { 22, 14, "GrandChild5
CGains" });
}

private void AddControls()
{
int i = 0;
foreach (DataRow myrow in ds.Tables["Parent"].Rows)
{
Control3 MyControl1 =
(Control3)LoadControl("Control3.ascx");
MyControl1.ID = "Control1" + i;
Panel1.Controls.Add(MyControl1);
MyControl1.BubbleClick += new
EventHandler(Default3_BubbleClick);
MyControl1.MyDataRow = myrow;

if (ViewState[MyControl1.ID] != null)
{
MyControl1.MyDisplay = (int)ViewState[MyControl1.ID];
}
i++;
}


int j = 0;
foreach (DataRow myrow in ds.Tables["Child"].Rows)
{
Control3 MyControl2 =
(Control3)LoadControl("Control3.ascx");
MyControl2.ID = "Control2" + j;
Panel2.Controls.Add(MyControl2);
MyControl2.BubbleClick += new
EventHandler(Default3_BubbleClick);
MyControl2.MyDataRow = myrow;

if (ViewState[MyControl2.ID] != null)
{
MyControl2.MyDisplay = (int)ViewState[MyControl2.ID];
}
else
{
MyControl2.MyDisplay = 0;
ViewState[MyControl2.ID] = 0;
}

if (MyControl2.MyDisplay == 0)
MyControl2.Visible = false;
else
MyControl2.Visible = true;

j++;
}

int k = 0;
foreach (DataRow myrow in ds.Tables["GrandChild"].Rows)
{
Control3 MyControl3 =
(Control3)LoadControl("Control3.ascx");
MyControl3.ID = "Control3" + k;
Panel3.Controls.Add(MyControl3);
MyControl3.BubbleClick += new
EventHandler(Default3_BubbleClick);
MyControl3.MyDataRow = myrow;
MyControl3.MyDisplay = 0;

if (ViewState[MyControl3.ID] != null)
{
MyControl3.MyDisplay = (int)ViewState[MyControl3.ID];
}
else

{
MyControl3.MyDisplay = 0;
ViewState[MyControl3.ID] = 0;
}

if (MyControl3.MyDisplay == 0)
MyControl3.Visible = false;
else
MyControl3.Visible = true;

k++;
}
}

private void Default3_BubbleClick(object sender, EventArgs e)
{
Control3 MyC = (Control3)sender;
int ParID = MyC.MyID;

foreach (Control myC in Page.Controls)
{
SetDisplay(myC, ParID);
}
}



public void SetDisplay(Control baseControl, int ParID)
{
foreach (Control c in baseControl.Controls)
{
if (c.GetType().ToString().Equals("ASP.control3_ascx"))
{
Control3 MyC = (Control3)c;
if (MyC.MyParentID == ParID)
{
Response.Write("Found control: " +
MyC.ID.ToString() + "<br />");
if (MyC.MyDisplay == 0)
{
MyC.MyDisplay = 1;
MyC.Visible = true;
ViewState[MyC.ID] = "1";
}

else
{
MyC.MyDisplay = 0;
MyC.Visible = false;
ViewState[MyC.ID] = "0";
SetDisplay(c, MyC.MyID);
}
}
}

if (c.HasControls())
SetDisplay(c, ParID);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top