Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Problem with User Control and unset references to controls within

Reply
Thread Tools

Problem with User Control and unset references to controls within

 
 
Sam Martin
Guest
Posts: n/a
 
      03-30-2006
Hi,

I have got a User Control that contains for the sake of argument, a single
DataList control.

eg.
<aspataList id="DataList1" runat="server" RepeatDirection="Horizontal"
RepeatColumns="4"
Width="100%" GridLines="Vertical">
<ItemTemplate>
asdf
</ItemTemplate>
</aspataList>

VS.NET 2003 automatically puts in the code behind:

protected System.Web.UI.WebControls.DataList DataList1;

in the Page_Load event of the control, this.DataList1 is always undefined.

I've tried this again with other controls, created new really simple test
control to see if the same thing happens, and it does. I can't seem to be
able to get a reference to the Web Control held within the UserControl.

I must be missing something yeah?

Please help

--
TIA
Sam Martin
 
Reply With Quote
 
 
 
 
CaffieneRush@gmail.com
Guest
Posts: n/a
 
      03-30-2006
Hi,

How are did you load the user control - declaratively or
programmatically?

Regards.

 
Reply With Quote
 
 
 
 
Sam Martin
Guest
Posts: n/a
 
      03-30-2006
Thx for reply,

i should have said, declaratively

e.g.

<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<Nc:ItemDataList ID="MyItemDataLisy" runat="server"></Nc:ItemDataList>

</form>
</body>
--
TIA
Sam Martin


"(E-Mail Removed)" wrote:

> Hi,
>
> How are did you load the user control - declaratively or
> programmatically?
>
> Regards.
>
>

 
Reply With Quote
 
CaffieneRush@gmail.com
Guest
Posts: n/a
 
      03-30-2006
How did you find out that DataList1 is undefined?
Did you set a breakpoint in your user control's Page_Load and examined
DataList?

Regards.

 
Reply With Quote
 
Sam Martin
Guest
Posts: n/a
 
      03-30-2006
yes, exactly that. same applied for other controls i declared in the ascx
file.

here's a proper listing to help, on the page_load, where i'm setting the
datasource of the DataList is where the exception is thrown.

Although the DataListMain variable is declared, and the <aspataList> in
the ascx file, the refernece is always undefined. Is there anything i'm
missing from the delcaration in the host Page (aspx) file?

ASCX File

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="ItemDataList.ascx.cs" Inherits="MyNamespace.ItemDataList"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<aspataList id="DataListMain" runat="server" RepeatDirection="Horizontal"
RepeatColumns="4"
Width="100%" GridLines="Vertical">
<ItemTemplate>
Template item
</ItemTemplate>
</aspataList>

ASCX.CS

namespace MyNamespace
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for ItemDataList.
/// </summary>
public class ItemDataList : System.Web.UI.UserControl
{
public System.Web.UI.WebControls.DataList DataListMain;

private object ds;

/// <summary>
/// Gets or sets the data source.
/// </summary>
/// <value>The data source.</value>
public object DataSource
{
set { ds = value; }
get { return this.ds; }
}

private void Page_Load(object sender, System.EventArgs e)
{
// EXCEPTION THROWN HERE as
// this.DataListMain is UNDEFINED!!
this.DataListMain.DataSource = ds;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

it's such a simple thing i'm trying to, i don't know what's wrong though.

Thanks for your reply

--
TIA
Sam Martin


"(E-Mail Removed)" wrote:

> How did you find out that DataList1 is undefined?
> Did you set a breakpoint in your user control's Page_Load and examined
> DataList?
>
> Regards.
>
>

 
Reply With Quote
 
CaffieneRush@gmail.com
Guest
Posts: n/a
 
      03-30-2006
One last thing, the datalist as written will not display on the client
browser if it is not bound to a data source.
However, if you put a breakpoint in the user control's Page_Load
handler, you should still see that the datalist is defined.

 
Reply With Quote
 
Sam Martin
Guest
Posts: n/a
 
      03-30-2006
yes, you're right. i noticed just after i posted that i'd left out the
DataBind call.

thanks for your help, it's often by have someone ask you questions about it,
that you spot mistakes/ try other ways.

i've just tried adding the control again, this time by dragging it from the
solution explorer and not manually declaring it. not sure what difference
this would have made, but the reference to the object is now there!!

<%@ Register TagPrefix="uc1" TagName="ItemDataList" Src="ItemDataList.ascx" %>

^^ is the Register tag VS put in, only noticable difference is that the
TagPrefix is different and the MyNamespace isn't in the Src. I dunno, it's
working now anyway

Thanks for your help

--
Sam Martin


"(E-Mail Removed)" wrote:

> One last thing, the datalist as written will not display on the client
> browser if it is not bound to a data source.
> However, if you put a breakpoint in the user control's Page_Load
> handler, you should still see that the datalist is defined.
>
>

 
Reply With Quote
 
CaffieneRush@gmail.com
Guest
Posts: n/a
 
      03-30-2006
Glad to be of help. (Instead of studying for the 229 exam, I'm
procrastinating by helping in a newsgroup

Regards,
Andy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unset TCL_LIBRARY and TK_LIBRARY Wolfman Python 1 04-05-2010 04:15 PM
Ant idiom for signaling error on unset properties? nyenyec Java 0 01-26-2007 05:24 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Problem with User Control and unset references to controls within Sam Martin ASP .Net Building Controls 1 03-30-2006 07:03 PM
Altenative of PHP Function "Unset" in ASP fasanay@yahoo.com ASP .Net 7 10-22-2004 08:11 PM



Advertisments