473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Setting UserControls properties in .ascx file

DC
Hi,

I want to keep all my layout information in the .ascx file of a
UserControl. If I require layout related data in codebehind, I would
usually use properties defined in the codebehind, e.g.

string tdStyle;

public string TdStyle
{
get { return tdStyle; }
set { tdStyle = value; }
}

and then set the property in the .ascx, like so:

<script type="text/C#" runat="server">
protected override void OnInit(EventArgs e)
{
TdStyle = "background-color:red;";
}
</script>

This also has the advantage that I could set the property TdStyle in
the .aspx file hosting the .ascx, too.

However, I dislike overriding OnInit in the .ascx (ideally I want to
keep as much code out from that file as possible) just to set some
properties. An alternative approach is using literals like

<asp:Literal ID="TdStyle" runat="server"
Visible="false">background-color:red;</asp:Literal>

but here I need to define a control for every property and the property
data is always a string. Too much overhead.

I was wandering, if someone knows a more elegant approach.

Regards
DC

Nov 12 '06 #1
2 3337
Why do you need to set it in the OnInit? Your tdStyle string is
uninitialized and shoule be initialized anyways to avoid issues. Why not set
it there

string tdStyle = "background-color:red";

One of the first things they teach in programming, always initialized your
variables so you can guarantee that they are actually a value. What value
does an integer x have when it's definited as int x; It's up to the
runtime and/or system to determine it. In this case it should always be int
x = 0; or some other number to start to guarantee that the variable is
initialized.
--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"DC" <dc@upsize.dewrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi,

I want to keep all my layout information in the .ascx file of a
UserControl. If I require layout related data in codebehind, I would
usually use properties defined in the codebehind, e.g.

string tdStyle;

public string TdStyle
{
get { return tdStyle; }
set { tdStyle = value; }
}

and then set the property in the .ascx, like so:

<script type="text/C#" runat="server">
protected override void OnInit(EventArgs e)
{
TdStyle = "background-color:red;";
}
</script>

This also has the advantage that I could set the property TdStyle in
the .aspx file hosting the .ascx, too.

However, I dislike overriding OnInit in the .ascx (ideally I want to
keep as much code out from that file as possible) just to set some
properties. An alternative approach is using literals like

<asp:Literal ID="TdStyle" runat="server"
Visible="false">background-color:red;</asp:Literal>

but here I need to define a control for every property and the property
data is always a string. Too much overhead.

I was wandering, if someone knows a more elegant approach.

Regards
DC

Nov 12 '06 #2
DC
Hi Mark,

I can do this:

string tdStyle = "background-color:red";

only in codebehind, and you are right in that it actually makes sense
to provide a default value for the property.

However, I want to make sure that all my layout information can be
configured in the .ascx, so I need a way to set the property in the
..ascx file. And the only way I found so far is overriding OnInit - and
I don't really want to do that in the .ascx file.

Regards
DC

Mark Fitzpatrick schrieb:
Why do you need to set it in the OnInit? Your tdStyle string is
uninitialized and shoule be initialized anyways to avoid issues. Why not set
it there

string tdStyle = "background-color:red";

One of the first things they teach in programming, always initialized your
variables so you can guarantee that they are actually a value. What value
does an integer x have when it's definited as int x; It's up to the
runtime and/or system to determine it. In this case it should always be int
x = 0; or some other number to start to guarantee that the variable is
initialized.
--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"DC" <dc@upsize.dewrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi,

I want to keep all my layout information in the .ascx file of a
UserControl. If I require layout related data in codebehind, I would
usually use properties defined in the codebehind, e.g.

string tdStyle;

public string TdStyle
{
get { return tdStyle; }
set { tdStyle = value; }
}

and then set the property in the .ascx, like so:

<script type="text/C#" runat="server">
protected override void OnInit(EventArgs e)
{
TdStyle = "background-color:red;";
}
</script>

This also has the advantage that I could set the property TdStyle in
the .aspx file hosting the .ascx, too.

However, I dislike overriding OnInit in the .ascx (ideally I want to
keep as much code out from that file as possible) just to set some
properties. An alternative approach is using literals like

<asp:Literal ID="TdStyle" runat="server"
Visible="false">background-color:red;</asp:Literal>

but here I need to define a control for every property and the property
data is always a string. Too much overhead.

I was wandering, if someone knows a more elegant approach.

Regards
DC
Nov 13 '06 #3

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

Similar topics

1
by: wh | last post by:
After spent a good half day looking for a solution I'm no further finding an answer so I'd appreciate any ideas as to how this can be done. I have developed a user control that resides in a .ascx...
1
by: Lan H. Nguyen | last post by:
I used the technique from this article http://msdn.microsoft.com/asp.net/using/building/webcontrols/default.aspx?pull=/library/en-us/dnaspp/html/usercontrols.asp, that set the properties to...
2
by: N. Demos | last post by:
I have a user control with code behind of which two instances are created/declared in my aspx page. The aspx page has code behind also, as I need to access methods of the usercontrols on page...
7
by: Nathan Sokalski | last post by:
I have a page which I dynamically add several usercontrols (*.ascx files) to using the following code: Public Sub Refresh() For Each section As DataRow In Me.GetSections().Rows...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.