NOT ANSWERED BEFORE (and pretty desperate!): Trouble using user control code-behind class name to load user controls
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngbeta' list.


Michael Bunger
Hi,

I've read many threads on this subject, and I tried to follow the examples
in them, but I'm still getting the error: The type or namespace name
'OptionsSelectorRow' does not exist in the class or namespace
'DCS.OptionsSelector'

So, I'm probably doing something stupid or have overlooked something.
Anyway, what I have is a user control with a code behind class written in vb
called OptionsSelectorRow (files are OptionsSelectorRow.ascx,
OptionsSelectorRow.vb). In my test form code behind file, written in c#,
OptionsSelector.cs (form file is OptionsSelector.aspx), in the page load
event, I am attempting to load three of the User controls with the following
line:

OptionsSelectorRow ctlOptionsSelectorRow = (OptionsSelectorRow)
LoadControl("OptionsSelectorRow.ascx");

But, this gives me the error mentioned above. However, when I replace the
line with the following, it works:
System.Web.UI.UserControl ctlOptionSelectorRow =
Page.LoadControl("OptionsSelectorRow.ascx");

So, with the loading at least working, I then try to set some properties
using the following code, but I get the error The type or namespace name
'OptionsSelectorRow_ascx' does not exist in the class or namespace
'DCS.OptionsSelector':
((OptionsSelectorRow_ascx)ctlOptionSelectorRow).msg = i.ToString();

So, this made me think I must put the user control class in a namespace and
import it in the OptionsSelector.cs file. I tried that and it doesn't work.
So I'm a little lost and confused and pretty sure that I'm missing something
major. So will anyone point out to me the problem causing both of the
errors? Any ideas and comments will be greatly appreciated. All code
below.

Sincere thanks,
Michael Bunger

TEST FORM GUI (OptionsSelector.aspx)
==================================
<%@ Page Trace="False" Debug="true" language="C#"
Codebehind="OptionsSelector.cs" src="OptionsSelector.cs"
inherits="DCS.OptionsSelector" %>
<%@ Import Namespace="System.Data" %>
<%@ Register TagPrefix="DCSControls" TagName="OptionsSelectorRow"
Src="OptionsSelectorRow.ascx" %>

<html>
<head>
<link rel="stylesheet" type="text/css" href="font.css">
</head>

<body>

</body>
</html>

TEST FORM CODE BEHIND (OptionsSelector.cs)
===========================================
namespace DCS {

using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class OptionsSelector : System.Web.UI.Page { // TODO__NEW: name
must match inherits field on aspx page.

protected void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}

private void InitializeComponent()
{
this.Load += new System.EventHandler (this.Page_Load);
}

void Page_Load(Object sender, EventArgs e)
{

for(int i = 0; i<3; i++)
{
// load in the User Control template
// this line DOES NOT work: OptionsSelectorRow ctlOptionsSelectorRow =
(OptionsSelectorRow) LoadControl("OptionsSelectorRow.ascx");
// the following line DOES work
System.Web.UI.UserControl ctlOptionSelectorRow =
Page.LoadControl("OptionsSelectorRow.ascx");

// The following line DOES NOT work.
((OptionsSelectorRow_ascx)ctlOptionSelectorRow).msg = i.ToString();

// add this product to the page
Page.AddParsedSubObject(ctlOptionSelectorRow);

}

if (!IsPostBack)
{
}

} // end of Page_Load

} // Of OptionsSelector class.

} // Of namespace.

USER CONTROL GUI (OptionsSelectorRow.ascx)
============================================
<%@ Control Language="vb" Codebehind="OptionsSelectorRow.vb"
Src="OptionsSelectorRow.vb" Inherits="OptionsSelectorRow" %>

