Tuesday, April 29, 2008

asp.net cs0103 cs0101

I had some strange erros when adding a user control

First of all it told me:

Element 'searchresults' is not a known element. This can occur if there is a compilation error in the Web site

The I tried to compile the user control and it said

The name 'lnavigation' does not exist in the current context

So I figured the variable wiring up wasnt working and added a definition for lnavigation manually. The I got.

The type 'referencesearchresultsusercontrol' already contains a definition for 'lnavigation'

Very frustrating.

Later I also got

The file 'src' is not a valid here because it doesn't expose a type in the register tag.

Turns out that the problem was that the register tag was the problem and I had to remove the ".cs" from the src attribute.

Change
<%@ Register TagPrefix="blahblah" TagName="searchresults" Src="searchresults.ascx.cs" %>

To
<%@ Register TagPrefix="blahblah" TagName="searchresults" Src="searchresults.ascx" >

Monday, April 28, 2008

.Net Regular Expressions and accented / unicode characters

I was trying to replace any non letter characters using regular expressions which turned out to be a bit of a pain when unicode / accented characters were used.

I ended up trying to match the stuff that I wanted and remove everything else. Regular expressions aren't really set up like this as there isn't really a "not" operator.

This page was very useful:

http://www.regular-expressions.info/unicode.html

This expression did the trick for me, it matches everything, but only replaces (with the match) matches that I wanted.

Regex.Replace(authors, @"(?(?\p{L}\p{M}*|[ ,;|-])|(?.))", "${allowed}", RegexOptions.Compiled | RegexOptions.Multiline);


\p{L} matches any letter character without a separate accent
\p{M} matches any accent
\P{L}\p{M}* matches any letter character with any number of accents (it is possible to have more than one)
[ ,;|-] matches any special characters that I wanted to keep
the all group matches everything
the allowed group matches characters that I want to keep
the not allowed group matches anything else
The first expression in an or (|) group is the one that is matched so is the allowed group matches then the not allowed doesn't.

"${allowed}" in the replace string replaces a match with the contents of the allowed group. Since everything is matched nothing remains of the original string. If an not allowed match is replaced there is nothing in the allowed group.

Some notes:
Accented characters in unicode can be represented by a single character (for legacy reasons) or as a combination of a base character and one or more accent characters.
Thus a single character on screen such as é can be represented by either one or two unicode characters.
It is thus not possible to match accented characters in the usual way using square brackets [].

Tuesday, February 12, 2008

PageSetupDlg Misreporting Margins Problem

I was having trouble in Delphi because the TPageSetupDialog component was repoting the margins at o.5 inches but when the dialog was shown the margins were all shown as 1 inch. If the cancel button is pressed the margins are still reported as 0.5 inches. If the OK button is pressed the margins are now reported at 1 inch and the control and the screen now match up.

This is obviously confusing for a user. The program reads 0.5 inch margins from the dialog and uses these values. The user looks at the dialog and sees 1 inch margins and thinks everything is OK. The the printout shows 0.5 inch margins and the user starts to attack the computer.

The problem is that the pagesetupdlg defaults to 1 inch margins instead of using the values in the rtMargin parameter of the PAGESETUPDLG Structure (which is the Rect object the Delphi maps to its TPageSetupDialog properties).

To solve this set the psoMargins flag and the dialog will use the values in rtMargin instead of its silly defaults.

Thursday, February 7, 2008

ASP.NET custom controls dll issues

I have an ASP.NET vb website that uses a large number of custom controls that I modify a lot during development. These rely a bit on some generic code from the application and some of the settings in web.config. Thus they are a bit of a pain to compile by themselves (as far as I know anyway).

I have since included customcontrols.vb into my project as well as the customcontrols.dll. Any change to customcontrols.vb has an effect as soon as the project is compiled but the dll is not changed.

So you'd think that the dll isn't required right? Its not being modified and the changes are present in the website. Right? Wrong, for some reason the dll still has to be there.

This is mainly just a note to myself not to delete it again and spend an hour trying to work out why the website wouldn't compile. I think the dll is required for asp to know about the declarative syntax for the controls, so it can't just be any dll. Then when the controls are actually created the customcontrols.vb code must take precedence over the dll.

Monday, December 10, 2007

Dynamic SiteMaps: SiteMapNode, SiteMapResolve

I was dynamically changing sitemaps in Asp.Net 2.0 using c#.

I wanted to remove a node in the site map chain under certain conditions.

You would think this could be done by removing the errant node (1) from its parent node (2) and then adding in the node that you want (3) instead. However this doesn't work, you also need to set parentnode of (3) to (2).

Then its all tickety boo.

Thursday, September 20, 2007

ASP.Net Ajax Update Panel changes TextBox Width

I had an UpdatePanel inside a table.

The table width was set to 100%.

The TextBox width was set to 90%.

On a button click the text of the TextBox was changed. The page updates asynchronously as hoped but the text box width changed to be larger than the page width. I have read some forum posts and it seems that it is not possible to stop this for some reason.

Instead of setting the width of the TextBox we can set the Columns attribute instead. This is obviously not as good and will cause problems with differing font sizes but is better than any other alternative I have found.

I assume that this is a bug in the Ajax implementation, or possibly within the browser.

Wednesday, September 12, 2007

ASP.NET Session State Variables NULL is the same as 0 for an integer

I set a session variable to Integer 0 and then tried to read it back in to an integer.

The result was a NULL value.

Writing the value as a string and then converting it when reading it back in solved the problem. I assume that 0 is the default initial value for an integer and so ASP did not bother to save it, although I haven't checked this. This would mean that other types would have the same problem.