Datagrid Dynamic Template Columns with a Data-Driven User Control


I am writing a dynamic datagrid that contains X number of columns. The columns are of type Template. Each cell contains a Web user control (which is a database-driven dropdown list). The problem I am having is that when I dynamically create the colum I have to data-bind the dropdown list and apperently it does not it exists. The Web user control works like a champ if I call it directly from another page.
This is the error:
Object reference not set to an instance of an object.
Public Sub Load_List()
Line 44:
Line 45: Histopathology_Values_List.DataSource = DM.Get_DataReader "LP1_Get_Histopathology_Values", Session("LoginID"), Session("User_pwd"))
The code that initiates the creation is:

For Looper = 1 To A_Columns
Dim NewCol As New TemplateColumn
NewCol.ItemTemplate = New Histopathology_Value_Template(ListItemType.Item, Looper.ToString, 1)
NewCol.HeaderTemplate = New Histopathology_Value_Template(ListItemType.Header, Looper.ToString, 1)

The Template Class is:
Sub New(ByVal type As ListItemType, ByVal ColName As String, ByVal IValue As Integer)
templateType = type
columnName = ColName
InitialValue = IValue
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim lc As New Literal
Select Case templateType
Case ListItemType.Header
lc.Text = "" & columnName & ""
Case ListItemType.Item
Dim Histopathology_Values_List As New UC_Histopathology_Values_list
Histopathology_Values_List.Histopathology_Value = 1
************* Call is made here ***********
************* Call is made here ***********
End Select
End Sub
My user control is (nutshell):
Public Sub Load_List()
***** It breaks here *****
Histopathology_Values_List.DataSource = DM.Get_DataReader
("LP1_Get_Histopathology_Values", Session("LoginID"), Session("User_pwd"))
Histopathology_Values_List.DataTextField = "Histopathology_Value"
Histopathology_Values_List.DataValueField = "Histopathology_Value_ID"
Histopathology_Values_List.Items.Insert(0, "NR")
If Histopathology_Value = 0 Then
Histopathology_Values_List.SelectedIndex = 0
Histopathology_Values_List.SelectedIndex = Histopathology_Values_List.Items.IndexOf(Histopathology_Values_List.Items.FindByValue(Histopathology_Value))
End If

End Sub

Thank you in advance

12/8/2003 6:41:18 PM 72751 articles. 3 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 35

I think you problem stems from the fact that when your usercontrol is being created inside the datagrid, the DM object is not in scope and that is the source of your error.

What you should do is give your user control a property of type DM, and set that in the OnItemDataBound event from the datagrid.
The user control I suspect is fine, since it works in a standalone fashion, but you are having a scope problem inside the datagrid.
12/9/2003 1:48:50 AM
Thank you for your response. However, I thought that was the problem as well and got rid off the DM and created a DS using the DM and then set the datasdource to the new DS and still the problem persists. It does not know the dropdown list. If I do not call the load list it does not crash, but when I see the code behind, there are no ddl rendered.

12/9/2003 4:37:26 PM
I just wanted to close this thread with the answer.

Point 1. You cannot recursively instantiate
In the InstantiateIn class I added the following addhandler to the bottom
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Case ListItemType.Item
Dim Local_Histopathology_Values_List As New DropDownList
AddHandler Local_Histopathology_Values_List.DataBinding, AddressOf TemplateControl_DataBinding

