Inline script does not resolve in ASP.Net custom control - asp.net

Currently I am working with a custom regular expression validator (unfortunately).
I am trying to set the Regex pattern using a server side inline script like this:
ValidationExpression="<%= RegExStrings.SomePattern %>"
However, the script is not resolving to server side code. Instead it is being interpreted literally and I end up with something like this in the rendered markup:
ctl00_DefaultContent_regexValidatorInvitation.validationexpression = "<%= RegExStrings.SomePattern %>";
Any clues as to why this is not resolving properly?

This has now been cleared up my MS. The issue I discovered was caused by the fact that the "action" attribute in server forms had no effect prior to .NET 2 SP2, but now can be set. Code render blocks have never worked in attribute values - this is explained towards the end of this post.
This was a consequence of a deliberate change in behaviour introduced in Microsoft .NET Framework 3.5 SP1. Prior to the service pack, action and method attributes on server side FORM tags could not be over-ridden. If specified they would be replaced by ASP.NET with "POST" and "page name".
Previously, the ASP.NET page parser did not prevent one specifying these attributes although the documentation advised against it for the action attribute:
http://msdn.microsoft.com/en-us/library/k33801s3.aspx
In particular the comment (in the context of the FORM element):
• "The opening tag must not contain an action attribute. ASP.NET sets these attributes dynamically when the page is processed, overriding any settings that you might make. "
The issue that was originally reported by Josh, where the code block was not being interpreted is not new behaviour but is a known bug - code render blocks cannot be used within server control attributes. This is reported as a "Connect" bug:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=109257
which contains the following:
" Attributes of server controls cannot take an inline expression as value. This explains the unexpected behaviour as seen with: " <link href="<%=RootPath %> ..." However, inline code can be used for values of attributes."

But why is this? I can reproduce your problem using a simple aspx page as below:
<%# Page language="c#" AutoEventWireup="true" %>
<html>
<body >
<form id="Form1" method="post" runat="server" action="?<%=Request.QueryString%>">
Query String value: <%=Request.QueryString %>
<br />
<input type=submit />
</form>
</body>
</html>
This displays the following after submitting the form:
Query String value:
%3c%25=Request.QueryString%25%3e
For some reason, the inline code is not executed when the
runat="server" is present. The strange thing is I have 3 machines
that do not behave this way and one that does, so I can only assume
that this is an IIS/.NET config issue, possibly caused by a recent MS
Update. The software I have installed recently on the machine
exhibiting this behaviour is:
Visual Studio 2008
WSE 3.0
IE8 RC1
I wonder if any of these have caused this?

devstuff, that doesn't explain why this works on 3 of my machines, but not a 4th one, does it? They are all using the same .NET Framework version and IIS settings (I believe, having checked as many as I coukd)

I've uninstalled the .NET framework while investigating this (3.5 then 3.0 and 2.0) - I then had no problems after
Installing each of the following:
.net framework 2.0
.net framework 2.0 SP1
.net framework 3.0
.net framework 3.0 SP1
.net framework 3.5
But after I installed .net framework 3.5 SP1 the behaviour returned – I guess this is the issue. I have raised this with Microsoft and will update this thread when I get a response.

Values in a web control do not render server side code.
Rather set that from the Code Behind
RegExValidator1.ValidationExpression = RegExStrings.SomePattern;

It's being taken as a literal string, try
ValidationExpression='<%= RegExStrings.SomePattern %>'
Edit: The above doesn't work, I've tried to see how to do this without success,I usually set properties in the code-behind and only use this syntax for databinding when I have to. I'd be interested to know if it can be done too.

If your regular expression validator has the runat="server" attribute, then change it from the code behind. It would be much easier.

You're using a databinding expression on a control that is not databound. You need to call DataBind(), or use an ExpressionBuilder implementation. A simple ExpressionBuilder for binding to arbitary code can be found at http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx

Related

asp.net: Handling form data in Visual Studio 2010

