Strange Custom Control Problem (.Net 2.0)

T

Tomasz J

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
 
S

Steven Cheng[MSFT]

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/infinitiesloop/archive/2006/08/09/The-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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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


This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: "Tomasz J" <[email protected]>
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
 
T

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 said:
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/infinitiesloop/archive/2006/08/09/The-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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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


This posting is provided "AS IS" with no warranties, and confers no
rights.


--------------------
From: "Tomasz J" <[email protected]>
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
 
S

Steven Cheng[MSFT]

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" <[email protected]>
References: <#[email protected]>
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 said:
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/infinitiesloop/archive/2006/08/09/The-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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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


This posting is provided "AS IS" with no warranties, and confers no
rights.


--------------------
From: "Tomasz J" <[email protected]>
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top