End Sub
Private Sub TemplateControl_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Local_Histopathology_Values_List As DropDownList
Dim container As DataGridItem
Local_Histopathology_Values_List = CType(sender, DropDownList)
Local_Histopathology_Values_List.DataSource = vHistopathology_Values.DataSource
Local_Histopathology_Values_List.DataTextField = vHistopathology_Values.DataTextField
Local_Histopathology_Values_List.DataValueField = vHistopathology_Values.DataValueField
Local_Histopathology_Values_List.Enabled = EditingMode
container = CType(Local_Histopathology_Values_List.NamingContainer, DataGridItem)
If DataBinder.Eval(container.DataItem, columnHeader) Is DBNull.Value Then
Local_Histopathology_Values_List.SelectedIndex = 0
Local_Histopathology_Values_List.SelectedIndex = Int32.Parse(DataBinder.Eval(container.DataItem, columnHeader))
End If
End Sub
Point 2
Just by adding the addhandler would cause another problem: unable to fieldcount. In order to save this one, I changed from a datareader to a dataset (DM.Get_Dataset(.....
12/15/2003 7:58:14 PM

Similar Artilces:

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&...

Data presentation control in another Data presentation control
 Hello,I want to make a menu with datalist or gridview control.Basically,the menu will be like this,Categories     Sub Categories.For example, for COMPUTING category there will be NOTEBOOKS DESKTOPS etc.There will be a image for CATEGORIES and linkbuttons with Subcategories.If I put a datalist inside a datalist and a link button inside the second datalist visually it satisfies my need.However,CommandArguments come from database to linkbutton.Andwhen one of these command buttons is cliked it must go to proper page.I dont know how to wrie an event_handler ,specifically, for...

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...

UpdatePanel within a web user control resets data of controls outside the user control
Hello, I have a usercontrol on my page which displays a running marquee text. It makes use of an UpdatePanel (within the user control) to refresh the marquee text (within the updatepanel) at periodic intervals. For this I added a timer control and its tick event as an async postback trigger for the UpdatePanel. I have a lot of textboxes and dropdown list boxes (all with runat=server) on the page outside the above user control. These are also contained in different UpdatePanels. Problem is, when the user control does a postback to refresh the marquee text, it causes some of...

Controlling a variable numbers of controls dynamically added to a user control
 Ok, thinking caps on people, this has been causing me grief for a couple of days now. I have an .aspx page which contains various user controls housed in an Accordion control from the Ajax Toolkit and lots of UpdatePanels, these user controls all interact with each other through the parent page, e.g clicking buttons in one control effects visibility of other controls. Two of these controls contain just a series of ImageButtons. In the first of these controls the ImageButtons are generated in the page_load method and added to a panel using Panel.Controls.Add(ImageButtonCont...

Dynamically instantiated user controls within user controls.....
Hello all,So, I have created a user control that dynamically/programatically creates many instances of another user control.  Everything is fine at design time, and everything compiles just fine.  However, at runtime, when my code tries to update the values of say an asp:HiddenField in the first dynamically created user control, I get an "Object reference not set to an instance of an object" exception. To illustrate:MyParentUserControl.ascx.cs:-----------------protected void Page_PreRender(object sender, EventArgs e){  WEB.Controls.ChildControl myCC = new WEB.Co...

Bind Data to an Image Control not in a Data Presentation Control
I've been searching for two days but I don't really know what to search for.I'm making a site for a photographer.  The Default.aspx page displays Albums by showing a title, image and description from the database table Albums. Click an album to view the contents and it passes the QueryString which is AlbumName.  The ViewAlbum.aspx page uses the QueryString to display an UpdatePanel holding a Large Image and a Repeater full of dynamically filled ImageButtons for thumbs.  Both are from the database table Photos and are retrieved using an ObjectDataSource. Click the ...

dynamically load user control from another user control
Is it possible to dynamically load a user control from another user control?  I have an aspx page where I have declaratively included a user control.  Now, within that user control I want to dynamically load another usercontrol onto the page based on some business logic.  Does this even make sense? So, here's the scenario:aspx page:<%@ Register TagPrefix="header" TagName="links" Src="~/usercontrols/toplink.ascx" %><asp:Content ID="ContentBody" ContentPlaceHolderID="cphBody" runat="Server">  ...

User Control Passing Data To Another User Control?
I got two user controls in a .aspx page. one user control contains a list of names along with the user id as that listbox .value property. now when the user selects a user a clicks on a command button it needs to send the value of the selected user name to another user control in the same form name shippingInfo; Anyone know how to do this; I keep getting a "Object reference not set to an instance of an object" error message here is the code for the MyContacts control (the one with the user name and user id value) Imports PackageDeliveryVB.ShippingRequestInfo Public Class...

Help with dynamic controls and retrieving data from dynamic controls
I am new to web development. My requirement is Text boxes get generated dynamically based on user input on click of a button called Ok after user enters values to these textboxes when user clicks on submit button ,values user entered to these textboxes have to be retrieved for further calculation I am generating text boxes in the ok button click event how to retrieve value user enters to these n where the procedure for that to be placed here u need to give name to id of every texbox in a specugic patterns so than u can know next time.   static int i = 0;protected...

problem with User Control position in DataGrid template column
I have a data bound user control, which includes an Image button and a label it all works fine but in the DataGrid it only appears in the first cell.As the DataGrid is populated the image button seems to load the correct number of times for the rows but does not go down the page just loads on top of its self. I am using an external style sheet to control the label display properties I believe it is due to the position which is set as Absolute for the button and label to keep the label on top of the image and I guess what I want to do is set the position attribute to relative for the c...

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...

Should I use DataGrid(add dynamic control) or just dynamic control?
Hi, I am debating between using just dynamic control's or actually adding the control's dynamically into a datagrid.  I have tried with just adding dynamically, but I am having so much trouble because the selectedindexchanged even it firing for no reason.  I have asked a lot of people, but nobody knows the answer for that and I searched in the web and I am not getting anything either. Now someone suggested to use DataGrid and add the control's dynamically.  In each row, I have a dynamic column, which could be 2 and above.  It will depends on a number that I get fromt he ...

user control to user control
I have an aspx page that have a "Register" directive to two usercontrol called rptMaster.ascx and rptDetail.ascx. I'm trying to set up a Master-Detail relationship and wish for the rptMaster.ascx to trigger and event from a dropdownlist and for the rptDetail to be listening for DropDownList event. I believe I have gotten the events part okay with help from this articel but my declaration for a private statement to my rptMaster class is producing an error. I'm using beta 2 of and I 'm having trouble getting a reference TO ...

Web resources about - Datagrid Dynamic Template Columns with a Data-Driven User Control -

Data driven journalism - Wikipedia, the free encyclopedia
Data-driven journalism strives to reach new levels of service for the public, helping consumers, managers, politicians to understand patterns ...

UX Week 2010 - Adam Mosseri - Data Informed, Not Data Driven - YouTube
At Facebook, analytics play a critical role in informing design decisions, but internally there's a wariness of the idea of design by numbers. ...

New CSIRO division to focus on data-driven research
The CSIRO has created a new research department that will focus on computational and information sciences.

Is Online Advertising Getting Too Complex? - Data-Driven Marketing - Advertising Age
Corporate Subscriptions or Site Licenses are also available . For questions, comments or other service issues, please contact our Customer Service ...

Evan Williams's Advice to Start-Ups: Don't Be Too Data-Driven
... down there worth fighting the dragons for, because if you don’t, you’ll never do anything good. I think it’s kind of problematic how data-driven ...

Data-Driven Dating, Ctd
While Amy Webb found her perfect match through data, Josh Freedman at McSweeney’s is using it to call things off: This may feel cold, but there’s ...

Best Tech Events This Week — Tribeca Film Festival Innovation Week, Mobile Week, Data Driven, SendGrid ...
... engagement, avoid spam traps, implement data-rich APIs & increase revenue! Tuesday (Apr 15), 2 p.m. @ Ace Hotel, 20 W 29th St Data Driven NYC ...

Venngage Launches to Help Marketers Create Data-Driven Visual Reports
The rise of big data has been well-documented this year, and along with it the number of companies, from DataHero to Visually, that are trying ...

Oracle sets out to redefine the data-driven enterprise
Data makes the modern world go round and by being able to use it effectively companies can gain a competitive advantage, improve customer experience ...

Resources last updated: 11/29/2015 4:46:35 PM