M
Mark
Hi.
I am making a user control right now, and it looks something like this:
<script runat="server">
public string SelectCommand
{
set
{
// see below for why the following line is here.
this.InitializeAsUserControl(HttpContext.Current.Handler);
this.sqlDataSource.SelectCommand = value;
}
get
{
// again, see below.
this.InitializeAsUserControl(HttpContext.Current.Handler);
return this.sqlDataSource.SelectCommand;
}
}
</script>
<asp:SqlDataSource runat="server" />
<%-- etc... repeat about 9 times --%>
So then, on my page I put something like this:
<asdf:MyControl SelectCommand="SELECT whatever FROM something"
runat="server" />
Now, the commented lines are there because, if I didn't have them,
ASP.NET would construct MyControl and immediately try and set
SelectCommand. This would happen before MyControl built its child
controls.
I'm wondering, is there a better way to do this, other than to
construct all the child controls manually in the constructor?
(Actually, I can't even declare the constructor for a user control, so
I'd have to switch to a custom control. And that is a problem for
various reasons I'd rather not get into.)
I could save the property settings in private member variables, but
that's really messy with about 10-15 of them, and I'd have to
arbitrarily pick a point where I "move over" the settings. (Probably
OnPreRender) And on top of that, there are read-only properties (i.e.
collections) that I expose this way, so it would take a lot of work to
get that to work right.
In a normal control, I guess I would call EnsureChildControls in a
place like this, but I don't have that option. InitializeAsUserControl
seems to be the closest thing available, but I don't like it because I
have to access HttpContext.Current.Handler, and so it doesn't seem like
it was designed for this.
Am I just trying to do something the wrong way?
Thanks.
I am making a user control right now, and it looks something like this:
<script runat="server">
public string SelectCommand
{
set
{
// see below for why the following line is here.
this.InitializeAsUserControl(HttpContext.Current.Handler);
this.sqlDataSource.SelectCommand = value;
}
get
{
// again, see below.
this.InitializeAsUserControl(HttpContext.Current.Handler);
return this.sqlDataSource.SelectCommand;
}
}
</script>
<asp:SqlDataSource runat="server" />
<%-- etc... repeat about 9 times --%>
So then, on my page I put something like this:
<asdf:MyControl SelectCommand="SELECT whatever FROM something"
runat="server" />
Now, the commented lines are there because, if I didn't have them,
ASP.NET would construct MyControl and immediately try and set
SelectCommand. This would happen before MyControl built its child
controls.
I'm wondering, is there a better way to do this, other than to
construct all the child controls manually in the constructor?
(Actually, I can't even declare the constructor for a user control, so
I'd have to switch to a custom control. And that is a problem for
various reasons I'd rather not get into.)
I could save the property settings in private member variables, but
that's really messy with about 10-15 of them, and I'd have to
arbitrarily pick a point where I "move over" the settings. (Probably
OnPreRender) And on top of that, there are read-only properties (i.e.
collections) that I expose this way, so it would take a lot of work to
get that to work right.
In a normal control, I guess I would call EnsureChildControls in a
place like this, but I don't have that option. InitializeAsUserControl
seems to be the closest thing available, but I don't like it because I
have to access HttpContext.Current.Handler, and so it doesn't seem like
it was designed for this.
Am I just trying to do something the wrong way?
Thanks.