Wednesday, March 28, 2012

Strange Custom Control Problem (.Net 2.0)

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="<%=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 JHi 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/infinitieslo...CodeExpressionB
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/subscript...ault.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/subscript...t/default.aspx.
========================================
==========
This posting is provided "AS IS" with no warranties, and confers no rights.
--
>From: "Tomasz J" <oegweb@.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="<%=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
>
>
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]" <stcheng@.online.microsoft.com> wrote in message
news:spXdCfpUIHA.4720@.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/infinitieslo...ve/2007/01.aspx
> Hope this helps.
> Sincerely,
> Steven Cheng
> Microsoft MSDN Online Support Lead
>
> ========================================
==========
> Get notification to my posts through email? Please refer to
> l]
> 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
> [url]http://msdn.microsoft.com/subscriptions/support/default.aspx." target="_blank">http://msdn.microsoft.com/subscript...t/default.aspx.
> ========================================
==========
>
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
> --
>
> Control
>
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" <oegweb@.nospam.nospam>
>References: <#an5P0lUIHA.4768@.TK2MSFTNGP02.phx.gbl>
<spXdCfpUIHA.4720@.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]" <stcheng@.online.microsoft.com> wrote in message
>news:spXdCfpUIHA.4720@.TK2MSFTNGHUB02.phx.gbl...
that
only
the
http://weblogs.asp.net/infinitieslo...CodeExpressionB
http://msdn.microsoft.com/subscript...ault.aspx#notif
issues
follow
in
>
>

0 comments:

Post a Comment