Climbing the learning curve for creating asp.net webform pages with Visual Studio 2010 (VB).
I had written a fairly complicated .aspx page with form controls, including textboxes and buttons, etc. I never thought to place the form controls inside a <form> block. Instead, all the controls include the "runat" directive; for example, <asp:textbox id="txtUserName" runat="server"> etc. In the codebehind I access the data with strUserName = txtUserName.text. This seems to work just fine.
Now, though, I received some form pages from our contracted "professional" web developer wherein the form code is all enclosed in a <form runat="server">block, and none of the controls include the runat directive. Accessing the data from these controls is a little different: It uses the <input type="text name="txtUserName" id="txtUserName" /> method, and accessing the data in the codebehind
is strUserName = Request.Form("txtUserName").ToString.
My method seems to work fine, but I am wondering if there is a difference in behavior or reliability between my method and his. Even though my way works, am I doing it wrong?
Mine is based on online research I have done to learn this stuff, and I don't remember seeing anything that looked like his. However, just today I see places that are saying that on .aspx pages, form controls MUST be enclosed in a <form> block (i.e., this page at w3schools.com).
Can anyone clarify this for me?
Thanks for your help!
You're not doing it incorrectly (you're using my preferred approach) but your inputs should still be in an enclosing Form tag.
He's using HtmlControls (System.Web.UI.HtmlControls namespace) and you're using web controls (System.Web.UI.WebControls.) Your controls provide better functionality on the server (viewstate and accessing via server code) and his approach is lighter weight.

Error in ScriptResource.axd: "Object of type 'Sys.Extended.UI.AccordionBehavior' cannot be converted to type 'Sys.UI.Behavior'

I have an asp.net web site where I'm trying to resolve what looks like a problem with ASP.NET AJAX:
Microsoft JScript runtime error:
Sys.ArgumentTypeException: Object of
type
'Sys.Extended.UI.AccordionBehavior'
cannot be converted to type
'Sys.UI.Behavior'. Parameter name:
instance
I've googled around a lot, and looked at the code presented (see below, no idea what it means) but no luck.
I've tried changing scriptmanger scriptmode to release, and a bunch of other things too. Anyone have any ideas?
Details:
Visual Studio 2010
ASP.NET 4.0
Ajax control toolkit 4.0
jQuery 1.4.2
jQuery UI 1.8.5
JS Code the error occurs in, inside ScriptResource.axd:
dispose:function()
{
var c=this;
b.UI.Behavior.callBaseMethod(c,eb); --------- this line
var d=c._element;
if(d)
{
var f=c.get_name();
if(f)
d[f]=a;
var e=d._behaviors;
Array.remove(e,c);
if(!e.length)
d._behaviors=a;
delete c._element
}
}
Declaration of accordion control:
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="atlas" %>
(snip)
<atlas:Accordion ID="menu" runat="server" SelectedIndex="0"
ContentCssClass="accordionContent" FadeTransitions="true" FramesPerSecond="30"
TransitionDuration="250" AutoSize="None" Width="270">
</atlas:Accordion>
*Update: *
Added accordion control declaration as requested, is there anything else I can add that could be useful?
Does anyone have any idea about this error at all - am I right in thinking it's MS AJAX not playing nicely with other javascript (probably jQuery UI) on the page?
Are you using update panels on your page at all? If so, remember that once you add controls inside\outside of an update panel, the "scope" of objects included in a postback changes dramatically, as only objects wrapped within an UpdatePanel are included.
If this doesnt help, I apologize, but my experiences with ASP.NET AJAX have added a layer of complexity to the postback model with sites Ive implemented it on.

ASP.Net WebForms requiredfieldvalidator not working in FireFox?

I have a WebForms app that uses a field validator on a dropdownlist. It works in IE but not FireFox.
This is pretty straightforward stuff I'm doing. Here are the setups for the dropdown and validator:
<asp:DropDownList ID ="dmbFileActNo" runat="server" CssClass="DROPDOWN_MEDIUM" AutoPostBack="True"></asp:DropDownList>
<asp:requiredfieldvalidator EnableClientScript="true" id="rfvFileActNo" Display="None" ControlToValidate="dmbFileActNo" Runat="server" InitialValue="-1"></asp:requiredfieldvalidator>
I'm running ASP.Net 2.0 on the web server. Javascript is enabled on the FireFox browser-- this problem happens on all FF browsers I've tested, on multiple everyday machines, so I don't believe it's due to a locked down install.
Client-side validation may not work in all browsers and in all scenarios. It's important to make sure that you always do validation on the server as well.
You may also want to take a look at what the DetermineRenderUpLevel() method on your validation control is returning in FF. Behind the scenes, it checks to see that the following are true:
The browser has client script
enabled.
The W3CDomVersion property of the
HttpBrowserCapabilitiesBase object
that is stored in the
HttpRequest.Browser property is 1 or
later.
The EcmaScriptVersion property of the
HttpBrowserCapabilitiesBase object
that is stored in the
HttpRequest.Browser property is 1.2
or later.
I got this working by modifying the web.config. As it turns out, someone had <xhtmlConformance mode="Legacy"/> in there, which is an older setting (the current default is "transitional"). Thanks for everyone's help.
Sometimes ASP.net sends different HTML/ javascript to different browsers. Check your browsercap file/settings
I have faced this problem and it was a problem of using the point character "." inside the id property of controls

ASP.NET inline code in a server control

Ok, we had a problem come up today at work. It is a strange one that I never would have even thought to try.
<form id="form1" runat="server" method="post" action="Default.aspx?id=<%= ID %>" >
Ok, it is very ugly and I wouldn't have ever tried it myself. It came up in some code that was written years ago but had been working up until this weekend after a bunch of updates were installed on a client's web server where the code is hosted.
The actual result of this is the following html:
<form name="form1" method="post" action="Default.aspx?id=<%= ID %>" id="form1">
The url ends up like this:
http://localhost:6735/Default.aspx?id=<%= ID %>
Which as you can see, demonstrates that the "<" symbol is being encoded before ASP.NET actually processes the page. It seems strange to me as I thought that even though it is not pretty by any means, it should work. I'm confused.
To make matters worse, the client insists that it is a bug in IE since it appears to work in Firefox. In fact, it is broken in Firefox as well, except for some reason Firefox treats it as a 0.
Any ideas on why this happens and how to fix it easily? Everything I try to render within the server control ends up getting escaped.
Edit
Ok, I found a "fix"
<form id="form1" runat="server" method="post" action='<%# String.Format("Default.aspx?id={0}", 5) %>' >
But that requires me to call DataBind which is adding more of a hack to the original hack. Guess if nobody thinks of anything else I'll have to go with that.
ASP.NET 3.5 added the "Action" property to the HtmlForm control. Your previous code worked just fine because "action" was just a string, and the code nugget would emit the additional data for you. Now that there is an Action property, you can't use a simple code emit nugget, since a server-side control expects a property to have a literal string value (same as any other server-side control property).
Your workaround of using the biding syntax is correct. To make it work like it used to, you would have to remove the runat=server tag on your form, which would then prevent the ASPX parser from treating it as an HtmlForm control, and instead treat it as a literal (where your code emit nugget would be allowed to work).
Your other option, which may be much cleaner - is to simply set the Form's action property through code-behind in the page_load. The reason the action property is set the way it is, was because in earlier versions of .NET Framework, there was no support for setting the Action property.
your form needs a runat=server

Why does Intellisense not work when using render blocks on an HTML attibute with double quotes?

When I try to write code like the following source code, Visual Studio doesn't show any Intellisense for current context. Moreover, it tells me that another "<% %>" brackets is incorrect.
<div>
<input type="checkbox" title="<%= LogOnView.RememberMe_ToolTip %>" />
</div>
How to solve this problem by patch or VSPackage(if you have some source code for modify existing Intellisense, I will add your source code to my VS Package)?
PS. I'm using Visual Studio 2008 SP1 with .NET 3.5 SP1
This is just a shortcoming in the Visual Studio HTML designer component. If you pull the code block outside of the attribute it works great, once you put <%= %> in the attribute it no longer gives you proper IntelliSense in that block. I've even had this within a style="<%= %>" attribute go totally haywire and won't even get the syntax color correct (tries to parse it as CSS).
My theory is that the parser is in the context of that tag and attribute so it is trying to interpret what you are doing as values of that attribute. For instance, when you type the open quote of the type=" it knows to show you a list of common values for that tag. If you try to invoke IntelliSense in the middle of type="<%= %>", it will show you that same list of values which is not what you would expect in this code block context.
I experience this same exact issue when using the Visual Studio syntax coloring for editing JBST client-side templates. The markup is identical but the syntax coloring freaks out every so often.
I'm betting that there isn't a fix for it as it is pretty core to the designer. Best bet is to wait for a future release. I haven't tried this in Visual Studio 2010 to see if they've fixed it.
I just installed Visual Studio 2010 Beta 1 and it doesn't work in this version either. I really hope they fix this before the final release.
I think it needs a hint to process that in ASP.NET.
Try using
<div runat="server">
<input type="checkbox" title="<%= LogOnView.RememberMe_ToolTip %>" runat="server"/>
</div>
Alternatively, you can do it in .NET with (might need a <%#Register%> at the top though):
<asp:Panel runat="server">
<asp:CheckBox runat="server" ToolTip="<%= LogOnView.RememberMe_ToolTip %>"/>
</asp:Panel>

Resources