<!-- GENERATES A SINGLE LINE -->
<table border="0" cellpadding="0" cellspacing="3" style="border-collapse:
collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
<tr>
<td class=Font>OPT#</td>
<td class=Font>OPTION_DESCRIPTION</td>
<td class=Font align="right">
<asp:TextBox id=t1 runat="server" />
<img border="0" src="images/camera.gif"> 
<img border="0" src="images/deposit.gif"> 
<img border="0" src="images/exclaim_red.gif"> 
<img border="0" src="images/exclaim_yel.gif"> 
<img border="0" src="images/comment.gif"> 
</td>
<td align="center"><select size="1" name="cboSelect"
Class="normalcboBox" style="width=120"><option
selected>----</option><option>Select</option><option>Thru
Sales</option><option>Included</option><option>Cancelled</option></select></
td>
<td align="center"><input type="text" name="txtQuantity"
Class="normaltextBox" style="width=40" size="20"></td>
<td class=Font align="right">UNIT_$</td>
<td class=Font align="right">TOT_$</td>
</tr>
</table>

USER CONTROL CODE BEHIND (OptionsSelectorRow.vb)
=================================================
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class OptionsSelectorRow
Inherits UserControl

Public TextValue as String ' Databind to the TextValue property of
the user control instance
Protected t1 as TextBox ' You need an instance for the control in
code-behind

Property msg as String
     Get
         Return t1.Text
     End Get

     Set
         t1.Text = Value
     End Set
    End Property

Sub Page_Load (s as Object, e as EventArgs)

If Not Page.IsPostBack Then

If TextValue <> "" Then
t1.Text = TextValue
End If

End If

End Sub

End Class

Reply to this message...
 
    
David Ebbo
Actually, your first attempt should work:

    OptionsSelectorRow ctlOptionsSelectorRow =3D (OptionsSelectorRow)
LoadControl("OptionsSelectorRow.ascx");

however, for it to work, you need to:
- first compile OptionsSelectorRow.vb into its own assembly, say
OptionsSelectorRow.dll
- then compile OptionsSelector.cs with a reference to
OptionsSelectorRow.dll (/r:OptionsSelectorRow.dll).

As for your second attempt, you cannot expect the type
OptionsSelectorRow_ascx to be available from OptionsSelector.cs, because
OptionsSelectorRow_ascx gets compiled on the fly at *runtime*, while
OptionsSelector.cs gets pre-compiled ahead of time.

Hope this helps,
David;

-----Original Message-----
From: Michael Bunger [mailto:Click here to reveal e-mail address]=20
Sent: Tuesday, May 01, 2001 9:18 PM
To: aspngbeta
Subject: [aspngbeta] NOT ANSWERED BEFORE (and pretty desperate!):
Trouble using user control code-behind class name to load user controls

Hi,

I've read many threads on this subject, and I tried to follow the
examples in them, but I'm still getting the error: The type or
namespace name 'OptionsSelectorRow' does not exist in the class or
namespace 'DCS.OptionsSelector'

So, I'm probably doing something stupid or have overlooked something.
Anyway, what I have is a user control with a code behind class written
in vb called OptionsSelectorRow (files are OptionsSelectorRow.ascx,
OptionsSelectorRow.vb). In my test form code behind file, written in
c#, OptionsSelector.cs (form file is OptionsSelector.aspx), in the page
load event, I am attempting to load three of the User controls with the
following
line:

OptionsSelectorRow ctlOptionsSelectorRow =3D (OptionsSelectorRow)
LoadControl("OptionsSelectorRow.ascx");

But, this gives me the error mentioned above. However, when I replace
the line with the following, it works: System.Web.UI.UserControl
ctlOptionSelectorRow =3D Page.LoadControl("OptionsSelectorRow.ascx");

So, with the loading at least working, I then try to set some properties
using the following code, but I get the error The type or namespace name
'OptionsSelectorRow_ascx' does not exist in the class or namespace
'DCS.OptionsSelector':
((OptionsSelectorRow_ascx)ctlOptionSelectorRow).msg =3D i.ToString();

So, this made me think I must put the user control class in a namespace
and import it in the OptionsSelector.cs file. I tried that and it
doesn't work. So I'm a little lost and confused and pretty sure that I'm
missing something major. So will anyone point out to me the problem
causing both of the errors? Any ideas and comments will be greatly
appreciated. All code below.

Sincere thanks,
Michael Bunger

TEST FORM GUI (OptionsSelector.aspx) =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D
<%@ Page Trace=3D"False" Debug=3D"true" language=3D"C#"
Codebehind=3D"OptionsSelector.cs" src=3D"OptionsSelector.cs"
inherits=3D"DCS.OptionsSelector" %> <%@ Import Namespace=3D"System.Data" =
%>
<%@ Register TagPrefix=3D"DCSControls" TagName=3D"OptionsSelectorRow"
Src=3D"OptionsSelectorRow.ascx" %>

<html>
<head>
<link rel=3D"stylesheet" type=3D"text/css" href=3D"font.css">
</head>

<body>

</body>
</html>

TEST FORM CODE BEHIND (OptionsSelector.cs)
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
namespace DCS {

using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class OptionsSelector : System.Web.UI.Page { // TODO__NEW:
name must match inherits field on aspx page.

protected void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}

private void InitializeComponent()
{
this.Load +=3D new System.EventHandler (this.Page_Load);
}

void Page_Load(Object sender, EventArgs e)
{

for(int i =3D 0; i<3; i++)
{
// load in the User Control template
// this line DOES NOT work: OptionsSelectorRow ctlOptionsSelectorRow =
=3D
(OptionsSelectorRow) LoadControl("OptionsSelectorRow.ascx");
// the following line DOES work
System.Web.UI.UserControl ctlOptionSelectorRow =3D
Page.LoadControl("OptionsSelectorRow.ascx");

// The following line DOES NOT work.
((OptionsSelectorRow_ascx)ctlOptionSelectorRow).msg =3D
i.ToString();

// add this product to the page
Page.AddParsedSubObject(ctlOptionSelectorRow);

}

if (!IsPostBack)
{
}

} // end of Page_Load

} // Of OptionsSelector class.

} // Of namespace.

USER CONTROL GUI (OptionsSelectorRow.ascx)
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
<%@ Control Language=3D"vb" Codebehind=3D"OptionsSelectorRow.vb"
Src=3D"OptionsSelectorRow.vb" Inherits=3D"OptionsSelectorRow" %>

<!-- GENERATES A SINGLE LINE -->
<table border=3D"0" cellpadding=3D"0" cellspacing=3D"3"
style=3D"border-collapse: collapse" bordercolor=3D"#111111" =
width=3D"100%"
id=3D"AutoNumber1">
<tr>
<td class=3DFont>OPT#</td>
<td class=3DFont>OPTION_DESCRIPTION</td>
<td class=3DFont align=3D"right">
<asp:TextBox id=3Dt1 runat=3D"server" />
<img border=3D"0" src=3D"images/camera.gif"> 
<img border=3D"0" src=3D"images/deposit.gif"> 
<img border=3D"0" src=3D"images/exclaim_red.gif"> 
<img border=3D"0" src=3D"images/exclaim_yel.gif"> 
<img border=3D"0" src=3D"images/comment.gif"> 
</td>
<td align=3D"center"><select size=3D"1" name=3D"cboSelect"
Class=3D"normalcboBox" style=3D"width=3D120"><option
selected>----</option><option>Select</option><option>Thru
Sales</option><option>Included</option><option>Cancelled</option></selec
t></
td>
<td align=3D"center"><input type=3D"text" name=3D"txtQuantity"
Class=3D"normaltextBox" style=3D"width=3D40" size=3D"20"></td>
<td class=3DFont align=3D"right">UNIT_$</td>
<td class=3DFont align=3D"right">TOT_$</td>
</tr>
</table>

USER CONTROL CODE BEHIND (OptionsSelectorRow.vb)
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class OptionsSelectorRow
Inherits UserControl

Public TextValue as String ' Databind to the TextValue property
of
the user control instance
Protected t1 as TextBox ' You need an instance for the control
in
code-behind

Property msg as String
     Get
         Return t1.Text
     End Get

     Set
         t1.Text =3D Value
     End Set
    End Property

Sub Page_Load (s as Object, e as EventArgs)

If Not Page.IsPostBack Then

If TextValue <> "" Then
t1.Text =3D TextValue
End If

End If

End Sub

End Class

| [aspngbeta] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.asplists.com/asplists/aspngbeta.asp =3D JOIN/QUIT

Reply to this message...
 
 
System.EventArgs
System.EventHandler
System.Web.UI.Page
System.Web.UI.UserControl
System.Web.UI.WebControls.TextBox
System.Windows.Forms.TextBox
System.Windows.Forms.UserControl




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com