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. 6 followers. Follow

3 Replies
2145 Views

Similar Articles

[PageSpeed] 16
Get it on Google Play
Get it on Apple App Store

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:

Similar Artilces:

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

How to do post back in a dynamically created user control [provided that update panel is in the master page of the page where user control is added] {B/C of FileUpload Control}
I have a register my scriptmanager and place the update panel around contenttempalte for my pages int the master page. Now in one of my pages i am adding some user controls dynamically. In two of my user user controls I have FileUpload control which will not work under partial post back. So I want's full postback for them How could I do that {Provided that i am on the user control,which is inside the page ,and page have the contenttemplat...

Bubbling Events on Dynamically created controls that are on dynamically created user-controls on a module!! HELP MAH PUHLEASE!
I may be cross posting this question, but I am really going to go crazy.. Please help if you can!I think I am going to go mad. If anyone can help me with this small issue, I will build an idol in your image made of clay, and have nubile maidens dance in front of it in worship!!!!I have a module. Let's call it Module-1Module-1 Connects to a database and gets a list of items.Module-1 Then creates on the fly a number of buttons that represent that list of items.Each button when clicked dynamically creates a web-user control. Let's pull out 1 button, and 1 usercontrol and call them Button-1 and ...

Problems with UpdatePanel, Web User Control and Dynamic Controls Creating
Hi,In a Page, i have a button that's outside an UpdatePanel, but that triggers him. That UpdatePanel get refreshed with a Web User Control inside it. The Web User Control, has come logic to dynamically create some Controls (TextBox and other simple controls), and he does that very well, and the UpdatePanel get refreshed ok and the controls get rendered to the browser. The problem is when i click in a Button inside the Web User Control (not dynamically created), and i can't get the controls values in the server-side. Before the server side event of the Button Click, i re-create all the contro...

access controls on web user control x from web user control Y
I have 2 web users controls in a page and I would like to get or set the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web user control Y, is this possible? How can I do it? User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

Custom User Controls and dynamically created web controls events not firing
I have a peculiar problem and since I am just starting to use .net I am confident I am doing something wrong, but I can't see it and I've wasted a lot of time so far trying to figure it out. Hopefully one of you can figure it out. I have a custom User Control and I am dynamically creating a RadioButtonList. If I remove the custom user control from the page the RadioButtonList works as expected and correctly. However, once I add the custom control, it does not fire the SelectedIndexChanged event. Below is some code. If all I do is remove: "<UC:TOPMENU id="TopMenu1"...

Reading form values in dynamically created child controls in a user control
I have a common problem for which I've searched the web for answers for hours to no avail. I have some radiobuttonlists ,dropdownlists,checkboxlists and text controls in a user control. The lists are created statically, however the items in the lists are populated at run time. The lists are displayed in default.aspx (the containing page) and populated on the Page_Load event of the user control .ascx file based on an external data source containing arrays of data. I want to be able to read the values selected by the user from deafult.aspx, the parent page. details: The .ascx...

User Control Issue
Dupelicate post dancioffi: Dupelicate post Please refer to this: http://forums.asp.net/t/1398304.aspxGary yang - MSFTMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Web User Control to Web User Control...
Hi, I have 2 User controls on a page and want Control1 to call a method of Control2. Is this Possible? Example: Con1 = Datalist showing products, each with a Quantity Box and a Button. Con2 = Repeater showing Cart Preview (name, Price) in page.aspx. When I click on the button on the DataList in Con1 it adds the item into the Session based Cart. However it does not update the Cart Preview. I need to call BindPreview() in Con2 in order to update the Cart Preview... Does that make sense? argh! any help much appreciated! PeteWeb Design Nottingham, UK Well I can call BindPreview() from m...

how to create an instance of a user control from another user control
Hi,I have an aspx page that declaratively creates an instance of a user control. This user control programmatically creates an instance of another user control. The code is something like below.File 1 -- default.aspx <%@ Register TagPrefix="userControl" TagName="firstControl" Src="~/firstControl.ascx" %>..Same code here.. <userControl:firstControl runat="server" />..File 2 -- firstConrtol.ascx<%@ Reference Control="~/secondControl.ascx" %><script runat="server">   protected override void OnLoad(EventArgs e)   {   ...

Creating a User Control that references another user control
Hi everybody,I have a web page that uses the same controls several times on the same page in different forms. I'm thinking of turning this group of controls into the UserControl so I can re-use them on that page and everythere else.Unfortunately, these controls reference another user control and I'm not sure how can I visually add a separate User Control to the first UserControl.I found this articlehttp://www.dotnetcurry.com/ShowArticle.aspx?ID=155 but it doesn't tell how exactly should I add ControlB for the ControlA.Thanks a lot in advance.Here is what I currently have:<%@ C...

Creating a Web User Control with FreeTextBox Control
 I'm trying to set up a FreeTextBox control in an ASCX file in Visual Studio 2008 with VB.NET (Framework 2). I'm am using Master pages just in case that matters. Anyway... even though I clearly see the control in 'Design' view on the ASPX page, I get the following error:   Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Excep...

Using user controls that contain dynamically-created controls
I have a web form that is split into multiple tabs (I'm using a 3rd-party ajax tabstrip), and I intend to use a user control to hold the contents of each tab. This means that I have to dynamically load the user control whenever the user selects a different tab.On top of that, most of the controls in my user controls will be dynamically generated, because different users get to see different features on my form. I have problems  with timing and events issue with the controls - if I'm using dynamic controls on a webform, I just create them during page on_init event, and every...

How get value from a dynamically created control in a user control
HI, I am working with a version of IBuySpy. On one page is a form that contains a user control that lists the items the user purchased, the subtotal, shipping, etc. I wanted to add a DropDownList from which the user could select the shipper. The dropdown list works fine as long as it has .AutoPostBack = false. However, I cannot figure out how to get the value that the user selected when they finally press the submit button on the form. All attempts to locate the control and it's value have failed. I tried drillingdown from the form to the child controls, and sub child controls. N...

Web resources about - User Control controls not created when dynamically creating user control - asp.net.web-forms

Placebase team at Apple file “Schematic Maps” patent dynamically detailing important data
In a report from Computerworld way back in 2009, we learned that Apple had quietly acquired Maps API company Placebase. Then, founder of Placebase ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

In a dynamically typed language, is it a bad idea to return different data types?
In some cases, returning different types from a single function works.

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ...

Everything.me launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...


A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97%

Everything.me Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 12/3/2015 2:33:15 AM