Stefan Pienaar
I would love to change the world, but they won't give me the source code

ValidationSummary and MaintainScrollPositionOnPostback

March 11, 2011 18:19 by StefanPienaar

When a ValidationSummary is used on an asp.net page, the MaintainScrollPositionOnPostback property no longer works as expected when one of the validation controls fails. This is because the ValidationSummary calls "window.scrollTo(0,0)" when the page is not valid.

To work around the problem, overwrite the scrollTo function on the page:

<script language="javascript" type="text/javascript">

window.scrollTo = function () { }

</script>

Reference: http://forums.asp.net/t/1157863.aspx


Categories: .net
Actions: E-mail | Permalink | Comments (0)

Changing regional settings in an ASP.net Website

June 25, 2009 13:52 by stefanpienaar

One of the ways in which the regional settings applicable to a website can be changed is by using the Session.LCID property. This is especially useful if you do not have direct access to the server where your website resides. Changing this property will change the way variables are formatted (for example dates and currency).

To change the regional settings to South Africa, add this line to your page load event:

Session.LCID = 7177;


Currency values will now be prefixed with a “R” instead of the default “$”.

Unfortunately it doesn’t seem like this setting is preserved throughout postbacks (contrary to what you would expect of the Session object) so I would recommend adding it to your Master Page’s page load event.

Here’s a list of available codes and their respective languages:

1078 Afrikaans 1052 Albanian
5121 Arabic(Algeria) 15361 Arabic(Bahrain)
3073 Arabic(Egypt) 2049 Arabic(Iraq)
11265 Arabic(Jordan) 13313 Arabic(Kuwait)
12289 Arabic(Lebanon) 4097 Arabic(Libya)
6145 Arabic(Morocco) 8193 Arabic(Oman)
16385 Arabic(Qatar) >1025 Arabic(Saudi Arabia)
10241 Arabic(Syria) 7169 Arabic(Tunisia)
14337 Arabic(U.A.E.) 9217 Arabic(Yemen)
1069 Basque 1059 Belarusian
1026 Bulgarian 1027 Catalan
3076 Chinese(Hong Kong) 2052 Chinese(PRC)
4100 Chinese(Singapore) 1028 Chinese(Taiwan)
1050 Croatian 1029 Czech
1030 Danish 2067 Dutch(Belgian)
1043 Dutch(Standard) 9 English
3081 English(Australian) 10249 English(Belize)
2057 English(British) 4105 English(Canadian)
9225 English(Caribbean) 6153 English(Ireland)
8201 English(Jamaica) 5129 English(New Zealand)
7177 English(South Africa) 11273 English(Trinidad)
1033 English(United States) 1061 Estonian
1080 Faeroese 1065 Farsi
1035 Finnish 2060 French(Belgian)
3084 French(Canadian) 5132 French(Luxembourg)
1036 French(Standard) 4108 French(Swiss)
2108 Gaelic(Irish) 1084 Gaelic(Scots)
3079 German(Austrian) 5127 German(Liechtenstein)
4103 German(Luxembourg) 1031 German(Standard)
2055 German(Swiss) 1032 Greek
1037 Hebrew 1081 Hindi
1038 Hungarian 1039 Icelandic
1057 Indonesian 1040 Italian(Standard)
2064 Italian(Swiss) 1041 Japanese
1042 Korean 2066 Korean(Johab)
1062 Latvian 1063 Lithuanian
1071 Macedonian 1086 Malaysian
1082 Maltese 1044 Norwegian(Bokmal)
2068 Norwegian(Nynorsk) 1045 Polish
1046 Portuguese(Brazilian) 2070 Portuguese(Standard)
1047 Rhaeto-Romanic 1048 Romanian
2072 Romanian(Moldavia) 1049 Russian
2073 Russian(Moldavia) 1083 Sami(Lappish)
3098 Serbian(Cyrillic) 2074 Serbian(Latin)
1051 Slovak 1060 Slovenian
1070 Sorbian 11274 Spanish(Argentina)
16394 Spanish(Bolivia) 13322 Spanish(Chile)
9226 Spanish(Colombia) 5130 Spanish(Costa Rica)
7178 Spanish(Dominican Republic) 12298 Spanish(Ecuador)
17418 Spanish(El Salvador) 4106 Spanish(Guatemala)
18442 Spanish(Honduras) 2058 Spanish(Mexican)
19466 Spanish(Nicaragua) 6154 Spanish(Panama)
15370 Spanish(Paraguay) 10250 Spanish(Peru)
20490 Spanish(Puerto Rico) 3082 Spanish,Spain-Modern Sort
1034 Spanish(Spain - Traditional Sort) 14346 Spanish(Uruguay)
8202 Spanish(Venezuela) 1072 Sutu
1053 Swedish 2077 Swedish(Finland)
1054 Thai 1073 Tsonga
1074 Tswana 1055 Turkish
1058 Ukrainian 1056 Urdu
1075 Venda 1066 Vietnamese
1076 Xhosa 1085 Yiddish
1077 Zulu 2048 default

