views:

129

answers:

4

I have a project where I made a couple user controls: a Legend, and a Legend Item.

On my web page, I am retrieving a collection of information that I want to load into LegendItem controls (setting basic text and color info) And then Add those Items to my Legend control (the Legend control has a Panel on it and I have referenced a Legend usercontrol on the web page itself.

I am having trouble figuring out how to properly get a handle on a LegendItem control so I can access its properties and set them.

I can provide code if you need it to assist - but I'm stuck and need some help! Thank you for taking the time to read this!

+1  A: 

The key to understanding dynamic controls is to understand the page lifecycle. You need to make sure your controls are being created during the page_init event to ensure that the control is being properly handled server side.

A great control that is available to help you manage dynamically created controls is the dynamic controls placeholder

lomaxx
I think I need to clarify. I do understand the page lifecycle - this question isn't about that.I want to be able to create a LegendItem in code, just as I can create a TextBox, DropDownList, etc, set it's properties, and add it to a Legend user control. I can do this creating my own class library and creating custom control then referencing that DLL, but I don't want to go through all that for this if possible.Thanks for your response, lomaxx.
TheGeekYouNeed
+1  A: 

You can just instance the user control using LoadControl.

Then set your properties.

Then add you control to an asp.net placeholder control that you have on your Legend control.


LegendItem li = (LegendItem)LoadControl("LegendItem.aspx");
li.name = "bob"
legend.placeholder.controls.add(li);
This isn't working for me - I've tried it.My web app is not recognizing LegendItem as an object. Getting the "type or namespace name 'LegendItem' cound not be found' error message.I have the Reference to the control in my web page. Not sure what else to try.
TheGeekYouNeed
@Cen - Without viewing your project and your code, I really cannot offer much more advise. You say you have a reference to the control in your web page? What do you mean by that? If it's all in one project you shouldn't need a reference.
Perhaps you need to cast the "object" type returned from LoadControl to type "LegendItem"?
Sorry LoadControl return base type "control."
Reference tag, as this article (http://msdn.microsoft.com/en-us/library/c0az2h86.aspx) states to do:<%@ Reference Control="MyUserControl.ascx" %>I've tried casting - the code is not recognizing the LegendItem object at all. (Missing type refence error message)
TheGeekYouNeed
A: 

I am not sure what are you trying to do. If all you need is to add your control (statically) to your page here is how you do it:

<%@ Page Language="C#" %>
<%@ Register TagPrefix="uc" TagName="Spinner" Src="~\Controls\Spinner.ascx" %>
<html>
<body>
<form runat="server">
      <uc:Spinner id="Spinner1" runat="server" MinValue="1" MaxValue="10" />
</form>
</body>

Once you have that, in the code behind you can locate the control by ID, cast it to your type, and then do whatever. BTW, the MinValue and MaxValue are properties defined on your control implementation.

Now, if you want to dynamically create a control instance on the fly, you need to use the LoadControl method of your page. This method returns an instance of your control, but to do anything to it you need to add it to the control collection of the page, like this:

page.Controls.Add(mycontrol).

You can do this anywhere in the page lifecycle before the OnRender event. Just keep in mind that once you do that the control will start playing catch-up - it will be pushed through all the page lifecycle events it missed. Also keep in mind that if the page is posted back, you need to ensure that the control is in place exactly as it was by the time the viewstate is processed.

As a side note I think that doing this sort of work server side is a little outdated. You will be better off doing something with JQuery and the likes

mfeingold
A: 

This is so weird. My aspx.cs page wouldn't recognize my control unless I gave the user control a namespace. Now I am able to create objects, cast properly, and the like.

Thanks everyone for trying.

TheGeekYouNeed