Prevent an Asp.Net Button from triggering a Postback

Sometimes, you want an Asp.net Button to not trigger a postback.

One solution is, don't use an asp.net button, use an html input or link tag instead. However, you may want to use an asp:XXXButton for consistency with the rest of your page; or it may seem a simple way to make the text on the button localisable (although you can equally achieve that on a plain old html control if you give it an id, a runat="server" and probably a meta:resourcekey); or you may have other stuff you want to do with it serverside.

To stop an asp.net button causing a postback, do it like this:

<asp:LinkButton id="btnX" OnClientClick="return false;" runat="server" />

The more likely scenarios is, that you want to run clientside javascript. In that case, put ";return false;" after your javascript call:

<asp:LinkButton id="btnX" OnClientClick="functionToCall();return false;" runat="server"/>

Write to a text file from MS SQL Server

There are several things you need to know to be able to do this.

  1. How to enable ad hoc queries.
    This guy enable-Ad-Hoc-Distributed-Queries shows you how.
  2. How to format the driver string and specify your directory and filename. Like this:
    OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=c:\MyDirectoryName;','select * from MyTextFile.txt')
  3. How to create a text file in the right format. This format works:
    "ColumnName1","ColumnName2"
    "value11", "value21"
    "value21", "value22"

There. That was only slightly unreasonably difficult. Now you can

Select * from OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)}; DefaultDir=c:\MyDirectoryName;','select * from MyTextFile.txt')

and

Insert into OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};
DefaultDir=c:\MyDirectoryName;','select * from MyTextFile.txt')
Values ('Col1', 'Col2')

Localising programmatically in asp.net code behind

The usual examples for asp.net localisation are for the aspx markup page, not for the code behind.

In fact localisation, accessing localisation resource files programmatically, in code behind is also dead simple. You use GetLocalResourceObject() like so:

ctrl.Text = (string)GetLocalResourceObject("myCtrlKey.Text");
ctrl.AnotherAttribute = (string)GetLocalResourceObject("myCtrlKey.AnotherAttribute");

Using LocalResource means that for a page called MyPage.aspx, you have created a resource file called MyPage.aspx.resx and/or MyPage.aspx.{culturename}.resx in the special directory App_LocalResource.

If you like GlobalResources instead of local, use the special directory App_GlobalResource
to hold a resource file called MyResourceFileName.resx and call:

ctrl.Text= (string)GetGlobalResourceObject("MyResourceFileName", "myGlobalKey");

as per http://msdn.microsoft.com/en-us/library/ms227982.aspx

For a bit more of an overview of asp.net localisation, the page at http://msdn.microsoft.com/en-us/library/ms227427.aspx should only take you a few minutes to scan.