source: http://www.123hostnow.com/articles/LCID.asp


Categories: .net
Actions: E-mail | Permalink | Comments (0)

SharePoint, Checking in all documents in a document library

March 20, 2009 11:20 by stefanpienaar

Manually checking in a bunch of files in a SharePoint Document Library is a mission. Create a new console application and paste the code below in your Main method:

* You will need to reference Microsoft.SharePoint.dll which can be found on the server where SharePoint is installed.

** Just remember to change the hardcoded site name (“http://myserver/”) and document library name (“MyDocumentLibrary”) to reflect your own settings.

using (SPSite site = new SPSite("http://myserver/"))
{
    SPWeb web = site.OpenWeb();
 
    foreach (SPList list in web.Lists)
    {
        if (String.Compare(list.Title, "MyDocumentLibrary", true) == 0)
        {
            SPDocumentLibrary docLib = (SPDocumentLibrary)list;
            // Take ownership of all checked out files in the document library
            foreach (SPCheckedOutFile checkedFile in docLib.CheckedOutFiles)
            {
                Console.WriteLine("Taking ownership of " + checkedFile.Url);
                checkedFile.TakeOverCheckOut();
            }
 
            // Check in all checked out files
            foreach (SPListItem item in list.Items)
            {
                SPFile file = null;
                try
                {
                    file = item.File;
 
                    if (file != null)
                    {
                        if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                        {
                            file.CheckIn("Checked in after uploading");
                            file.Update();
                            Console.WriteLine("File checked-in = " + file.Name);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not check-in file = " +
                                        file.Name + " : " + ex.Message);
                }
            }
        }
    }
}
 
Console.WriteLine("All documents checked in, press enter to close this application.");
Console.ReadLine();

ASP.net MVC 1.0

March 19, 2009 10:22 by stefanpienaar

MVC 1.0 has just been released, download it here.

Since it’s now considered stable and ready for production, I no longer have an excuse to not get my hands dirty :)


Categories: .net
Actions: E-mail | Permalink | Comments (0)

ASP.net and the Canonical Tag

March 10, 2009 18:50 by stefanpienaar

The major role players in the Search Engine world (Google, Yahoo and Microsoft) recently announced support for the canonical tag. What this means is you now have a way to *hint* the crawler to the best version of the current page.

Because “http://www.stefanpienaar.co.za”, “http://stefanpienaar.co.za” and “http://www.stefanpienaar.co.za/Default.aspx” are seen as different pages (with duplicate data) by search engine crawlers, we can now add the canonical tag to a page’s head tag to indicate which one should be used.

For example for my blog I could add the following tag between my opening and closing head tags:

<link href="http://www.stefanpienaar.co.za" rel="canonical" />

This way even if someone incorrectly links to “http://stefanpienaar.co.za”, the search crawler will now know that they are in fact the same page and which one I prefer.

To get your asp.net page to work with the canonical tag is really simple:

1. Change your <head> tag to include runat=”server” if it doesn’t already.

<head runat="server">
    <title>My Site</title>
</head>

 

2. From your page load event, create a new HtmlLink object and add it to the Page’s header:

HtmlLink lnkCanonical = new HtmlLink();
lnkCanonical.Href = "http://www.stefanpienaar.co.za";
lnkCanonical.Attributes["rel"] = "canonical";
Page.Header.Controls.Add(lnkCanonical);

Categories: .net
Actions: E-mail | Permalink | Comments (0)

Visual Studio Shortcuts

March 2, 2009 14:10 by stefanpienaar

Here’s a list of Visual Studio shortcuts I use quite often.

F12 Go to declaration
F10 Debug: Step over
Control + F10 Debug: Run to cursor
Control + - Move to previous edit point
Control + Shift + - Move to next edit point
Control + K, Control + M Create method stub when you call a method which does not exist yet
Control + . Same as above, but opens menu
Shift + Alt + Enter Full Screen mode
Control + M, Control + M Collapse current code block / region
Control + M, Control + O Collapse all methods
Control + M, Control + P Expands all method blocks
Control + ] Move to matching brace
Control + Shift + V Cycle through clipboard

 

