Tuesday, October 28, 2008

Essential Visual Studio Tips & Tricks that Every Developer Should Know

Found this great post on Stephen Walther's blog.

tip 1: don't need to select a line to copy or delete it

If you want to copy a line of code then you can simply press CTRL-c to copy the line and press CTRL-v to paste the line. If you want to delete a line, don’t select it first, just press CTRL-x. You’ll be surprised how much time this one tip will save you.

tip 3: Never create properties by hand

just type prop + TAB + TAB. When you type prop + TAB + TAB, you get a code snippet (template) for entering a property. Use TAB to move between the template parameters. Press the ENTER key when you are finished creating the propert.

I found these two are most useful and time saving for me. Others are at: http://weblogs.asp.net/stephenwalther/archive/2008/10/21/essential-visual-studio-tips-amp-tricks-that-every-developer-should-know.aspx

Monday, October 27, 2008

Jing Project: Visual conversation starts here.

Jing is a great screen capture tool and it's free!! This is so far the best screen capture tool I used. Another feature is it also can record your activity, so it also a great tool to create the video demo.

www.jingproject.com

XNView

Photo viewing tool. like ACDSee, but it is FREE.
NView

Tuesday, October 21, 2008

Take dnn site offline

It's better to take the DNN site offline for maintenance. The best way to do this is to upload an App_Offline.htm file to the root.

Monday, October 06, 2008

Navigate the parent page from the IFrame page (child page)

When user clicks the link/button on a page inside the IFrame, it will bring up another page inside the IFrame and the parent page will not change.

Sometimes we do need the link/button on the Iframe page update the parent page. Here is how to do it.

If the link is used, make the target property "_top".
HyperLink

If the button is used, in the code-behind, do this:
Response.Write("<" + "script>" + "window.open('http://webpage_address','_top');<" + (char)47 + "script>");

Thursday, September 04, 2008

Use overloaded web methods in web services (asmx)

In order to use the overloaded methods in the web services (asmx), two things need to be done.

1. When creating a new webservice in Visual Studio .NET 2005, the IDE automatically adds the following line on top of your serviceName.cs file:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

Change it to WsiProfiles.None

