.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Controls within Base Control are Not initialized

Posted By:      Posted Date: August 31, 2010    Points: 0   Category :ASP.Net
I have an ASP.net control that contains a grid (protected member variable). Grid is populated from a function in the control. I have derived a new user control from this control without any control in it. I added additional functionality to populate grid and called base class function to do further operation on the grid.When I run my code, I get the null reference exception on grid object in the base control.Any idea why this might happen? Am I missing something in control inheritance? at what point do inner controls get created? 

View Complete Post

More Related Resource Links

Accessing the different controls inside a GridView control

how we can access a particular control which resides inside a GridView control. In this article I will show you how you can access different controls inside a GridView control. We will see how we can access a TextBox control, a DropDownList control and a ListBox control. If you are working with ASP.NET 1.X then you might want to check out my article Accessing Different Controls Inside a DataGrid.

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)


Hey guys

I came across this bug using while trying to implement a sitemap

Line 147:						Panel panel = new Panel();
Line 148:						panel.Controls.Add(_viewState);
Line 149:						Page.Form.Controls.Add(panel);
Line 150:						string script = "document.getElementById('" + _viewState.ClientID + "').value = GetViewState__AspNetTreeView('" + Extender.MakeChildId("UL") + "');";
Line 151:						Page.ClientScript.RegisterOnSubmitStatement(typeof(TreeViewAdapter), _viewState.ClientID, script);

