Forcing constructor from aspx

T

tadrichard

Is there any way in ASP.Net (1.1) to force a parameterized constructor
of an object from the server tag in the ASPX code? Alternatively, if
there any way to force the IDE to render tag properties in a specific
order?

For instance, let's say I have the following code for a custom server
control:

namespace Myspace
{
public class MyObject : WebControl
{
public MyObject(SubObjectType Type): base(HtmlTextWriterTag.Div)
{
_Type = Type;
}

public MyObject(): base(HtmlTextWriterTag.Div)
{
_Type = SubObjectType.Foo;
}

private SubObjectType _Type;

public string BuriedText
{
set
{
EnsureChildControls();
Control Sub = (Control) Controls[0];
Sub.Text = value;
}
}

override protected void CreateChildControls()
{

switch(_Type)
{
case SubObjectType.Foo:
{
FooControl Ctrl = new Myspace.FooControl();
Controls.Add(Ctrl)
}
case SubObjectType.Bar:
{
BarControl Ctrl = new Myspace.BarControl();
Controls.Add(Ctrl)
}
default:
break;
}
}
}
}

I want to pass "Bar" as the SubObjectType using a server tag in the
ASPX code. If I put the "type=" parameter BEFORE the "buriedtext="
parameter as in:

<myspace:myobject id="myobject1" type="bar" buriedtext="hello" />

the Type property is set before the BuriedText property. So when
CreateChildControls is executed, the correct subcontrol is created.
However, if I swap the order, as in:

<myspace:myobject id="myobject1" buriedtext="hello" type="bar" />

CreateChildControls is executed before the Type is set and I'm hosed.

I can work around this by editing all the server tags, but Visual
Studio likes to rearrange them again on me any time I edit a property.
What I'd really like is to do something like:

<myspace:myobject(bar) id="myobject1" buriedtext="hello" />

Any ideas?
Thanks.

--Tad
 
G

Guest

Could an administrator (or MVP if you are able) please remove this thread? I
mistakenly submitted it with my email address and I'd like to minimize the
spam I receive as a result.

Thanks
--Tad
 
J

Jon Skeet [C# MVP]

TadRichard said:
Could an administrator (or MVP if you are able) please remove this thread? I
mistakenly submitted it with my email address and I'd like to minimize the
spam I receive as a result.

I'm afraid even if it could be done at this stage - and MVPs don't have
access - the way things work these days, your post will be on various
mirroring websites etc by now.

(Personally I find it better just to use my real email address
everywhere and just deal with spam. YMMV.)
 

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

Top