By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,275 Members | 947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,275 IT Pros & Developers. It's quick & easy.

Strange Custom Control Problem (.Net 2.0)

P: n/a
Hello Developers,

I have a control derived from System.Web.UI.WebControls.WebControl. Control
has this property:

[Bindable(true), Category("Misc"), Description("value")]
public string Value
{
set { _value = value; }
get { return _value; }
}

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute("alt", _value);
}

I use my control like this:
<mc:MyControl ID="MyControl" Value="<%=Value %>" runat="server" />

(the page class has protected Value property)

There are no compilation problems, but ASP.Net incorrectly renders alt
attribute:

alt="&lt;%=Value %>"

It seems like it is a problem at compile-time. I cannot use expressions in
control attribute values.
What could be wrong with this picture?
Any hints highly appreciated.

Tomasz J
Jan 9 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Tomasz,

As for the problem you met, it is due to the limitation of expression that
can be used inside ASP.NET server control attribute/tag. The <%= %>
expression is brought from classic ASP for compatible purpose and can only
be used in plain html fragment(at top level of aspx page or ascx user
control).

For ASP.NET server control tag, you can consider the following means to
embed dynamic value:

1. Use <%# ... %like databinding expression, however, you need to call
control.DataBind() at runtime (in page load or init ...) so as to make the
databinding expression get evaluated.

2. In ASP.NET 2.0, there provides a custom Expression Builder
functionality, that can help you define your own custom expression(can be
used inside server control tag/attributes). Here are some articles
introducing this:

#The CodeExpressionBuilder
http://weblogs.asp.net/infinitiesloo...odeExpressionB
uilder.aspx

http://weblogs.asp.net/leftslipper/archive/2007/01.aspx

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "Tomasz J" <oe****@nospam.nospam>
Subject: Strange Custom Control Problem (.Net 2.0)
Date: Wed, 9 Jan 2008 02:15:06 +0100
>
Hello Developers,

I have a control derived from System.Web.UI.WebControls.WebControl.
Control
>has this property:

[Bindable(true), Category("Misc"), Description("value")]
public string Value
{
set { _value = value; }
get { return _value; }
}

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute("alt", _value);
}

I use my control like this:
<mc:MyControl ID="MyControl" Value="<%=Value %>" runat="server" />

(the page class has protected Value property)

There are no compilation problems, but ASP.Net incorrectly renders alt
attribute:

alt="&lt;%=Value %>"

It seems like it is a problem at compile-time. I cannot use expressions in
control attribute values.
What could be wrong with this picture?
Any hints highly appreciated.

Tomasz J
Jan 9 '08 #2

P: n/a
Thank you Steven!

I was unaware of this limitation. This syntax worked with my ascx controls.
Good to know. Perhaps it is something which could be improved, because
binding is less convenient.

Tomasz J
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:sp**************@TK2MSFTNGHUB02.phx.gbl...
Hi Tomasz,

As for the problem you met, it is due to the limitation of expression that
can be used inside ASP.NET server control attribute/tag. The <%= %>
expression is brought from classic ASP for compatible purpose and can only
be used in plain html fragment(at top level of aspx page or ascx user
control).

For ASP.NET server control tag, you can consider the following means to
embed dynamic value:

1. Use <%# ... %like databinding expression, however, you need to call
control.DataBind() at runtime (in page load or init ...) so as to make the
databinding expression get evaluated.

2. In ASP.NET 2.0, there provides a custom Expression Builder
functionality, that can help you define your own custom expression(can be
used inside server control tag/attributes). Here are some articles
introducing this:

#The CodeExpressionBuilder
http://weblogs.asp.net/infinitiesloo...odeExpressionB
uilder.aspx

http://weblogs.asp.net/leftslipper/archive/2007/01.aspx

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>>From: "Tomasz J" <oe****@nospam.nospam>
Subject: Strange Custom Control Problem (.Net 2.0)
Date: Wed, 9 Jan 2008 02:15:06 +0100
>>
Hello Developers,

I have a control derived from System.Web.UI.WebControls.WebControl.
Control
>>has this property:

[Bindable(true), Category("Misc"), Description("value")]
public string Value
{
set { _value = value; }
get { return _value; }
}

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute("alt", _value);
}

I use my control like this:
<mc:MyControl ID="MyControl" Value="<%=Value %>" runat="server" />

(the page class has protected Value property)

There are no compilation problems, but ASP.Net incorrectly renders alt
attribute:

alt="&lt;%=Value %>"

It seems like it is a problem at compile-time. I cannot use expressions in
control attribute values.
What could be wrong with this picture?
Any hints highly appreciated.

Tomasz J

Jan 9 '08 #3

P: n/a
Thanks for your reply Tomasz,

Yes, I agree that Binding Expression is not quite convenient for value
assignment cases. I think that's why "code expression bulder" is added in
ASP.NET 2.0 which is quite flexible and powerful :)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "Tomasz J" <oe****@nospam.nospam>
References: <#a**************@TK2MSFTNGP02.phx.gbl>
<sp**************@TK2MSFTNGHUB02.phx.gbl>
>Subject: Re: Strange Custom Control Problem (.Net 2.0)
Date: Wed, 9 Jan 2008 16:30:32 +0100

Thank you Steven!

I was unaware of this limitation. This syntax worked with my ascx controls.
Good to know. Perhaps it is something which could be improved, because
binding is less convenient.

Tomasz J
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:sp**************@TK2MSFTNGHUB02.phx.gbl...
>Hi Tomasz,

As for the problem you met, it is due to the limitation of expression
that
>can be used inside ASP.NET server control attribute/tag. The <%= %>
expression is brought from classic ASP for compatible purpose and can
only
>be used in plain html fragment(at top level of aspx page or ascx user
control).

For ASP.NET server control tag, you can consider the following means to
embed dynamic value:

1. Use <%# ... %like databinding expression, however, you need to call
control.DataBind() at runtime (in page load or init ...) so as to make
the
>databinding expression get evaluated.

2. In ASP.NET 2.0, there provides a custom Expression Builder
functionality, that can help you define your own custom expression(can be
used inside server control tag/attributes). Here are some articles
introducing this:

#The CodeExpressionBuilder
http://weblogs.asp.net/infinitiesloo...odeExpressionB
>uilder.aspx

http://weblogs.asp.net/leftslipper/archive/2007/01.aspx

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

================================================= =

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
>ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
>where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each
follow
>up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

================================================= =
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>>>From: "Tomasz J" <oe****@nospam.nospam>
Subject: Strange Custom Control Problem (.Net 2.0)
Date: Wed, 9 Jan 2008 02:15:06 +0100
>>>
Hello Developers,

I have a control derived from System.Web.UI.WebControls.WebControl.
Control
>>>has this property:

[Bindable(true), Category("Misc"), Description("value")]
public string Value
{
set { _value = value; }
get { return _value; }
}

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute("alt", _value);
}

I use my control like this:
<mc:MyControl ID="MyControl" Value="<%=Value %>" runat="server" />

(the page class has protected Value property)

There are no compilation problems, but ASP.Net incorrectly renders alt
attribute:

alt="&lt;%=Value %>"

It seems like it is a problem at compile-time. I cannot use expressions
in
>>>control attribute values.
What could be wrong with this picture?
Any hints highly appreciated.

Tomasz J


Jan 10 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.