开发者

Programmatically using UserControls

开发者 https://www.devze.com 2022-12-10 13:12 出处:网络
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 ba

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!


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


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);


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消