There are a lot of sites with a lot more shortcuts as well as thorough descriptions (dofactory.com), the few listed above is meant as a personal reminder for my most frequent used shortcuts.


Categories: .net
Actions: E-mail | Permalink | Comments (0)

Small Basic - A Programming language for kids

November 10, 2008 10:28 by stefanpienaar

Microsoft just released the first version of Small Basic which is aimed to get kids into programming. They also claim that it's "aimed at brining the fun back to programming".

The syntax has really been simplified from it's ancient predecessor and it comes with a really cool IDE. Here's a few screenshots from the documentation:

The hello world example:

image

 

image

 

Randomised drawing:

image

Code:

GraphicsWindow.BackgroundColor = "Black" 
For i = 1 To 1000 
    GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor() 
    x = Math.GetRandomNumber(640) 
    y = Math.GetRandomNumber(480) 
    GraphicsWindow.FillEllipse(x, y, 10, 10) 
EndFor

 

Forms:

image

Code:

GraphicsWindow.MouseDown = OnMouseDown 
 
Sub OnMouseDown 
    GraphicsWindow.ShowMessage("You Clicked.", "Hello") 
EndSub

 

It seems like a really cool way to help kids (or even adults) get started with programming and later move on to a more advanced programming language.


Categories: .net
Actions: E-mail | Permalink | Comments (0)

New .NET Logo

October 27, 2008 17:42 by stefanpienaar

Looks like the .net team has decided to update their logo, It looks awesome!

image_3

Original Article


Categories: .net
Actions: E-mail | Permalink | Comments (0)

Copying the text in a MessageBox

October 16, 2008 11:31 by stefanpienaar

Found this awesome tip on Vadim's blog this morning. All you need to do when you want to copy the text inside a MessageBox, is make sure it has focus and press the control+c keys together (normal copy shortcut).

image

When you paste the text from the clipboard, you get something like this:

---------------------------
curl error
---------------------------
Empty reply from server
---------------------------
OK   
---------------------------

 

So simple, don't know why I never bothered trying it, no more rewriting an error message in Google!. Here is the link to the original article.


Categories: .net
Actions: E-mail | Permalink | Comments (0)

How to bind a DateTime value to a GridView

October 8, 2008 10:36 by stefanpienaar

Binding a datetime value is just as straight forward as binding any other data type to your grid, here is a quick reference however on how to properly format it:

When binding to a BoundField:

<asp:BoundField HeaderText="Date Finalised" 
                HtmlEncode="false" 
                DataField="DateAgreementCompleted" 
                DataFormatString="{0:yyyy/MM/dd}" />

 

The only thing to note here is the DataFormatString and the HtmlEncode property. The DataFormatString works in exactly the same way you would format a datetime when using .ToString (here's a quick reference on string formatting in c#) and the HtmlEncode ensures that the data is displayed properly and not encoded by asp.net.

When binding to a TemplateField:

<asp:TemplateField HeaderText="Date Finalised">
    <ItemTemplate>
        <asp:Label ID="lblDate" runat="server" 
            Text='<%# Bind("DateAgreementCompleted", "{0:yyyy/MM/dd}") %>' />
    </ItemTemplate>
</asp:TemplateField>
Here you can see that we use the Bind function as we normally would but we included a second parameter to the function ("{0:yyyy/MM/dd}")
Categories: .net
Actions: E-mail | Permalink | Comments (0)