Using User Control from class library

M

Marcel Gelijk

Hi,

I am trying to create a User Control that is located in a seperate class
library. The User Control contains a textbox and a button.
The page generates an exception when it tries to access the code variable
that are supposed to be linked to the contained controls.

It runs fines when everything is contained in a single web form project.

What do I need to do to make it work from a class library?

Thanks,

Marcel

I used Visual Studio 2003 to create this ascx file:

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="WebUserControl1.ascx.cs"
Inherits="WebControlLibrary1.WebUserControl1"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>

The code behind file for the user control looks like this:

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

namespace WebControlLibrary1
{
/// <summary>
/// Summary description for WebUserControl1.
/// </summary>
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#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.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{

}
}
}

The ASPX form looks like this:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="WebApplication1.WebForm1" %>
<%@ Register TagPrefix="cc1" Namespace="WebControlLibrary1"
Assembly="WebControlLibrary1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
&nbsp;
<cc1:WebUserControl1 id="WebUserControl11"
runat="server"></cc1:WebUserControl1>
</form>
</body>
</HTML>

and finally the code behind file for the web form:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected WebControlLibrary1.WebUserControl1 WebUserControl11;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#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
}
}
 
M

Marcel Gelijk

Hermit,

Ok, that would explain why my control fails.

Perhaps I should explain what I am trying to do here. I want to create some
common controls for toolbars, menu's and login that can be used across
serveral web apps. I also like to use Visual Studio for building the
control. I guess I want to throw some control on a custom control surface
and write some code, without having handle all the HTML rendering myself.

Any thoughts on that?

Thanks,

Marcel
 
H

Hermit Dave

nope cant do. atleast not the easy way.
if you want to use the spirit of vs.net you will have to settle for custom
web control. though you can use child objects within the control, instead of
having to write all the html

you do have way in which you can bypass and use user controls (you wanted to
drag and drop and just write some quick code) across applications. but again
thats not the most elegant solution. and you will have no visual studio.net
support.

here's what you do.. create a web application and put all your user controls
in that. for every application that needs to use it, you will have to
manually create a virtual directory that points to that app. Since vs.net
has no concept of server's virtual directory structure.. you dont have be
able to drag and drop from vs.net but you would still be able to use them.
 
M

Marcel Gelijk

Hermit,

I was affraid I had do it the hard way. I think I will use the class derived
from WebControl and override the function CreateChildControls. This seams to
be the best way to build a composite control.

Marcel
 
H

Hermit Dave

good luck with that... if you need any help you can always come around here
.... a better place would be
microsoft.public.dotnet.framework.aspnet.buildingcontrols
or
microsoft.public.dotnet.framework.aspnet.webcontrols
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top