What does 'parse-time' mean? Does it mean rendering? - asp.net

When I was reading Asp.Net Expressions article, I have seen such a sentence:
"Expressions are evaluated at run time when the declarative elements of the page are parsed, and the value represented by the expression is substituted for the expression syntax. (Because expressions are evaluated at parse time, you cannot dynamically create expressions in code.)"
Where does the parse-time exist in terms of page life-cyle? Rendering?

Yeah, the "code front" or the "aspx" page is interpretted each time a request for it is made. At the time of this request the aspx page is consumed or parsed by the asp.net dll. This would be the point in time they are referencing in this blurb.

Related

Regular Expression Extractor failing in JMeter for asp.net web forms

Using JMeter, I'm attempting to log in to an asp.net web forms application. I recorded the login sequence to a *.jmx file, and now I'm attempting to extract the __VIEWSTATE, __VIEWSTATEGENERATOR and __EVENTVALIDATION hidden inputs using the Regular Expression Extractor post-processor.
In all 3 cases JMeter is extracting the name of the variable that I want to extract into (eg "${viewstate}") instead of the value I want to extract. Here is what the RequestBody looks like when I look at the ViewResults Tree and select "Text":
ReturnUrl=%2F&__VIEWSTATEGENERATOR=%24%7Bviewstategenerator%7D&__EVENTARGUMENT=&__VIEWSTATE=%24%7Bviewstate%7D&ctl00%24ContentPlaceHolder1%24Login1%24LoginButton.x=25&ctl00%24ContentPlaceHolder1%24Login1%24Password=MyPassword%21&ctl00%24ContentPlaceHolder1%24Login1%24LoginButton.y=4&__LASTFOCUS=&ctl00%24ContentPlaceHolder1%24Login1%24UserName=MyUserName&__EVENTTARGET=&__EVENTVALIDATION=%24%7Beventvalidation%7D
Oddly enough, if I select "RegExpTester" in the ViewResults Tree and test my regular expressions, all of them appear to work.
For example, here is what my __VIEWSTATE extractor looks like:
The Regular Expression is this bit of text:
name="__VIEWSTATE" id="__VIEWSTATE" value="(.+?)"
When I enter that expression into the RegExp Tester it finds it. The other 2 also work:
This is my first time using JMeter, I suspect I've got something in the wrong place.
Here is how my HTTP Request is set up:
Here is how the entire project looks:
Where do you expect these values to come from? You're missing one GET request which will open the login page, your test should not start from POST request.
Once you execute GET request - your Regular Expression extractors will capture viewstate and friends and you will be able to log in.
Also consider switching to CSS Selector Extractors as using regular expressions to parse HTML is not the best idea.
The relevant CSS Selector expression would be as simple as input[id=__VIEWSTATE], use value as the attribute. Similarly correlate remaining dynamic values. See ASP.NET Login Testing with JMeter article for more details if needed.

JMeter: "__VIEWSTATE" and "__EVENTVALIDATION" value does not get replaced after extracting values using Regular Expression Extractor Post Processor

I am trying to load test the ASP.net website and after a bit of research, it became apparent that JMeter was running into issues with VIEWSTATE, which is one of the workarounds ASP.NET WebForms uses to make HTTP appear to be stateful. JMeter is sending a stale value for VIEWSTATE since it is replaying the HTTP requests in the test plan. I extracted the VIEWSTATE from each response and re-include that value on requests. I did it with two Regular Expression Extractors but I still don't see values getting replaced after parameterization.
Your regexp is probably wrong.
It's better to use css/jquery extractor instead of regexp in this case
Just put:
- expression : input[id=__VIEWSTATE]
- attribute : value
and for second one:
expression : input[id=__EVENTVALIDATION]
attribute : value
Use the below regex..it worked for me
input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="([A-Za-z0-9+=/-_]+?)"

How do I validate a zip code in visual basic?