2. Add the MessageName in the [WebMethod(MessageName="UniqueName1"]

Wednesday, September 03, 2008

Make default button on the page

There are different ways control the page behavior when user enter the text in the text box then press the Enter key.

One way is to user the Javascript function to monitor the key pressed. If it's the enter key, then activate the submit button (the default button).

Another way is to put the page around a panel and use the "defaultbutton" property.

The best way to do this is to use the "UseSubmitBehavior" property for the button. Just make it false for buttons that not default.

Tuesday, August 26, 2008

Export data to csv file

Use the following code to export data to the csv file:

string attachment = "attachment; filename=MyFilename.csv";
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "text/csv";
Response.AddHeader("Pragma", "public");
Response.Write("hello world"); //data goes here;
Response.End();

Clear the current page (window)

In order to clear out the current page (make the page as a blank page), use following code:

Response.Flush();//will send the current response stream to the browser

Response.End() ;// will end the response,

Friday, August 22, 2008

Change the table schema

To change the schema of the tables, use this script command:

Alter schema "new_schema_name" transfer "old_schema_name.table_name"

Thursday, August 14, 2008

Retrieve images from Database using ASHX

Most of articles (which I found) about retrieve images from database are using Response.writebanary(...) to aspx file, then point the image's url to this aspx file. It worked fine for the most asp.net website. The luck ran out for me when I tried to implement a module for a dnn site. Because DNN only allows the user control (ascx) files, I had trouble to imped the image on my user control inside the DNN. The DNN will log me out every time after I displayed an image from the database.

The solution is quite simple, but took me a while to found it. Instead of using the aspx page as the image url to display the image on the page, use ASHX (HTTPHandle) file instead. The code used inside the ASHX file is almost the same as in the ASPX page.

For more detail on how to retrieve the image from database using ASHX: Go here

Tuesday, August 12, 2008

Restart IIS

To restart IIS

  • Click Start, click Run type IISReset, and then click OK.

    A Command Prompt window opens displaying the status of the IISReset command. You should read the status at the command prompt to make sure that IIS stops and restarts.

http://msdn.microsoft.com/en-us/library/ms957500.aspx

Thursday, August 07, 2008

ASP.NET UploadFile control

I am currently working on a project requires uploading the image to the database. It also requires to have a confirmation screen before saving the image to the database. What I found out is the UploadFile control's PostedFile object will get lost every time after the postback. So, there is no file on the confirmation screen to be saved to the database.

Only workaround I found for this issue is to save the PostedFile object to the Session state.

Monday, August 04, 2008

Create Javascript inside the ASP.net code behind page

I often ran into the situation that need to create the Javascript function dynamically instead of having it on the Aspx page. For example, if there is a text box inside one of the view in the MultiView, the Javascript function to detect the Enter Key press will not work when the view with text box is not showing (the rendered html code does not contain this specific text box).

In case like this, we need to create the Javascript code dynamically. The Javascript function will only show up if the perticular view is showing. We use the "Page.ClientScript.RegisterClientScriptBlock" to accomplish this problem.

First, use ServerSideControl.FindControl("controlName").ClientId to find unique client side id for this control.

Then use the Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ScriptId", "my Javascript code") to register the Javascript code.

To find this control inside the Javascript function: var btn = document.getElementById('" + strMyControlId + "');"

We can put above code inside a function and call it before making the view containing the text box visible to true.

MSDN: Page.ClientScript.RegisterClientScriptBlock
JavaScript with ASP.NET 2.0 Pages - part I

Monday, July 21, 2008

Progress Bar

Essential Object offers a complete free to use progress bar for ASP.net. It is very nice tool with great flexibility for user to change the layout and it's very easy to use. Here is the link. However, the disadvantage is you'll have to install the full asp.net control set to your VS. The dll has to be included in the bin folder to deploy the site. (Telerik has a progress bar only for using with the upload control. I find it is odd that they didn't including a progress bar for general use.)

An alternative solution is to use an simple user control. The disadvantage is the look & feel of the progress bar. It's not as good looking as the progress bar from EO, but you can get the full source code. Here is the code in c#:

ProgressBar.aspx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProgressBar.ascx.cs" Inherits="ProgressBar" %>

BorderWidth="1px"
CellPadding="1"
CellSpacing="1"
Height="10px"
Width="200px">




ProgressBar.ascx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ProgressBar : System.Web.UI.UserControl
{
private System.Drawing.Color _colFillColor = System.Drawing.Color.Green;
private System.Drawing.Color _colBackcolor = System.Drawing.Color.White;
private System.Drawing.Color _colBorderColor = System.Drawing.Color.Black;
private int _intBorder = 1;
private int _intCellspacing = 1;
private int _intCellpadding = 1;
private int _intHeight = 15;
private int _intWidth = 130;
private int _intBlockNumber = 20;
private int _intValue;
TableRow _tblBlock;

public System.Drawing.Color BGColor
{
get
{
return _colBackcolor;
}
set
{
_colBackcolor = value;
}
}

public System.Drawing.Color FillColor
{
get
{
return _colFillColor;
}
set
{
_colFillColor = value;
}
}

public System.Drawing.Color BorderColor
{
get
{
return _colBorderColor;
}
set
{
_colBorderColor = value;
}
}

public int BorderSize
{
get
{
return _intBorder;
}
set
{
_intBorder = value;
}
}

public int Cellpadding
{
get
{
return _intCellpadding;
}
set
{
_intCellpadding = value;
}
}

public int CellSpacing
{
get
{
return _intCellspacing;
}
set
{
_intCellspacing = value;
}
}

public int Blocks
{
get
{
return _intBlockNumber;
}
set
{
_intBlockNumber = value;
}
}

public int Value
{
get
{
return _intValue;
}
set
{
_intValue = value;
}
}

public int Height
{
get
{
return _intHeight;
}
set
{
_intHeight = value;
}
}

public int Width
{
get
{
return _intWidth;
}
set
{
_intWidth = value;
}
}

protected void Page_PreRender(object sender, System.EventArgs e)
{
int intBlocks;
// add a new row to the table
_tblBlock = new TableRow();
// create cells and add to the row
for (intBlocks = 1; (intBlocks <= this.Blocks); intBlocks++)
{
TableCell tblCell = new TableCell();
tblCell.Text = " ";
decimal temp = (Convert.ToDecimal(this.Blocks) / 100) * Convert.ToDecimal(this.Value);
if ((intBlocks <= temp))
{
tblCell.BackColor = this.FillColor;
}
_tblBlock.Cells.Add(tblCell);
}
tblProgressBar.Rows.Add(_tblBlock);
// set the progress bar properties
tblProgressBar.CellPadding = this.Cellpadding;
tblProgressBar.CellSpacing = this.CellSpacing;
tblProgressBar.Width = this.Width;
tblProgressBar.Height = this.Height;
tblProgressBar.BackColor = this.BGColor;
tblProgressBar.BorderColor = this.BorderColor;
LabelProgress.Text = this.Value + "% complete";
}
}

Wednesday, July 16, 2008

Debug javascript inside VS2005

writing JavaScript functions in the asp.net is not an easy task specially lacking the debugging feature in VS2005 (VS2008 has this build in). However, there is an easy way to enable debugging feature in the VS2005.

1. uncheck the "disabling scripting debug" in the IE internet option.
2. add "debugger;" in the script block to enable javascript debugging.

for more detail, click here

Tuesday, July 15, 2008

Restore the SQL 2005 Database using script

The SQL 2005 management studio has wizard for restore the database. It works pretty good for most cases, but I found the scripting is much for flexible. Recently, I tried to restore a database to with a different name, but the wizard won't let me to do it. Here is the example of the alternative script way. It worked great for me.

Restore Database YourDBName
From Disk = 'TheBAKFileName.bak'
With Move 'MDFFileName' To 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\YourDBName.MDF',
Move 'LDFFileName_Log' To 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\YourDBName.ldf',
REPLACE

Tuesday, July 08, 2008

DNN installation problem

I tried to install the latest DNN 4.8.x last night, but the process is not very smooth. In the middle of the installation, the DNN hung up on the install the database script page. Not sure what was going on. Some people had the same experience (google search). The "Custom" and "Typical" installation choices are not working for some reason. The "Auto" did the job. I think the "Auto" is the traditional way of installing the DNN.

Monday, July 07, 2008

Multiple IEs

When developing the web applications, we have to make sure that the application is not only running fine on multiple browsers (IE and firefox), but also running on different version of IEs (firefox always keep the user in the latest version).

The MultipleIEs by tredosoft is a great tool for web developers to test the application on multiple version of the IE. I have latest version of IE installed on my pc and added only IE 6 for MultipleIEs.

MultipleIEs links at: http://tredosoft.com/Multiple_IE

Wednesday, July 02, 2008

Prevent XSS in ASP.net (Regular expression)

I've been working on the security of a web application to prevent the XSS (cross site scripting). I read many articles about how to handle this issue. There are two ways that I found is easy to implement and will prevent most of the XSS attack.

The first is to use HttpUtility.HtmlEncode in the code behind page. It will encode specially characters in the script code, so the script will not run when the page is rendered.

The second is to use the Regular Expression validator on the client side (I think custom validaor calling the System.Text.RegularExpressions.Regex.IsMatch(string input, string pattern) is most secure).

Here are some frequently use regular expressions I found on the web:

1. Name
"^([A-Za-z']|-|\s)+$"

2. Address (allow number, text only, ., -, and space. no special characters are allowed)
"^([a-zA-Z0-9.]|-|\s)+$"

3. Email
"^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$"

4. Social Security #:
"\d{9}"

5. Date:
"^(([1-9])|(0[1-9])|(1[0-2]))\/(([0-9])|([0-2][0-9])|(3[0-1]))\/(([0-9][0-9])|([1-2][0,9][0-9][0-9]))$"

Regular Express Library