13 June, 2008

.Net Tips and Tricks (Part -II)

How to form Color Picker in ASP.NET
[Click to enlarge]

Make sure to Import the System.Reflections namespace, as well as the System.Drawing namespace, for this example

Prevent caching of webform in Asp.net

private void Page_Load(object sender, System.EventArgs e)

ASP.Net Server Controls Not Showing on pages

Try running aspnet_regiis from the command prompt. Here's the default location: C:\WINNT\Microsoft.NET\Framework\<>\aspnet_regiis.exe -i

Currently logged in user

To get the user ID of the user who is currently logged in, you would get it using this: System.Web.HttpContext.Current.User.Identity.Name

Get windows Identity(win auth)


Disabling a Button Until Processing is Complete

Here's the scenario - let's say you have an Insert subroutine, called 'doInsert'. You want to immediately disable the Submit button, so that the end-user won't click it multiple times, therefore, submitting the same data multiple times.

For this, use a regular HTML button, including a Runat="server" and an 'OnServerClick' event designation - like this:

INPUT id="Button1" onclick="document.form1.Button1.disabled=true;" type="button" value="Submit - Insert Data" name="Button1" runat="server" onserverclick="doInsert"

Then, in the very last line of the 'doInsert' subroutine, add this line: Button1.enabled="True"

Adding a MailTo Link inside a DataGrid

Suppose you have a database table which contains an email field, and you'd like to display that field, as well as others in a DataGrid. Let's go one step further - - let's say you'd like to display that email field as a clickable MailTo link. One way that this can be easily be done using TemplateColumns. First, you create your DataGrid start and end tags. Then, inside these tags, you include 'Columns' start and end tags. That's where you put your TemplateColumn. Here's how to do it

Use Path.GetRandomFileName() or Path.GetTempFileName() when working with temp files
Do not reinvent function for generating unique name for temporary files. Use one of the existing methods:
*** System.IO.Path.GetTempFileName() - use this method if you want to create temporary file in user's temp folder.
*** System.IO.Path.GetRandomFileName() - use this method if you just want to generate unique file name.

Popup Window Code

You can use Javascript to do so. e.g.

input type="Button" Value="Open New" onclick="Javascript:window.open('webform1.aspx');"

Uploading Large Size File
The default file size limit for the FileUpload control in ASP.Net is 4Mb. To get around this, in the HTTPRuntime section of the Web.config file, you can set the 'maxRequestLength' (in kb):
[httpRuntime maxRequestLength="1500000" /]

How to Register User Controls and Custom Controls in Web.config

ASP.NET 2.0 makes control declarations much cleaner and easier to manage. Instead of duplicating them on all your pages, just declare them once within the new pages->controls section with the web.config file of your application:

Once you register the controls within the web.config file, you can then just use the controls on any page, master-page or user control on your site like so (no registration directives required):
[form id="form1" runat="server]
[scott:header ID="MyHeader" runat="server" /]

No comments: