User Control controls not created when dynamically creating user control

  

Have created a user control for displaying and editing a dynamically number of values. The user control contains two labels and an edit box. When dynamically loading the user control I get an error when the control itself tries to change setting on the fields within the user control.

 

Why doesnt this work? It works okay if I dont load it dynamically but create a user-control-instance in the aspx-page but thats no solution.

 

Thankyou!

 

 

Here is my code:

 

ucDataValue.ascx

 

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucDataValue.ascx.cs" Inherits="ucDataVariableValue" CodeFileBaseClass="System.Web.UI.UserControl" %>

<asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>

<asp:Label ID="lblShowValue" runat="server" Text="Label"></asp:Label>

<asp:TextBox ID="txtEditValue" runat="server"></asp:TextBox>

 

 

ucDataValue.ascx.cs

 

public partial class ucDataVariableValue : System.Web.UI.UserControl

{

private int m_ControlState = 0;

 

public int ControlState

{

set

{

m_ControlState = value;if (m_ControlState == 0)

{

//This row doesnt work... Error: Object reference not set to an instance of an object.

lblShowValue.Visible =
true;txtEditValue.Visible = false;

}

else

{

lblShowValue.Visible = false;txtEditValue.Visible = true;

}

}

get { return m_ControlState; }

}

 

protected void Page_Load(object sender, EventArgs e)

{

}

 

public void setValue(string Value)

{

//Get value from database

if (ControlState == 0)

lblShowValue.Text = Value;

else

txtEditValue.Text = Value;

}

public ucDataVariableValue() { }

}

 

 

CallingPage.aspx

 

<%@Register tagprefix="ucDataValue" Tagname="DataValue" src="~/UserControls/ucDataValue.ascx" %>

 

<asp:Table id="TestTable" runat="server"></asp:Table>

 

 

CallingPage.aspx.cs

 

ucDataVariableValue newDataVariable = new ucDataVariableValue();

newDataVariable.ControlState = 0;

newDataVariable.setValue( "Some Text" );

 

TableRow tr = new TableRow();

TableCell td = new TableCell();

td.Controls.Add(newDataVariable);

tr.Controls.Add(td);

TestTable.Rows.Add(tr);

 

 

0
Shumway
11/29/2007 9:38:20 AM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 1 followers.

💬 3 Replies
👁️‍🗨️ 438 Views

Hi Shumway

When you create your usercontrol dynamic, the user control is not included to your page before you add it to the page. Your problem is that you tyr to set a value to usercontrol before it exits in you page. Try this:

ucDataVariableValue newDataVariable = new ucDataVariableValue();

newDataVariable = (ucDataVariableValue) LoadControl("~/UserControls/ucDataValue.ascx" );

TableRow tr = new TableRow();

TableCell td = new TableCell();

td.Controls.Add(newDataVariable);

tr.Controls.Add(td);

TestTable.Rows.Add(tr);

newDataVariable.ControlState = 0;

newDataVariable.setValue( "Some Text" );

 

 


Best regards,
Ahmad
0
ahmad222
11/29/2007 12:38:39 PM

Thankyou ahmad222 

I was searching on my problem and found a lot of explanations including LoadControl but isnt that just another (not as good) way of doing what I already do? I have defined my control on the page and im creating an instance of my User Control Class. Why would I have to run LoadControl? Ive done the same thing in Windows Forms before and there I dont need to do this extra step. I dont like it since I dont see the point in the nice .net object oriented structure :-)

 

 

 

0
Shumway
11/29/2007 1:48:36 PM

You don't have to do the extra step. You can just do:

ucDataVariableValue newDataVariable = (ucDataVariableValue) LoadControl("~/UserControls/ucDataValue.ascx" );

if you put a break point in the constructor of your user control, you'll see that when this line of code is executed, it will call your constuctor. Besides, this method initializes the components of your user control, and does some extra steps for you. I always use this method to load dynamically user controls.


______________________________________________
Oswaldo Rodriguez
ozinabox.com
0
oswaldorb
11/29/2007 2:00:07 PM
Reply: