ThreadAbortException

April 2008 Entries

Web Control Library Template in Visual Studio 2008

If you are working with Visual Studio 2008, you might be looking for the Web Control Library template that used to be quite apparent in Visual Studio .NET 2003 and kind of hidden under "File - New - Project - Windows - Web Control Library" in Visual Studio 2005.  Check my earlier post on this http://geekswithblogs.net/ranganh/archive/2006/09/06/90370.aspx

Well, in Visual Studio 2008, this option is under "File - New - Project - Web - ASP.NET Server Control".  Yeah that is the place you you would find the template to create a Web Control Library.

Thought this was again tricky to figure out and would help if I post the details over here.

Cheers !!!

File Upload in UpdatePanel, ASP.NET AJAX

UDPATE: October 2, 2009

There is a better way of doing it with Ajax Control Toolkit version 3.0.30930 which works with .NET 3.5 SP1 and Visual Studio 2008 SP1.  Please read this post for a step by step instruction

One of the common queries I get across my sessions is that, the File Upload control doesnt work inside an Update panel.  All of us would like to implement a Gmail File Upload kind of interface and when you try to implement a similar thing using UpdatePanel (which works like a charm for other activities), it simply doesn't work.

The behaviour is expected.  The File Upload Control doesnt work inside an Update Panel due to security reasons and restrictions a browser implies.  They dont allow Javascript files to directly access the files in an user's sytem and dont allow to modify or access the details of a file when working with the File Upload Control.

There are a couple of ways to solve this issue, one using Update Panel and Post Back Triggers and the other using Iframes.

1. Use Update Panel, File Upload Control and use a PostBackTrigger Control to force a postback only for the File Upload Control

This approach works well without much tweaking except for that, there would be a postback only for the File Upload Control.  While the rest of the stuff happens asynchronously, using the UpdatePanel, when the user presses the "Upload" Button, the page will be refreshed.  Let us examine how we can accomplish this.  Place the following code within the <form> </form> tags in your ASP.NET Page.  

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

 <br />

<asp:Button ID="Button1" runat="server" Height="20px" onclick="Button1_Click" Text="Submit" Width="128px" />

<br />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<br />

<asp:FileUpload ID="FileUpload1" runat="server" />

<br />

<asp:Button ID="Button2" runat="server" Height="25px" onclick="Button2_Click" Text="Upload" Width="128px" />

<br />

<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

<br />

</ContentTemplate>

<Triggers>

<asp:PostBackTrigger ControlID="Button2" />

</Triggers>

</asp:UpdatePanel>

In the Code behind, add the following lines of code:-

 

protected void Button1_Click(object sender, EventArgs e)

{

Label1.Text = TextBox1.Text;

}

protected void Button2_Click(object sender, EventArgs e)

{

FileUpload1.PostedFile.SaveAs(@"C:\test\"+System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName));

Label2.Text = FileUpload1.PostedFile.FileName;

}

If you run the above sample, you would notice that upon entering something in the TextBox and clicking "Submit" (Button1) the Label above the File Upload Content, shows the Text you typed, without a page refresh.

However, when you select a file and click on the "Upload" (Button2) Button, you would notice that a postback happens and the file gets posted to the "C:\Test\" folder and also the full path is specified in the Label 2.

In the above code, I have not taken any steps regarding validation, checking if file exists etc., since it just shows how you could accomplish File Upload within Update panel.  In normal cases, you would write better code to accomplish a file upload feature.

2. Use Iframes and accomplish a truly Gmail Like File Upload Interface.

I thought of writing a post on this, but did a quick research and found that there are a few solutions posted by our MVPs / Community Folks and just thought of providing a link to the same.

http://vinayakshrestha.wordpress.com/2007/03/13/uploading-files-using-aspnet-ajax-extensions/

http://msmvps.com/blogs/luisabreu/archive/2006/12/14/uploading-files-without-a-full-postback.aspx

Note that this post doesnt claim any warranty / support for the above articles, though.

Cheers !!!

 

Mobile Web Forms in Visual Studio 2008

UPDATE:  I see a lot of comments which make me feel that this post has been useful.  There have been a lot of queries also around the inability to find the download link.  For the benefit of those, I am posting direct link here Download Mobile Web Form Templates for Visual Studio 2008

Now, for some for information around why it has been missing Visual Studio releases.  According to Omar Khan from the Web Development Tools team, ASP.NET Mobile Webforms are really only useful for last generation ‘WAP’ Devices.  Most modern devices have a full featured web browser and hence the regular ASP.NET Webforms or MVC to build websites that can run on such browsers.

Going further VS 2010 also doesnt feature ASP.NET Mobile Web Template and it will be a separate download similar to VS 2008.

If you have worked with Visual Studio 2008 and tried creating Mobile Web Applications, you would have figured out that the "Mobile Web Form" template is missing.  In the past, in Visual Studio .NET 2003 you had Mobile Web Forms that allow you to create webforms that can be rendered over different types of HTML, WML and XHTML capable devices.

Well, it is not shipped by default in Visual Studio 2008, but Omar Khan from the Web Development Tools Team has put up a few templates that can be downloaded and brings in those Mobile Templates required for creating mobile web applications.

You can download the templates from the Web Development Tools Team Blog Post here  There is a "readme" file attached to each of the extracted Zip folders.  But to make it simpler, once you install and unzip the package, you get a folder "ASP.NET Mobile Templates" and inside that you might find a bunch of folders.

Copy the Zip folders inside each of the "ASP.NET Web Application" Folders into your system's Visual Studio Templates folder.  For instance, there is a folder for "ASP.NET Web Application CS".  You need to copy the contents of this file into

c:\Documents and Settings\<username>\My Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C#

Similarly you would copy the contents of the "VB" folder into the Visual Basic folder under "ItemTemplates" above.

Once you copy these and restart Visual Studio 2008, you would be able to get the "Mobile Web Form", "Mobile Web User Control" and "Mobile Web Configuration File" under "My Templates" in "Add New Item" Wizard.

Cheers !!!