I am supposed to use the regularExpressionValidator to verify a ZIP code for a basic webpage I'm making. If the Zip code is valid, the submit button's click event procedure should display the message "Your ZIP code is" followed by the ZIP code and a period.
I don't know how to do an "if" statement to check to see if the zip is valid or not
**Why does the value = 0 when I enter 60611-3456
...don't know how to do an "if" statement...
You were assigned to use a RegularExpressionValidator, and this sounds like homework. If so, it also sounds like the purpose of the assignment is to make this happen without writing any if statements at all.
The validator controls have a feature where a postback event will not occur if validation fails. You use a correct regular expression with a correctly configured validator control, and the code that shows the "Your zip code is..." message will never run. Configuring the validator control is the point of the assignment; you need to do that part on your own. But finding an acceptable regular expression is a distraction from the real learning, and so I don't mind just giving that to you:
^\d{5}(-\d{4})?$
The issue is that your regular expression indicates the four digits must exist if you have the dash. Generally that would be okay but since you're using an input mask the dash always exists, even when it's only five digits. Try the following expression.
ValidationExpression="\d{5}-?(\d{4})?$"
Hope it helps.

Displaying proper date format depending on culture

I am using a control for a popup calendar date picker. This uses a javascript function, SetText, to set the textbox to the given date. I can't change anything in the calendar control itself but I can override the SetText function. The SetText javascript just takes the TextBox name and the date value in string format and sets the TextBox to the string.
The problem:
I need to display the date in the format "April 30".
Easy to do. Use getMonth() and getDate() where I can parse the information from there.
Now, I need to make sure this shows correctly for different cultures. For example, the UK shows dates as "30 April". Since the code-behind(c#) could be sending the date in the UK format how do I know in the javascript that they're using UK(dd/mm/yyyy) and not US(mm/dd/yyyy)?
The browsers navigator language could be set to one setting while the server is set to another to cause a January 4 as April 1 mismatch.
You are using the Microsoft Ajax Framework, this framework defines a set of "client-side type extensions" which provide added functions or "extensions" to the JavaScript base types.
The Date Type Extensions is what you're looking for, specifically the Date.parseLocale function.
With this function you can parse a string, using a given format.
You can synchronize your server-side and client-side culture by setting the ScriptManager.EnableScriptGlobalization property to true, and use the Date.parseLocale function without specifying any format.
Give a look to this article:
Walkthrough: Globalizing a Date by Using Client Script
See toLocaleString and related functions.
If you control the backend, why not just send a timestamp and push it into Date object?
As for formatting on the client side, since I was already using Dojo, I solved this problem by using dojo.date.locale.format. It was completely painless.
Locale is detected automatically or can be set arbitrarily.
Shorthand format options (e.g.: long short)
Data selectors (e.g.: time, date)
Ability to specify an arbitrary date/time pattern (probably not application to this application, but still useful).
Tutorial: http://docs.dojocampus.org/dojo/date/locale
API doc:
http://api.dojotoolkit.org/jsdoc/1.3/dojo.date.locale.format
Date format descriptions: http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns
Three things you could use:
1) toLocaleString - As suggested already. The problem with this is when sending a string of "4/1/2009" this can result in a couple things. January 4 or April 1.
2) navigator.language and navigator.systemLanguage - After you get the date string you can check to see what language the system is in and parse the date from there. The problem with this and solution 1 is what if you have a UK server and the browsers machine is US. You will have the code behind sending April 1 as 1/4/2009 where the javascript will read the string as whatever language the clients browsers is. So, UK server and US browser will give you a wrong result.
3) Use Code Behinds Culture - Create a variable in your javascript that when the page loads, it will call a function in your code behind that returns this.Page.Culture from there, you will know what culture the string is being sent back as. This will eliminate the mismatch that the first two solutions can cause. It will take a little extra work to make sure it's displayed correctly but at least you will be able to use the string without having the possibility of mismatching cultures.
toLocaleDateString would be a better solution than toLocaleString for your problem as it doesn't include the time (as you only are requesting the date).
The open-source JavaScript library Date.js has some great methods for formatting dates, as well as it supports a bunch of languages:
Date.js at Google Code: http://code.google.com/p/datejs/
If you want nicely formatted dates / times, you can just pass a formatting string (nearly identical to those used in .NET Framework) into any Date object's .toString() method.
It also has a whole set of cultures which allow you to simply include the appropriate script for that culture.
If you want to manage that yourself (as we do in our apps), you can find resources which give you the list of appropriate resource strings for a given culture. Here's one that shows proper formatting strings for a ton of cultures: http://www.transactor.com/misc/ranges.html
As you are using ASP.NET then you may also be using ASP.NET Ajax. If so, there are two properties on the ScriptManager that are of use to you:
EnableScriptLocalization - Gets or sets a value that indicates whether the ScriptManager control renders localized versions of script files.
EnableScriptGlobalization - Gets or sets a value that indicates whether the ScriptManager control renders script that supports parsing and formatting of culture-specific information.
<asp:ScriptManager ID="AjaxManager" runat="Server" EnablePartialRendering="true"
EnableScriptGlobalization="true" EnableScriptLocalization="true" />
When you enable both of these (set to true) then ASP.NET Ajax extenders etc. should automatically be localised into the culture specified in web.config:
<configuration>
<system.web>
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-GB"
uiCulture="en-GB" />
</system.web>
</configuration>
For instance, setting this will localise the AjaxControlToolkit Calendar into your specificed culture.
Even if you are NOT using ASP.NET Ajax adding a ScriptManager and enabling localisation will give you a useful javascript variable called __cultureInfo that contains a JSON array of localised formate, such as currencies, dates etc.
"CalendarType":1,"Eras":[1],"TwoDigitYearMax":2029,"IsReadOnly":true},"DateSeparator":"/","FirstDayOfWeek":1,"CalendarWeekRule":0,"FullDateTimePattern":"dd MMMM yyyy HH:mm:ss","LongDatePattern":"dd MMMM yyyy","LongTimePattern":"HH:mm:ss","MonthDayPattern":"dd MMMM","PMDesignator":"PM","RFC1123Pattern":"ddd, dd MMM yyyy HH\u0027:\u0027mm\u0027:\u0027ss etc....
I solved this problem by using Datejs as
In codebehind(aspx.cs) I get the culture for an employee and add the appropriate js to the header as
string path =
"http://datejs.googlecode.com/svn/trunk/build/date-"
+ GetCulture() + ".js"; Helper.AddJavaScript(this, path);
(in your case you can get the culture from navigator.systemLanguage (or navigator.browserLanguge etc) and add a script tag to the header with src attribute pointing to the appropriate path)
On the client-side I use
d.toString(Date.CultureInfo.formatPatterns.shortDate)
where d is any date object
(I tried using Date.today().toShortDateString() but it was throwing exception. (the CultureInfo JSON object had a different structure than what the function expects).

jQuery autocomplete in ASP.NET webforms?

Has anyone used jQuery to populate an autocomplete list on a textbox using ASP.NET webforms? If so, can anyone recommend a good method? From my reading so far, it seems like most people are using delimited lists rather than JSON to bring the items back. I'm open to any ideas that will get me up and running rather quickly.
I made a tutorial to do this with asp.net mvc but it should be almost identical for traditional webforms:
http://blogs.msdn.com/joecar/archive/2009/01/08/autocomplete-with-asp-net-mvc-and-jquery.aspx
There are many, many examples on the web. I've used this one before, and if I recall you only need to create an aspx that will return matching terms as a <BR/> separated list:
http://www.dyve.net/jquery/?autocomplete
The documentation shows php in the example, but there's no difference in the way the plugin itself works and I didn't have to do anything special as a result.
From the documentation:
> $("#input_box").autocomplete("my_autocomplete_backend.php");
In the above example, Autocomplete
expects an input element with the id
"input_box" to exist. When a user
starts typing in the input box, the
autocompleter will request
my_autocomplete_backend.php with a GET
parameter named q that contains the
current value of the input box. Let's
assume that the user has typed
"foo"(without quotes). Autocomplete
will then request
my_autocomplete_backend.php?q=foo.
The backend should output possible
values for the autocompleter, each on
a single line. Output cannot contain
the pipe symbol "|", since that is
considered a separator (more on that
later).
An appropiate simple output would be:
foo
fool
foot
footloose
foo fighters
food fight
I wrote an Asp.Net WebControl and some Asp.Net MVC extension methods wrapping the JQuery UI autocomplete widget.
I wrote documentation as well about how to implement a working resource providing a JSon result.
You can find it at:
http://autocompletedotnet.codeplex.com/
Hope it can help

Resources