views:

1308

answers:

8

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?

A: 

Values in a web control do not render server side code. Rather set that from the Code Behind

RegExValidator1.ValidationExpression = RegExStrings.SomePattern;
WebDude
This doesn't make sense as this style of inline script is used all the time for databinding within web controls.
Josh
If you could provide some documentation confirming this then I would be a little less skeptical. I know I can set this in the codebehind, but I shouldn't have to.
Josh
A: 

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.

PhilGriffin
This isn't the problem. Regardless of whether I put a single quote or a double quote doesn't matter. ASP.Net uses everything inside of the <% %> operators as server side code.
Josh
A: 

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

Sir Psycho
+1  A: 

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?

MarkD
Thanks for your thorough investigation into this matter :) Honestly I had given up on this one, but you seem to be pretty determined. Also, hopefully the rep boost will help you out a little too ;)
Josh
A: 

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

devstuff
+1  A: 

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)

MarkD
+1  A: 

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.

MarkD
+2  A: 

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."

MarkD
Awesome investigation on this one Mark! I really appreciate the work you did on this issue.
Josh