[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
   System.Web.UI.ControlCollection.Add(Control child) +8696951
   CSSFriendly.TreeViewAdapter.SaveAdapterViewState() in E:\wwwroot\ThirdParty\CSSFriendly\CSSFriendly_24242\CSSFriendly\TreeViewAdapter.cs:149
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +8901848
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +148
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +148
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +148

Extreme ASP.NET: A Little Bit of Control for Your Controls


Having worked for so many years designing and developing ASP. NET while at Microsoft, it's exciting now to have a venue in which to talk about it. In this new column, Extreme ASP. NET, I'll discuss and demonstrate time-tested techniques and approaches to implementing high-performance, reliable, secure, and user-friendly Web applications with ASP.

Rob Howard

MSDN Magazine May 2005

User Controls and Control Extenders


Two separate questions, but sufficiently intertwined that I'm going to try the two together....

Firstly - User Controls and Behaviours

I have a User Control which contains a few visual controls.  I am attaching some behaviours by using GetScriptDescriptors and am passing some JavaScript to the browser using GetScriptReferences.  That is all rock solid and working well.  However, when developing, I did discover that I couldn't attach the behaviours to the User Control itself - not surprising as rendering a User Control does not create a DOM element which represents the actual control.  So, I have attached my behaviours to a "random" TextBox within the User Control. 

So - first question, is there a better way to do that?  For example, should I always be rendering a SPAN as an element to surround the control?  Not sure what that would do to the robustness of the rendering.  What is the right way to attach a behaviour to a User Control.

Secondly - Accessing those same Behaviours later

Assume I have placed a User Control on the page.  That User Control exposes a Property (actually a Javascript Function of course) which does "something" to the control - for the sake of example we'll say that the function is set_Border(colour). 

Login control - Controls do not exist in current context

Hi all,Trying to set up the Login control as a User control, but none of the controls exist in the current context?None of my code below is recognised? What's going on here please? I've rebuilt the solution many times.if (Membership.ValidateUser(this.UserName.Text, this.Password.Text))        {            FormsAuthentication.RedirectFromLoginPage(UserName.Text, RememberMe.Checked);        }        else        {             Msg.Text = "Login failed. Please check your user name and password and try again.";        }

Controls Disappear in Custom Control Design View

I have a site using eight (so far) custom web user controls  and two of them have started misbehaving in design view.  When designing the control itself some (in one control) or all (in the other) regular controls (labels, text boxes, etc.) disappear from the design view surface.  They can still be seen in the source view and if the control is hosted on a regular .aspx page the control displays properly.  The code window recognizes all controls in the objects dropdown list and I can write appropriate code.  The controls run properly on their hosted pages.  If I switch to Split view and select controls in the source view panel a very small area od the design surface becomes selected (like one pixel for any of us who remember programming old DOS applications with 80-column screens) but no controls can be seen. In short, the controls work in every respect except that I cannot see all or part of the design surface in Design view when editing the controls directly.  I've double-tripple checked the source code for anything that may be causing this and just don't see anything.  (The controls aren't that complicated.)

Custom Control Controls Collection

I have an ASP.NET custom control with several asp:panel controls that have various questions for users in them. When the control loads I need to set a Session object to the default panel (first question). This worked when I had the objects and code in a web form, but I moved them to a custom web control, and now when I try to access that control in the Load event of the custom control it says the default panel control is null. It seems that the controls are not created when the Load event is firing. Evidently the order of loading controls is different between web forms and custom controls. Can someone explain to me where the controls inside the custom control will become available to use? Thanks for the help! 

Select multiple controls on tabpage, Drag & Drop control(s) (WinForms)


I'm wondering, if somebody got some code or thoughts for selecting multiple control displayed on tab page and drag & drop control on tab page it self. Something like on windows desktop, where you can draw a rectangel with mouse cursor to select folder and files. Also, drag folder or files to different location.



DataAnnotation on multiple user controls displaying on wrong control


I have a table that displays a user control on each row. The user controls are strongly typed to a viewmodel which contains DataAnnotation style validation. If one of the rows has invalid data entered (lets say the 3rd row) the data annotation validates display agains the first row/user control.

How do I get the data annotation to show against the relevant user control?

I'd post a pic to show what I mean except it will not display it!.:/

Repeater Control Does not exist in current context, but other controls do


I have an aspx page containing a number of asp:Repeater Controls.

Im not using the default code-behind file that is generated, ive changed this to file in a different folder.

In the code-behind, i populate the repeater controls, all was workign fine until i added another repeater control to the aspx page.
In the code-behind, the intellisense picks up this control and i can access its methods and properties to set the datasource and bind the data. However, when i go to build the project, i get an error "The name Repeater1 does not exist in the current context"

Firstly, all the other repeater controls, declared in exactly the same way, do not throw throw this error.
Secondly, yes the repeater does have a runat="server" tag.
Thirdly, It is not inside any other controls.

I have tried various different methods of accessing this repater instead of just "Repeater1." but each returns null:

Repeater r1 = (Repeater)Page.FindControl("Repeater1") ;
Repeater r1 = (Repeater)Page.Master.FindControl("Repeater1"); 
Repeater r1 = (Repeater)Master.FindControl("Repeater1") ;

Ive no idea why this "doesnt exist in the current context" as all the other controls on this page do, even if i create a literal i get the same problem

The Accordion Control change font controls


Hi, I use an accordion control that contains some aspnet control inside. 

When I collapse the accordion control, all of aspnet controls include labels or textboxes change the font to another differnt, what can I do.

There is the code:

<cc1:Accordion ID="Accordion1"  Width = "100%" runat="server"  FadeTransitions="True" SelectedIndex="0" TransitionDuration="300"  HeaderCssClass="accordionHeader" ContentCssClass="accordionContent">


                <cc1:AccordionPane id="AccordionPane1" runat="server" >

                <Header> Datos Generales de la Póliza</Header>


                         <asp:Label ID="Label6" runat="server" Font-Bold="True" Font-Names="Tahoma" Font-Size="8pt" CssClass="labelFormat"


Acessing controls in usercontrol in javascript function where user control hosted in webpart



1how to acess controls in usercontrol  in javascript function where user control hosted in webpart


2)How to retieve values from javscript code to server side

details as follows

StringBuilder controlString = new StringBuilder();
            controlString.Append("<p>" + CurrentPage.ToString() + "of " + TotalPages.ToString() + " </p>");
            for (int intIndex = 1; intIndex <= TotalPages; intIndex++)
                controlString.Append("<li><a hre

Not able to access child controls in user control using javascript



I have a user control "SettingsControl" containing an ajax:CollapsiblePanelExtender which in turn has a GridView (gridView) and checkBoxes. On top of GridView we have two LinkButtons "Select All" and "Clear All". I have written to enable select all and clear all functionality. Select All should select all the rows in the grid by calling the following JavaScript written on the Client .aspx file.

function SelectAll(chk)
    //get reference of GridView control
    var grid = document.getElementById('<%= SettingsControl1.FindControl("gridView").ClientID %>');
    //variable to contain the cell of the grid
    var cell;

    if (grid.rows.length > 0)
        //loop starts from 1. rows[0] points to the header.
        for (i=1; i<grid.rows.length; i++)
            //get the reference of first column
            cell = grid.rows[i].cells[0];


Tab Control - load user controls in tab dynamically when tab is selected


I have searched the forum and I cant seem to find the answer to my question, although I have tried some of the hints I have found with no luck....

Can someone show me a working sample (with source code) of a page with a TabContainer and several tabs, with each tab having a user control, but the controls are not added/initialized/shown until the appropriate tab is shown/clicked?

Something like this forum's "Write a New Post" page, where the tabs load after I click on them, and an animated picture displays to show that its loading.

The solution needs to add user controls to the page, not just html into a dynamic populate extender.

I have one user control per tab, and is linked (ala master-child relationship) to the page using a property. My user controls perform long-running web service queries, and I dont want to waste database/computing resources loading content into a tab that the user will never look at.   Right now, all tabs load their content when the page loads, even though the first tab is only shown.

As mentioned above, sample code for the page (.aspx and .cs) and the user control (.ascx and .cs) would really help.


How can I create a custom control that adds other controls visible to the page hosting my custom con


I want to create a custom control where I define several specifically named DIV sections.  The code-behind logic of the custom control will perform operations on these named DIV sections.  The page where the custom control is hosted needs to be able to add named controls to the DIV sections and perform operations on these named controls added to the DIV sections.

Let me explain a little further.  In the custom control, I want to have code behind logic that fires the following Page Event and uses a condition to make one or another specifically named DIV section visible or not:

    < _
    ToolboxData("<{0}:BaseDetails runat=""server""></{0}:BaseDetails>") _
    > _
    Public Class BaseDetails
        Inherits UserControl

        Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
            DetailsNameDiv1.Visible = False
            DetailsNameDiv2.Visible = False

Making your own controls - how to make them powerfully modifiable so anyone can use a control templa


If I look at a template for an expander, it always has to have a ContentPresenter in its control template and the control knows to use that for the content shown when the expander is expanded.  From there you can template out the control in all sorts of cool ways. 

A textbox's control templates need a scrollviewer with x:Name=PART_ContentHost for example.

The expander is fine with an unnamed control template, but the textbox requires a named scrollviewer.  I haven't messed around with putting multiple content presenters in an expander's control template and I currently have no idea what that would do.  This aspect of building my own controls is still quite foggy to me.  If I were to build a control, how do I determine if it looks for a named element in the control template (ala the textbox) or if it just finds a content presenter etc?

What if I wanted my new control to always contain several elements?

For example, with my current set of questions I am working on a resizable element.  I want that to always contain a draggable left and right boundary, as well as a togglable UI element for each of those boundaries that will toggle certain behaviors when I drag a specifc size and resize the control.  So ideally, I would want to define all of those things in a control template.  I would like it to

Problems with Repeater Control, Update Panel and the Toolkit controls


Hi Guys,


Having a bit of an issue with the Toolkit controls.

I have a Repeater Control nested inside the Update panel, the repeater item template contains a multiview control allowing users to flick through seperate bits of information. This was working fine the async update was perfect, no issues.

I have then decided to include the rating control to one of the views. problem is now everytime i want to change the view it causes the whole page to postback.  

Attached is the code.

Any help is much appreciated

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend