473,856 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

User-control with property accessors == null references

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.Initialize AsUserControl(H ttpContext.Curr ent.Handler);
this.sqlDataSou rce.SelectComma nd = value;
}
get
{
// again, see below.
this.Initialize AsUserControl(H ttpContext.Curr ent.Handler);
return this.sqlDataSou rce.SelectComma nd;
}
}
</script>

<asp:SqlDataSou rce runat="server" />
<%-- etc... repeat about 9 times --%>
So then, on my page I put something like this:

<asdf:MyContr ol 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 EnsureChildCont rols in a
place like this, but I don't have that option. InitializeAsUse rControl
seems to be the closest thing available, but I don't like it because I
have to access HttpContext.Cur rent.Handler, and so it doesn't seem like
it was designed for this.

Am I just trying to do something the wrong way?

Thanks.

Aug 1 '06 #1
2 2072
You are not creating a user control, you are creating a data access layer,
business layer and UI element wrapped into on ASCX page. IT is a clever
idea, but you end up with a couple of problems.

1. Your database is exposed, at least partially, in your pages.
2. An error in coding is not discovered until the page is run

Technically, UI controls should paint the page. They should not care about
SQL statements, as that is a business concern.

In your case, you could tweak the page and get generic data to bind. But,
the design is not a wise direction and I would consider looking at the more
common patterns presented in Microsoft documentation. It will be more
maintainable in the long run.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** *************** ****
Think outside of the box!
*************** *************** *************** ****
"Mark" <st******@yahoo .comwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
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.Initialize AsUserControl(H ttpContext.Curr ent.Handler);
this.sqlDataSou rce.SelectComma nd = value;
}
get
{
// again, see below.
this.Initialize AsUserControl(H ttpContext.Curr ent.Handler);
return this.sqlDataSou rce.SelectComma nd;
}
}
</script>

<asp:SqlDataSou rce runat="server" />
<%-- etc... repeat about 9 times --%>
So then, on my page I put something like this:

<asdf:MyContr ol 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 EnsureChildCont rols in a
place like this, but I don't have that option. InitializeAsUse rControl
seems to be the closest thing available, but I don't like it because I
have to access HttpContext.Cur rent.Handler, and so it doesn't seem like
it was designed for this.

Am I just trying to do something the wrong way?

Thanks.

Aug 1 '06 #2
Thanks for the response. Definitely a little beyond what I expected.
I'm curious, could you give an example of how to improve? I figured
that using an SqlDataSource along with a DataGrid went along pretty
well with MSDN examples. Are you suggesting a more layered approach,
perhaps using an ObjectDataSourc e instead?

However, I think I have another example that you would not object to:

<%@ Control Language="C#" ClassName="Date Input" %>

<script runat="server">
public string Label
{
get
{
return this.label.Text ;
}
set
{
this.label.Text = value;
}
}

public string Value
{
get
{
if (this.year.Text == ""
&& this.month.Text == ""
&& this.day.Text == "")
{
return "";
}
return new DateTime(int.Pa rse(this.year.T ext),
int.Parse(this. month.Text),
int.Parse(this. day.Text)).ToSt ring();
}
set
{
if (value == "")
{
this.month.Text = "";
this.day.Text = "";
this.year.Text = "";
}
else
{
DateTime dateTime = DateTime.Parse( value);
this.month.Text = dateTime.Month. ToString();
this.day.Text = dateTime.Day.To String();
this.year.Text = dateTime.Year.T oString();
}
}
}
</script>

<div class="field">
<asp:Label ID="label" AssociatedContr olID="month" runat="server" />
<asp:TextBox ID="month" CssClass="mm" MaxLength="2" runat="server" />
/
<asp:TextBox ID="day" CssClass="dd" MaxLength="2" runat="server" />
/
<asp:TextBox ID="year" CssClass="yyyy" MaxLength="4" runat="server" />
</div>

Now the weird part is, this does not require that I call
InitializeAsUse rControl() ever! I can do this in any page:

<ct:DateInput Value="2006-01-01" runat="server" />

And it does not give me trouble. I might have been premature in
identifying this as the solution to my other problem.

Any ideas?

Thanks again,

Mark

Cowboy (Gregory A. Beamer) wrote:
You are not creating a user control, you are creating a data access layer,
business layer and UI element wrapped into on ASCX page. IT is a clever
idea, but you end up with a couple of problems.

1. Your database is exposed, at least partially, in your pages.
2. An error in coding is not discovered until the page is run

Technically, UI controls should paint the page. They should not care about
SQL statements, as that is a business concern.

In your case, you could tweak the page and get generic data to bind. But,
the design is not a wise direction and I would consider looking at the more
common patterns presented in Microsoft documentation. It will be more
maintainable in the long run.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** *************** ****
Think outside of the box!
*************** *************** *************** ****
"Mark" <st******@yahoo .comwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
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.Initialize AsUserControl(H ttpContext.Curr ent.Handler);
this.sqlDataSou rce.SelectComma nd = value;
}
get
{
// again, see below.
this.Initialize AsUserControl(H ttpContext.Curr ent.Handler);
return this.sqlDataSou rce.SelectComma nd;
}
}
</script>

<asp:SqlDataSou rce runat="server" />
<%-- etc... repeat about 9 times --%>
So then, on my page I put something like this:

<asdf:MyContr ol 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 EnsureChildCont rols in a
place like this, but I don't have that option. InitializeAsUse rControl
seems to be the closest thing available, but I don't like it because I
have to access HttpContext.Cur rent.Handler, and so it doesn't seem like
it was designed for this.

Am I just trying to do something the wrong way?

Thanks.
Aug 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

60
7333
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm The detector requires javascript 1.0 to work. This translates to netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
3
4152
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability. The User Experience, or how the user experiences the end product, is the key to acceptance. And that is where User Interface Design enters the design process. While product engineers focus on the technology, usability specialists focus on the user...
3
1956
by: Jiho Han | last post by:
Should ASPNET user belong to the local Users group? I may have made some changes that affected my workstation setup and I am experiencing some unexpected behaviors. For example, I have my IIS set up with anonymous login and have ASP.NET running. My ASP.NET application then creates a log file and writes to it during its course. The only thing is that it should not be able to. My questions are below. Please correct any incorrect...
17
2119
by: Alphonse Giambrone | last post by:
I am building a web app for users to add/edit data. They may add/edit several records during a session. When they are done (not necessarily immediately, could be 10 or more minutes later), I need to send an email with some summary info of what was added/edited. I can keep track of the records by using the sessionid or user's login, but how can I determine when to send the email and who the user was since there is no session info available...
7
2923
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of the class and store the DB values in there temporarily. My problem is that if user1 looks at...
6
2444
by: Andrew Chalk | last post by:
My application attempts to connect to an SQL Server database as name ASPNET and Login Name SERVERNAME/ASPNET in response to these commands: SqlConnection myConnection = new SqlConnection("Data Source=(local);Initial Catalog=MCSCRE;Integrated Security=SSPI"); myConnection.Open(); However, the user of this database is ASPNET. I can't create a user ASPNET with a login name SERVERNAME/ASPNET, SQL Enterprise Manager always keeps the name...
1
1976
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users are created but the account is disabled (see the flag User.AccountDisabled = False ). There is also another problem even if the user does not exist , the application returns to me with the message that the user already exist. Thank you for...
4
2510
by: Scott M. | last post by:
When profile data is stored in ASP .NET, where is the user data persisted? For how long is it persisted: is it session persisted or permanent (like a cookie)?
31
2810
by: zdenko | last post by:
I have a multi user database and users were created by user level security wizzard - as I mentioned in message before. Everything works fine for those users, but now I have another problem. I have another database with linked tables from secured database. How can I approach secured database from the unsecure one? Thx
14
3279
by: chromis | last post by:
Hi, I've been trying to implement a more OOP oriented approach to dealing with user security on one of my websites, and I am trying to validate the user against an array of roles, however I am struggling with a type error: The argument ROLES passed to function setRoles() is not of type array. If the component name is specified as a type of this argument, the reason for this error might be that a definition file for such component...
0
11051
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10693
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10773
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7929
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5756
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3196
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.