Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add control programmatically

Status
Not open for further replies.

gorgor

Programmer
Aug 15, 2002
164
US
I'm using ASP.NET (C#) in Visual Studio. I'm trying to make it as much like windows programming as possible, so I'm trying to add my custom control (ImagePreview Ctrl) to my aspx page programmatically. I created a custom control and it's compiled as an assembly. When I drop the control onto my aspx page in the designer, it adds the line:

<cc1:ImagePreviewCtrl id="ImagePreviewCtrl1" runat="server"></cc1:ImagePreviewCtrl>

to the HTML source. That's as much modification as I want to do to the HTML source. I want to do the rest of the initialization and customization in the aspx.cs file. However, when I drop the control onto my page, the control is not automatically instantiated in the aspx.cs file. There is no reference to ImagePreviewCtrl1 in the .cs file at all. How do I change its properties if there is no object? I tried adding the line:

protected ImagePreview.ImagePreviewCtrl ImagePreviewCtrl1;

to the .cs file and assigning values to its properties, but I get a web application error.

What am I doing wrong? It has to be something simple.

Thanks in advance.
 
Well the only thing I can think of is that maybe you need to add a using statement at the top of the .cs .

For example in my projects I keep the data access classes in a separate assembly. If I try to reference any of the methods in there without first putting a using myProj.DataObjs at the top I get an error as well.

Hope this helps,
- VB Rookie
 
Take a look at LoadControl(). This allows you to instantitate a control and add properties to it programatically.

An example of using it in VB (sorry, I don't know C#):

Dim objControlThing As ControlThing = LoadControl("ControlThing.ascx")

objControlThing.myProperty = "FOO"
Me.myControlThingPlaceHolder.Controls.Add(objControlThing)

This example makes use of a Placeholder, but there are many other ways of adding the control to the page. (Such as: Me.myControlThing = objControlThing)

Hope this helps.

Steve.
 
If you are going to drop it on your page you must use this page directive.

<%@ Register Namespace='ControlsNameSpace' TagPrefix='cc1' Assembly='ControlsAssembly' %>

You can also set properties of your control in the page,
or you can do it in the code behind like you want. You will have to declare it in your code behind.

protected ControlsNameSpace.ControlName ImagePreviewCtrl;

Marty
 
cappmgr,

Whenever I have done as you suggested in the .cs file (which is main reason I posted this question):

Code:
protected ImagePreview.ImagePreviewCtrl ImagePreviewCtrl1;

I get an web application error if I try to assign values to its properties. I don't have my computer in front of me right now, but it's some sort of 'object reference' error. I take it to mean that it's not instantiating the object (or at least the 'same' object that was inserted in the aspx file). Since I'm trying to change the control's properties programattically (from the .cs file), it appears that I'm assigning property values to an object that does not exist.

By the way, the page directive IS in the aspx file for my control's namespace. It was inserted automagically by Visual Studio.
 
The problem here is that you have no code declaration of the object in your .cs file. I think we've pretty well established that here, and VBRookie adeptly pointed out that it's probably a simple problem, such as a missing using directive or something else (add a fully qualified declaration to avoid this problem).

So the question is, "Why is it not putting the code declaration for me when it is putting the aspx page declarations for me?"

Adding design time support for dragging and dropping controls is a very long and tedious process. There's alot to markup in your original file to make it work. If you search it out ( you'll find lots of articles out there.

But you need to ask yourself if it's really that important that you spend the time to do it. Personally, for my little toolbox of custom server controls, I don't add it.

I wrote the controls. I know what their namespaces, properties, methods, etc... are. And besides, once you get that declaration (protected myControlType myControl;) fixed up in the code behind, you get full IntelliSense right out of the box.

Now if you're selling or otherwise redistributing the control, then you have yourself a different animal. Design time support is very important in that case.

Just some thoughts.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Why is it not putting the code declaration for me when it is putting the aspx page declarations for me?"
Good question you will have to find an answer to.

Did you right click on the project and add a reference to your customcontrol.dll?

Marty
 
cappmgr, yes I did. It seems strange that it isn't instantiating the object in the .cs file even though it's putting everything in the aspx page.

I'll have to play with it more once I get home. If it doesn't work, I'll post some code here.
 
I've been messing with the control and here is the error I get when I run the page...

"[StackOverflowException: Exception of type System.StackOverflowException was thrown.]"

No other information is provided?!

My aspx page looks like this:
Code:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<%@ Register TagPrefix="cc1" Namespace="ImagePreview" Assembly="ImagePreview" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>WebForm1</title>
		<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
		<meta content="C#" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta content="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"[/URL] name="vs_targetSchema">
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<cc1:imagepreviewctrl id="ImagePreviewCtrl1" runat="server"></cc1:imagepreviewctrl></form>
	</body>
</HTML>

My aspx.cs page looks like this: (I had to instantiate the object "ImagePreviewCtrl1" MANUALLY because it was not added for me for some reason. Also, if I remove the line where I assign a value to ImagePreviewCtrl1's property, the application doesn't crash. However, the control doesn't show up on the page either. (For some reason, IE won't let me 'view source'. The source will never be displayed.)

Code:
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;
using ImagePreview;

namespace WebApplication1
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected ImagePreview.ImagePreviewCtrl ImagePreviewCtrl1; // <--added this manually

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!IsPostBack)
			{
				ImagePreviewCtrl1.LButtonImageUrl = "/buttons/leftArrow1.gif";
			}
		}

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

Does my aspx page and code-behind look ok? I'm pretty sure the composite control is ok, but I can't be sure since I can't get it to work on my test page. If you'd like me to post the code for that, let me know.

Thanks again!
 
I do not know. Does anyone know if there could be a name clash. Assembly being the same as the namespace.
Not sure how you can eliminate the custom control as the problem when LButtonImageUrl does not accept your argument.

What I would do is make my pool smaller. If it does not work in a little aspx page it is not going to work with code behind. Please note I changed imagepreviewctrl to the actual control name.
Code:
<%@ Page language="c#" %>
<%@ Register TagPrefix="cc1" Namespace="ImagePreview" 
Assembly="ImagePreview" %>
<HTML>
    <HEAD>
    </HEAD>
	<FORM runat="server">
    <BODY>
         <H1>Test Control</H1>
         <cc1:ImagePreview runat="server" 
         LButtonImageUrl = "~/buttons/leftArrow1.gif">
         </cc1:ImagePreview>
    </BODY>
	</FORM>
</HTML>
If this errors try removing
Code:
LButtonImageUrl LButtonImageUrl = "~/buttons/leftArrow1.gif
Hopefully someone has a better idea.
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top