1.0Where
do I get the tools to get started with ASP.NET? [Top]
Here is a link to the WebPI installer for everything you need to start writing ASP.NET Web Forms and ASP.NET
MVC apps: http://go.microsoft.com/?linkid=9730538
<div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">After learning the information above, you can then move
on to learning AJAX in ASP.NET http://www.asp.net/learn/ajax-videos/ and
ASP.NET AJAX Overview.
</div>
<div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">Then you can try a starter-kit to learn how to develop a complete website:http://www.asp.net/downloads/starter-kits/</div>
You can also get more information at:
<div style="margin-left: 0cm;" class="MsoListParagraphCxSpFirst" mce_style="MARGIN-LEFT: 0cm">Get Started with ASP.NET</div>
<div class="Section1"></div> <div class="Section1">1.2What
are the differences between Web Sites and Web Applications? [Top]</div>
The default website project model uses the directory structure to define the contents of the project. In this
model, there is no project file and all files in the directory are part of the project.
However, in a Web application project, the files that are explicitly referenced in the solution's project file
are the only ones that are part of the project. These files are displayed in Solution Explorer and they are the only files that are compiled during a build.
For more information on the differences, you can refer to the following articles:
<div style="margin-left: 0cm;" class="MsoListParagraphCxSpMiddle" mce_style="MARGIN-LEFT: 0cm">Introduction to
Web Application Projects, refer to section: ‘Comparing Web Site Projects and Web Application Projects’ http://msdn2.microsoft.com/en-us/library/aa730880(VS.80).aspx.</div>
<div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">Before
calling Web Service in ASP.NET, you need to create a Web Service and add a Web Reference into ASP.NET. You can get more information from the following link:
http://www.asp.net/learn/videos/video-280.aspx.</div>
<div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">There are two approaches for calling
the Web Service, one is the synchronous approach, the other is the asynchronous approach.</div>
Synchronous approach for calling Web Service:
YourWebServiceName.Service someWS = new YourWebServiceName.Service();
someWS.WebServiceMethod();
<div class="Section1">You can choose the edition you need after downloading. The samples below demonstrate how to zip files.</div> <div class="Section1">
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
ZipOutputStream zos = null;
protected void Button1_Click(object sender, EventArgs e)
{
string[] pathCollection = new string[2];
PathCellction[0] = "c:\\folder1";
PathCellction[1] = "c:\\folder2";
StartZip(pathCollection, "filename");
}
protected void StartZip(string[] pathCollection, string strFileName)
{
MemoryStream ms;
Response.ContentType = "application/octet-stream";
strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
ms = new MemoryStream();
zos = new ZipOutputStream(ms);
addZipEntry(pathCollection);
ms.Close();
zos.Finish();
zos.Close();
Response.Clear();
Response.BinaryWrite(ms.ToArray());
Response.End();
}
protected void addZipEntry(string[] pathCollection)
{
for (int i = 0; i < pathCollection.Length; i++)
{
string strPath = pathCollection[i];
addZipEntry(strPath, strPath.LastIndexOf("\\") + 1);
}
}
protected void addZipEntry(string strPath, int baseIndex)
{
DirectoryInfo di = new DirectoryInfo(strPath);
foreach (DirectoryInfo item in di.GetDirectories())
{
addZipEntry(item.FullName, baseIndex);
}
foreach (FileInfo item in di.GetFiles())
{
FileStream fs = File.OpenRead(item.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string strEntryName = item.FullName.Remove(0, baseIndex);
ZipEntry entry = new ZipEntry(strEntryName);
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
fs.Close();
}
}
<div class="Section1">Also, there is a new GZipStream class introduced in .Net Framework 2.0 under System.IO.Compression. It is for GZip, which uses RFC1952, making it different from Zip.</div> <div class="Section1"> </div> </div> <div class="Section1"></div>
IPostBackEventHandler defines the method that ASP.NET server controls must implement to handle postback events. Calling functions like Page.GetPostBackEventReference and Page.GetPostBackClientHyperlink allows ASP.Net render the JavaScript Function __doPostBack(), which submits the form with postback.
</div> <div class="Section1">
To see how to perform postbacks for custom events, please check the following sample (onmouseover triggers __doPostBack):
</div>
Firstly, define on mouseover event on the client side to do the JavaScript function __doPostBack which can do postback to Code Behind.
</div> <div class="Section1">Page.GetPostBackClientHyperlink
will register __doPostBack JavaScript function to the client to execute __doPostBack('ImageButton1','onmouseover')
when the ‘onmouseover’ client side event of the ImageButton1 fires.</div> <div class="Section1"> </div> <div class="Section1">Related
thread: http://forums.asp.net/t/1227950.aspx</div>
1.6 How to create ASP.NET Server Side Table/Client Side Table dynamically? [Top]
ASP.NET Server Side Table has ‘runat="server"’, Client Side Table does not have ‘unat="server"’.
<div style="margin-left: 11.35pt;" class="Section1" mce_style="MARGIN-LEFT: 11.35pt">You can create ASP.NET Server Side Table in server side code in
Code Behind. </div>
<div style="margin-left: 11.35pt;" class="Section1" mce_style="MARGIN-LEFT: 11.35pt">You can create Client Side Table via JavaScript.
</div>
This post will demonstrate how to create an ASP.NET Table by using C# and inserting controls to the Table dynamically: http://forums.asp.net/t/1213400.aspx.
1.8 How to check if the users are online or offline? [Top]
Many developers tend to use ‘Session_End’ event to mark a user as ‘Offline’, but this may not be a good idea, because
there are no one-to-one mappings between an ASP.Net session and a user. For example if a user opens 2 separate instances of Internet Explorer (e.g. 2 separate IE processes) on his workstation, he can have 2 ASP.Net sessions open. The ‘Session_End’ event of
one of the session does not necessarily mean that the user is offline, they can still be using the website.
In addition, ‘Session_End’ event is only available in the ‘InProc’ session mode. If you are trying to store session states in the State Server
or SQL Server, ‘Session_End’ event, it will never fire.
I would suggest using the last visiting time as the ideal approach.
Membership helps build the user database and there is a convenient approach to get it via
Membership.GetUser(string userName).IsOnline.
If you don’t use Membership, you can use the method below which uses the same principle in Membership:
<div class="MsoNormalCxSpMiddle">In the application, you can define a global dictionary
membershipDictionary to store the information about users online and offline.</div>
<div class="MsoNormalCxSpMiddle">Use an AJAX Timer (or simply XmlHttpRequest in client side JavaScript) in the
pages to update the LastVisitTime of the user and store it into membershipDictionary at intervals.</div>
<div class="MsoNormalCxSpMiddle">From time to time, visit the dictionary
membershipDictionary to filter out the idle users (who have not visited the page for a long time) and also retrieve information on how many users are still online. Regular maintenance can be done in a separate thread or a server-based timer.
Please also be aware of thread synchronization issues.</div>
1.9 How to insert a special non-ASCII characters into a database? [Top]
When you try to insert a special non-ASCII characters into a database, such as ‘Poor’s’, you find that they can’t be inserted, by using “INSERT
INTO table VALUES(1,’Poor’s’)”,
only “Poora€?s” can be inserted.
<div class="Section1">You can insert the value as parameter into the database.</div>
InsertCon.Open();
SqlCommand InsertCmd = new SqlCommand("INSERT INTO table VALUES (1,@name)", InsertCon);
InsertCmd.Parameters.Add(new SqlParameter("@name", SqlDbType.NVarChar,200));
InsertCmd.Parameters["@name"].Value = TextBox1.Text; //Assume the input is validated and legal.
InsertCmd.ExecuteNonQuery();
InsertCon.Close();
Using this approach also prevents potential SQL injection attacks.
There are several approaches to transfer data between pages:
Query Strings: You can transfer data between pages
with query strings. The data will be displayed in the URL as parameters. Sometimes you want to encode the parameters, which can be done with
HttpServerUtility.UrlEncode
or HttpUtility.UrlEncode.
For example:
In page1, you can transfer the data to another page by redirecting.
QueryString["parameter"]
to get the data from page1.
Cookies: Cookies can be stored on client side machines. If you do not set the cookie's expiration, the cookie will be created but will not be stored on the user's hard disk. Instead,
the cookie will be maintained as part of the user's session information. When the user closes the browser or if the session times out, the cookie is discarded.
You can add cookies to the Response.Cookies collection in a couple of ways, the following example shows two methods:
1.11 How to hide the URL parameters in the browser? [Top]
There are three ways to achieve it.
1. Using Server.Transfer
When you transfer URL via
Server.Transfer("sample2.aspx?x=1&y=2"),
in page2 you can retrieve these two variables via Request but it’ll only display the URL of the previous page which executed Server.Transfer.
Please check the sample code below, the page ‘link.aspx’ lists all of the files available in a folder, when clicking the hyperlinks
in ‘link.aspx’, the user can download the files via ‘downloading.aspx’.
1.13 What are the differences between <%# %> and <%= %>? [Top]
The <%# %> is used for data binding where as <%= %> is used to output the result of an expression. The expression inside <%# %> will
be executed only when you call the page's or control's DataBind method. The expression inside <%= %> will be executed and displayed when it appears on the page.
1.14 Why can’t I step into a DLL’s source code from ASP.NET application while debugging? [Top]
First of all, if you are using the ‘Release’ build of the modules, please try building the modules or application under ‘Debug’ build.
If the debugger cannot load the DLL's .PDB file, we also cannot use the source code. The PDB files
are debug symbols that are generated with the modules (EXE/DLL). They hold the necessary information for source code level debugging. The debugger will not load a PDB that does not match
the binary being debugged (For more information, see http://msdn.microsoft.com/en-us/library/ms241903.aspx.
You can check if the debugger loads this PDB file by using the following steps:
<div style="line-height: normal;" class="MsoNormal" mce_style="LINE-HEIGHT: normal">Click ‘Debug’ menu and then select ‘Windows’ to enter the ‘Modules’ window.</div>
<div style="line-height: normal;" class="MsoNormal" mce_style="LINE-HEIGHT: normal">Locate your DLL module
to see if the PDB file is loaded. If the PDB file is not loaded, you can load it manually by right clicking the DLL module and then select ‘Load Symbols’ option).</div>
1.15 During a debugging session, why isn’t the break point hit? [Top]
There are several reasons why this could happen:
1.Matching symbols cannot be found without symbols, breakpoints
cannot be mapped from source code to machine code while it is being executed. 2.The source code you have opened inside Visual Studio 2005 does not match the build of the executable code you’re trying to debug. If you are aware of this but want the debugger to attempt to set
the breakpoint anyway, you can do one of following: 2.1. Right click on the breakpoint and choose ’Location…’ Then check the ’Allow the source
code to be different from the original version’ checkbox. 2.2. Turn off source code verification. Go to the menu Tools->Options->Debugging->General
and uncheck ‘Require source files to exactly match the original version’. 3.The component or program you have set the breakpoint
to, have not been loaded into the memory yet. When a DLL or component is loaded to your program, the debugger is notified and will attempt to ‘bind’ the breakpoint so that it can be hit.
1.17 What does the character ‘~’ mean in ASP.NET applications? [Top]
The character tilde (~) in the ASP.NET paths points to the root of the web application.
Web developers are familiar with using relative paths for all links, including hyperlinks,
images and style sheets to be able to move around web pages collectively.
In ASP.NET when using User controls the relative paths can be difficult to use. The typical solution to this is to use
web-root absolute paths instead here, such as ’~/UserControl1.ascx’, resulting in the hard-coded sub-directories that are common on ASP.NET sites.
The correct solution to this problem is to use app-relative paths instead, which ASP.NET nicely makes possible through the use
of the tilde (~) prefix. Instead of <ahref="/UC/Page.aspx">,use <aid="A1"
href="~/Page.aspx"
runat="server">. The same ~ notation works for images also, as long as you add runat="server". There is also a ResolveUrl method that allows you to use
~ in your own code, which is one possible way to get stylesheet paths app-relative.
For more information about ASP.NET Web Site Paths, please see:
<div style="border-bottom: windowtext 1pt solid; border-left: medium none; padding-bottom: 1pt; padding-left: 0cm; padding-right: 0cm; border-top: medium none; border-right: medium none; padding-top: 0cm;" class="Section1" mce_style="BORDER-RIGHT: medium none;
PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 1pt; BORDER-LEFT: medium none; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid">
</div>
2.
Send mail
2.1What are the differences between ‘System.Net.Mail’ and ‘System.Web.Mail’? [Top]
System.Web.Mail and System.Net.Mail are both built-in libraries provided by Microsoft in .NET framework. System.Web.Mail is supported
by all versions of .Net Frameworks at the moment. However, System.Web.Mail is obsolete since .Net 2.0 could be removed from the class library in the future. In .Net 2.0, the new ‘System.Net.Mail’ namespace is intrdocued. It is recommend against ‘System.Web.Mail’,
if you are developing applications targeting .Net 2.0 and later.
2.3Why SMTP Mails does not work when I try to send? [Top]
<div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">If
you are using the System.Web.Mail namespace in .Net 2.x or a later edition, you can try using System.Net.Mail instead.</div>
<div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">Please ensure that the ’From Email Address’, password,
’To Email Address’, SMTP server and port of the server are correct. </div>
<div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">Please
check the SMTP configuration and start Default SMTP Virtual Server by configuring IIS:
http://forums.asp.net/t/1214374.aspx.</div>
<div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">Yahoo
and Hotmail don't support SMTP: http://forums.asp.net/t/1212160/2144153.aspx.</div>
<div style="border-bottom: windowtext 1pt solid; border-left: medium none; padding-bottom: 1pt; padding-left: 0cm; padding-right: 0cm; border-top: medium none; border-right: medium none; padding-top: 0cm;" class="Section1" mce_style="BORDER-RIGHT: medium none;
PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 1pt; BORDER-LEFT: medium none; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid">
</div>
3.GridView
3.1
How to get the index of a row which contains the clicked control in GridView? [Top]
It can be implemented by using property CommandName and Event RowCommand of GridView control.
To get the index of row which contains the clicking control in GridView, you can try the method below.
1. Define property CommandName in ImageButton: CommandName="display"
5.3How to pass values between Code Behind and client side JavaScript? [Top]
Passing values from Code Behind to client side JavaScript:
You can use <%=variable %> in JavaScript code body to transfer the value of the server side variable in Code Behind. Note that member field
must be non-private in Code Behind, if it will be included in the <% %> block.
For example, you can use JavaScript code: alert("<%=DateTime.Now.ToString() %>"); to get the server time. Another approach
is using ScriptManager to register a JavaScript code in Code Behind:
You can retrieve the value from Hidden control in Code Behind after
storing the variable into Hidden control by using JavaScript. In the above sample, 'OnClientClick="clientButtonClick()"' will be executed before the server side click event (“onclick="Button1_Click"”).
5.4How to display dynamic ToolTips for controls? [Top]
Some controls have the property ‘ToolTip’ which can display the details when mouse cursor moves over.
However, when you want to display dynamic tooltips for every cell in GridView or display dynamic tooltips of
every specific day in Calendar, the property ‘ToolTip’ is not sufficient.
In order to implement the dynamic tooltips for the controls, you can use Control.Attributes.Add and tooltips
JavaScript function to deploy respective tooltip for every control.
Display the dynamical tooltips of every cell in GridView:
</div>
Please download wz_tooltip.zip from the below link:
<div style="margin-left: 0cm;" class="MsoListParagraphCxSpMiddle" mce_style="MARGIN-LEFT: 0cm">You can add the
‘onmouseover’ event to every cell to display tooltips via ‘Attributes.Add’ in GridView1_RowDataBound.</div>
Sincerely,
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Allen Chen –...
All-Star
40943 Points
4949 Posts
FAQ in Getting Started
Nov 25, 2008 07:31 AM|LINK
1. General Questions
1.0 Where do I get the tools to get started with ASP.NET?
1.1 How do I learn ASP.NET?
1.2 What are the differences between Web Sites and Web Applications?
1.3 How do I use Web Service in ASP.NET 2.0?
1.4 How to zip a file or a folder?
1.5 How do controls carry out postbacks and how are the control events triggered?
1.6 How to create ASP.NET Server Side Table/Client Side Table dynamically?
1.7 How can I encrypt a string such as passwords or QueryStrings?
1.8 How to check if the users are online or offline?
1.9 How to insert a special non-ASCII characters into a database?
1.10 How to transfer data between pages?
1.11 How to hide the URL parameters in the browser?
1.12 How to download files?
1.13 What are the differences between <%# %> and <%= %>?
1.14 Why can’t I step into a DLL’s source code from ASP.NET application while debugging?
1.15 During a debugging session, why isn’t the break point hit?
1.16 How to speed up your ASP.NET pages?
1.17 What does the character ‘~’ mean in ASP.NET applications?
2. Send mail
2.1 What are the differences between ‘System.Net.Mail’ and ‘System.Web.Mail’?
2.2 How to send email?
2.3 Why SMTP Mails does not work when I try to send?
3. GridView
3.1 How to get the index of a row which contains the clicked control in GridView?
4. Membership
4.1 How to deploy the custom Membership Database?
4.2 How to customize password generation?
5. JavaScript
5.1 How to update the data on another page?
5.2 Refresh the page in another page.
5.3 How to pass values between Code Behind and client side JavaScript?
5.4 How to display dynamic ToolTips for controls?
6. Image operation
6.1 How to resize an image?
6.2 How to display dynamically generated images?
7. Office related
7.1 Can I automate Office applications (e.g. Word and Excel) in ASP.Net?
7.2 How to retrieve data from an Excel file and export data from a database to an Excel file?
8. Validation & Regular Expressions
8.1 How can I implement complicated validations?
1. General Question
1.0 Where do I get the tools to get started with ASP.NET? [Top]
Here is a link to the WebPI installer for everything you need to start writing ASP.NET Web Forms and ASP.NET MVC apps: http://go.microsoft.com/?linkid=9730538
1.1 How do I learn ASP.NET? [Top]
Here is a step by step schedule for learning ASP.NET:
You can also get more information at:
- <div style="margin-left: 0cm;" class="MsoListParagraphCxSpFirst" mce_style="MARGIN-LEFT: 0cm">Get Started with ASP.NET</div>
- <div style="margin-left: 0cm;" class="MsoListParagraphCxSpFirst" mce_style="MARGIN-LEFT: 0cm">Data Access Tutorials</div>
- <div style="margin-left: 0cm;" class="MsoListParagraphCxSpFirst" mce_style="MARGIN-LEFT: 0cm">Popular Books</div>
- <div style="margin-left: 0cm;" class="MsoListParagraphCxSpFirst" mce_style="MARGIN-LEFT: 0cm">Starter Kits and Community
Projects</div>
<div class="Section1"></div> <div class="Section1">1.2 What are the differences between Web Sites and Web Applications? [Top]</div>The default website project model uses the directory structure to define the contents of the project. In this model, there is no project file and all files in the directory are part of the project.
However, in a Web application project, the files that are explicitly referenced in the solution's project file are the only ones that are part of the project. These files are displayed in Solution Explorer and they are the only files that are compiled during a build.
For more information on the differences, you can refer to the following articles:
http://msdn2.microsoft.com/en-us/library/aa730880(VS.80).aspx.</div>
http://msdn2.microsoft.com/en-us/library/aa983474.aspx.</div>
http://blogs.vertigo.com/personal/swarren/Blog/Lists/Posts/Post.aspx?ID=10.</div>
Related thread: http://forums.asp.net/t/1240650.aspx
1.3 How do I use Web Service in ASP.NET 2.0? [Top]
Synchronous approach for calling Web Service:
Asynchronous approach for calling Web Service:
<div class="Section1"> </div> <div class="Section1">You can get more information from the following link:</div>http://msdn2.microsoft.com/en-us/library/t745kdsh.aspx.
Related thread: http://forums.asp.net/t/1204640.aspx
1.4 How to zip a file or a folder? [Top]
It can be achieved by using java.util.zip.ZipFile, ICSharpCode.SharpZipLib.Zip or other library assemblies.
You can make use of java.util.zip.ZipFile to deal with zip files: http://msdn.microsoft.com/en-us/magazine/cc164129.aspx.
You can also try SharpZipLib:
This DLL file can be downloaded from: http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx.
<div class="Section1">You can choose the edition you need after downloading. The samples below demonstrate how to zip files. </div> <div class="Section1"> <div class="Section1">Also, there is a new GZipStream class introduced in .Net Framework 2.0 under System.IO.Compression. It is for GZip, which uses RFC1952, making it different from Zip.</div> <div class="Section1"> </div> </div> <div class="Section1"></div>Related thread: http://forums.asp.net/p/1252894/2326757.aspx#2326757
1.5 How do controls carry out postbacks and how are the control events triggered? [Top]
Please check these two interfaces: IPostBackEventHandler and IPostBackDataHandler.
<div class="Section1">IPostBackEventHandler defines the method that ASP.NET server controls must implement to handle postback events. Calling functions like Page.GetPostBackEventReference and Page.GetPostBackClientHyperlink allows ASP.Net render the JavaScript Function __doPostBack(), which submits the form with postback.</div> <div class="Section1">To see how to perform postbacks for custom events, please check the following sample (onmouseover triggers __doPostBack):</div>Firstly, define on mouseover event on the client side to do the JavaScript function __doPostBack which can do postback to Code Behind.
Code Behind:
<div class="Section1">In Page_Load, it allows to retrieve the doPostBack. </div> <div class="Section1"> </div> <div class="Section1">Page.GetPostBackClientHyperlink will register __doPostBack JavaScript function to the client to execute __doPostBack('ImageButton1','onmouseover') when the ‘onmouseover’ client side event of the ImageButton1 fires.</div> <div class="Section1"> </div> <div class="Section1">Related thread: http://forums.asp.net/t/1227950.aspx</div>1.6 How to create ASP.NET Server Side Table/Client Side Table dynamically? [Top]
ASP.NET Server Side Table has ‘runat="server"’, Client Side Table does not have ‘unat="server"’.
This post will show you how to create an ASP.NET server side Tables by using C#: http://forums.asp.net/t/1219390/2174565.aspx
This post will demonstrate how to create an ASP.NET Table by using C# and inserting controls to the Table dynamically: http://forums.asp.net/t/1213400.aspx.
This post is about creating Client Side Table by using JavaScript and inserting controls in the Table dynamically: http://forums.asp.net/t/1220376/2179390.aspx.
1.7 How can I encrypt a string such as passwords or QueryStrings? [Top]
DES encryption, MD5 and other approaches can achieve it:
<div class="Section1">Sample of DES encryption is as below.Related thread:
</div>http://forums.asp.net/p/1238171/2258386.aspx#2258386
1.8 How to check if the users are online or offline? [Top]
Many developers tend to use ‘Session_End’ event to mark a user as ‘Offline’, but this may not be a good idea, because there are no one-to-one mappings between an ASP.Net session and a user. For example if a user opens 2 separate instances of Internet Explorer (e.g. 2 separate IE processes) on his workstation, he can have 2 ASP.Net sessions open. The ‘Session_End’ event of one of the session does not necessarily mean that the user is offline, they can still be using the website.
In addition, ‘Session_End’ event is only available in the ‘InProc’ session mode. If you are trying to store session states in the State Server or SQL Server, ‘Session_End’ event, it will never fire.
I would suggest using the last visiting time as the ideal approach.
Membership helps build the user database and there is a convenient approach to get it via Membership.GetUser(string userName).IsOnline.
If you don’t use Membership, you can use the method below which uses the same principle in Membership:
Related thread: http://forums.asp.net/t/1204142.aspx
1.9 How to insert a special non-ASCII characters into a database? [Top]
When you try to insert a special non-ASCII characters into a database, such as ‘Poor’s’, you find that they can’t be inserted, by using “INSERT INTO table VALUES(1,’Poor’s’)”, only “Poora€?s” can be inserted.
<div class="Section1">You can insert the value as parameter into the database. </div>Using this approach also prevents potential SQL injection attacks.
Related thread: http://forums.asp.net/p/1241151/2270935.aspx#2270935
1.10 How to transfer data between pages? [Top]
There are several approaches to transfer data between pages:
Query Strings: You can transfer data between pages with query strings. The data will be displayed in the URL as parameters. Sometimes you want to encode the parameters, which can be done with HttpServerUtility.UrlEncode or HttpUtility.UrlEncode.
For example:
In page1, you can transfer the data to another page by redirecting.
In page2, you can use
QueryString["parameter"] to get the data from page1.
Cookies: Cookies can be stored on client side machines. If you do not set the cookie's expiration, the cookie will be created but will not be stored on the user's hard disk. Instead, the cookie will be maintained as part of the user's session information. When the user closes the browser or if the session times out, the cookie is discarded.
You can add cookies to the Response.Cookies collection in a couple of ways, the following example shows two methods:
You can get more details from this link: http://msdn.microsoft.com/en-us/library/aa289495.aspx .
Session Variables: A session is stored in the server memory and it is unique to a particular client.
For example:
Store the data into a session in Page1:
Retrieve the data in Page2:
Server.Transfer:
Server.Transfer sends all of the information that has been assembled for processing by one page to another page. The URL on IE won’t be changed.
For example:
In page1, you can use ‘transfer’ to send all of the information in page1 to page2.
Then you can use the below codes in Page_Load to retrieve the value of TextBox1 from page 1.
You can get more information from this link: http://msdn.microsoft.com/en-us/library/ms525800.aspx and ASP.NET State Management Overview.
Related thread: http://forums.asp.net/t/1208146.aspx
1.11 How to hide the URL parameters in the browser? [Top]
There are three ways to achieve it.
1. Using Server.Transfer
When you transfer URL via Server.Transfer("sample2.aspx?x=1&y=2"), in page2 you can retrieve these two variables via Request but it’ll only display the URL of the previous page which executed Server.Transfer.
Reference:
http://msdn2.microsoft.com/en-us/library/ms525800.aspx
http://support.microsoft.com/kb/219294
2. If you encrypt QueryString then the real string will not be displayed in URL:
Please check Question 1.7.
3. URL Rewriting:
You can get more information from this link: http://www.codeproject.com/KB/aspnet/urlrewriter.aspx.
Related thread: http://forums.asp.net/t/1204609.aspx
1.12 How to download files? [Top]
Please check the sample code below, the page ‘link.aspx’ lists all of the files available in a folder, when clicking the hyperlinks in ‘link.aspx’, the user can download the files via ‘downloading.aspx’.
<div class="Section1">link.aspx: </div> <div class="Section1"> <div class="Section1">downloading.aspx:</div> </div> <div class="Section1"></div>
<div class="Section1"> </div> <div class="Section1"> </div> <div class="Section1">Related thread: http://forums.asp.net/p/1204802/2109808.aspx#2109808</div>1.13 What are the differences between <%# %> and <%= %>? [Top]
The <%# %> is used for data binding where as <%= %> is used to output the result of an expression. The expression inside <%# %> will be executed only when you call the page's or control's DataBind method. The expression inside <%= %> will be executed and displayed when it appears on the page.
1.14 Why can’t I step into a DLL’s source code from ASP.NET application while debugging? [Top]
First of all, if you are using the ‘Release’ build of the modules, please try building the modules or application under ‘Debug’ build.
If the debugger cannot load the DLL's .PDB file, we also cannot use the source code. The PDB files are debug symbols that are generated with the modules (EXE/DLL). They hold the necessary information for source code level debugging. The debugger will not load a PDB that does not match the binary being debugged (For more information, see http://msdn.microsoft.com/en-us/library/ms241903.aspx. You can check if the debugger loads this PDB file by using the following steps:
1.15 During a debugging session, why isn’t the break point hit? [Top]
There are several reasons why this could happen:
1. Matching symbols cannot be found without symbols, breakpoints cannot be mapped from source code to machine code while it is being executed.
2. The source code you have opened inside Visual Studio 2005 does not match the build of the executable code you’re trying to debug. If you are aware of this but want the debugger to attempt to set the breakpoint anyway, you can do one of following:
2.1. Right click on the breakpoint and choose ’Location…’ Then check the ’Allow the source code to be different from the original version’ checkbox.
2.2. Turn off source code verification. Go to the menu Tools->Options->Debugging->General and uncheck ‘Require source files to exactly match the original version’.
3. The component or program you have set the breakpoint to, have not been loaded into the memory yet. When a DLL or component is loaded to your program, the debugger is notified and will attempt to ‘bind’ the breakpoint so that it can be hit.
1.16 How to speed up your ASP.NET pages? [Top]
You can improve your ASP.NET application performance using:
For more information, please refer to: Speed Up Your ASP.NET Pages and Developing High-Performance ASP.NET Applications.
1.17 What does the character ‘~’ mean in ASP.NET applications? [Top]
The character tilde (~) in the ASP.NET paths points to the root of the web application.
Web developers are familiar with using relative paths for all links, including hyperlinks, images and style sheets to be able to move around web pages collectively.
In ASP.NET when using User controls the relative paths can be difficult to use. The typical solution to this is to use web-root absolute paths instead here, such as ’~/UserControl1.ascx’, resulting in the hard-coded sub-directories that are common on ASP.NET sites.
The correct solution to this problem is to use app-relative paths instead, which ASP.NET nicely makes possible through the use of the tilde (~) prefix. Instead of <a href="/UC/Page.aspx">, use <a id="A1" href="~/Page.aspx" runat="server">. The same ~ notation works for images also, as long as you add runat="server". There is also a ResolveUrl method that allows you to use ~ in your own code, which is one possible way to get stylesheet paths app-relative.
For more information about ASP.NET Web Site Paths, please see:
http://msdn.microsoft.com/en-us/library/ms178116.aspx.
<div style="border-bottom: windowtext 1pt solid; border-left: medium none; padding-bottom: 1pt; padding-left: 0cm; padding-right: 0cm; border-top: medium none; border-right: medium none; padding-top: 0cm;" class="Section1" mce_style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 1pt; BORDER-LEFT: medium none; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid">
</div>2. Send mail
2.1 What are the differences between ‘System.Net.Mail’ and ‘System.Web.Mail’? [Top]
System.Web.Mail and System.Net.Mail are both built-in libraries provided by Microsoft in .NET framework. System.Web.Mail is supported by all versions of .Net Frameworks at the moment. However, System.Web.Mail is obsolete since .Net 2.0 could be removed from the class library in the future. In .Net 2.0, the new ‘System.Net.Mail’ namespace is intrdocued. It is recommend against ‘System.Web.Mail’, if you are developing applications targeting .Net 2.0 and later.
2.2 How to send email? [Top]
To send mail you can use ‘System.Net.Mail’.
Code-Behind:
<div class="Section1">Related thread:
</div>http://forums.asp.net/t/1207868.aspx
2.3 Why SMTP Mails does not work when I try to send? [Top]
- <div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">If
you are using the System.Web.Mail namespace in .Net 2.x or a later edition, you can try using System.Net.Mail instead.</div>
- <div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">Please ensure that the ’From Email Address’, password,
’To Email Address’, SMTP server and port of the server are correct. </div>
- <div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">Please
check the SMTP configuration and start Default SMTP Virtual Server by configuring IIS:
http://forums.asp.net/t/1214374.aspx.</div>
- <div style="text-indent: 0cm; margin-left: 0cm;" class="Section1" mce_style="MARGIN-LEFT: 0cm; TEXT-INDENT: 0cm">Yahoo
and Hotmail don't support SMTP: http://forums.asp.net/t/1212160/2144153.aspx.</div>
<div style="border-bottom: windowtext 1pt solid; border-left: medium none; padding-bottom: 1pt; padding-left: 0cm; padding-right: 0cm; border-top: medium none; border-right: medium none; padding-top: 0cm;" class="Section1" mce_style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 1pt; BORDER-LEFT: medium none; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid">
</div>3.GridView
3.1 How to get the index of a row which contains the clicked control in GridView? [Top]
It can be implemented by using property CommandName and Event RowCommand of GridView control.
To get the index of row which contains the clicking control in GridView, you can try the method below.
1. Define property CommandName in ImageButton: CommandName="display"
2. In event RowDataBound, pass the row index to ImageButton CommandArgument.
3. Use the RowCommand event to execute the click event of ImageButton, then you can get the index of clicking ImageButton.
Related thread: http://forums.asp.net/t/1222400.aspx
<div style="border-bottom: windowtext 1pt solid; border-left: medium none; padding-bottom: 1pt; padding-left: 0cm; padding-right: 0cm; border-top: medium none; border-right: medium none; padding-top: 0cm;" class="Section1" mce_style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 1pt; BORDER-LEFT: medium none; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid">
</div>4. Membership
4.1 How to deploy the custom Membership Database? [Top]
Please use ‘aspnet_regsql.exe’ to deploy the membership database and set the membership provider in ‘Web.Config’. You can learn about how to implement membership in ASP.NET 2.0:
http://www.asp.net/learn/moving-to-asp.net-2.0/module-08.aspx. You can also find detailed steps about implementing a custom membership user at How to: Implement a Custom Membership User.
4.2 How to customize password generation? [Top]
Membership.GeneratePassword(length,numberOfNonAlphanumericCharacters)is the method for generating a password. If you don’t use Membership in ASP.NET, you can use the sample codes below.
Related threads:
http://forums.asp.net/t/1201826.aspx
http://forums.asp.net/t/1202361.aspx
<div style="border-bottom: windowtext 1pt solid; border-left: medium none; padding-bottom: 1pt; padding-left: 0cm; padding-right: 0cm; border-top: medium none; border-right: medium none; padding-top: 0cm;" class="Section1" mce_style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 1pt; BORDER-LEFT: medium none; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid">
</div>5. JavaScript
5.1 How to update the data on another page? [Top]
How do you change the data on a page and update the related data on another page?
For example, there are two pages. One is the main page and the other is the popup page. Popup page is opened by main page via 'window.open()'.
Main page:
Popup.aspx:
Related thread: http://forums.asp.net/t/1200262.aspx
5.2 Refresh the page in another page. [Top]
In the sample below, after the user closes the child page, the parent page will be refreshed.
Parent page: (use window.open to open the popup page)
Popup.aspx: (After clicking the Button1, it will submit the change and refresh parent page.)
Related thread:
http://forums.asp.net/t/1248402.aspx
5.3 How to pass values between Code Behind and client side JavaScript? [Top]
Passing values from Code Behind to client side JavaScript:
You can use <%=variable %> in JavaScript code body to transfer the value of the server side variable in Code Behind. Note that member field must be non-private in Code Behind, if it will be included in the <% %> block.
For example, you can use JavaScript code: alert("<%=DateTime.Now.ToString() %>"); to get the server time. Another approach is using ScriptManager to register a JavaScript code in Code Behind:
Passing values from client side JavaScript to Code Behind:
You can use Hidden Control to pass the value.
HTML:
Code Behind:
You can retrieve the value from Hidden control in Code Behind after storing the variable into Hidden control by using JavaScript. In the above sample, 'OnClientClick="clientButtonClick()"' will be executed before the server side click event (“onclick="Button1_Click"”).
Related thread: http://forums.asp.net/p/1211504/2139016.aspx#2139016
5.4 How to display dynamic ToolTips for controls? [Top]
Some controls have the property ‘ToolTip’ which can display the details when mouse cursor moves over.
However, when you want to display dynamic tooltips for every cell in GridView or display dynamic tooltips of every specific day in Calendar, the property ‘ToolTip’ is not sufficient.
In order to implement the dynamic tooltips for the controls, you can use Control.Attributes.Add and tooltips JavaScript function to deploy respective tooltip for every control.
Display the dynamical tooltips of every cell in GridView:
</div>Please download wz_tooltip.zip from the below link:
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.