<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<html>
<head>
<title>Custom Alert Box</title>
<meta equiv="Content-Type" content="text/html;
charset=utf-8">
<script type="text/javascript" language="javascript">
document.onclick=disableEvents
document.oncontextmenu=disableEvents
var ef=0
function custAlert()
{
ef=1
var ids
ids=document.getElementById('alert')
ids.style.left=300;
ids.style.top=150;
ids.style.visibility="visible"
}
function disableEvents()
{if(ef)
return false;
} </script>
</head>
<body bgcolor="#ffffff" onload="custAlert();"> <form id="Form1" method="post" runat="server">
<div align="center" id="alert" style="border: 2px solid gray; background: #cdeb8b
url('images/img3.gif') repeat-x; left: 240; visibility: hidden; width: 349; color: red; position: absolute;
top: 104; height: 108">
<br> <h3 style="left: 0; width: 152; position: absolute; top: 0; height: 40; ali
gn: Left">
<span style="color:#356AA0;">Custom Alert Box</span></h3>
<img style="left: 336px; width: 6px; position: absolute; top: 8px; height: 6px" height="6"
alt="Close" src="images/Cross.png" onclick="ef=0;document.getElementById('alert').style.visibili
ty='hidden';" >
<br> <hr width="99.03%" size="1" style="left: 0px; width: 99.03%; position: absolute;
top: 32px; height: 1px">
<div id="msg" style="width: 349; color: #356aa0; height: 85; text-decoration: none">
NJoy Programming<p>
Success be Yours</div>
<p>
</div>
</form>
</body>
</html>
My Bookshelf
Technology Towards Microsoft Headlines
Wednesday, September 24, 2008
Custom Alert box using JavaScript
Sunday, September 21, 2008
Garbage Collector
Friday, September 12, 2008
ASP.NET Word document to text file Conversion
ASP.NET Word document to text file Conversion
Environment: Windows Server 2003ASP.NET 1.1, Office 2003
Recently I was looking for a solution to convert a word document to text file. I found code at some locations:
http://www.codeproject.com/aspnet/wordapplication.asp
http://msdn2.microsoft.com/en-us/library/tcyt0y1f(VS.80).aspx
While implementing the code I got following two errors:
Error 1. System.UnauthorizedAccessException: Access is denied.
It was occuring at below line : Word.ApplicationClass oWord = new Word.ApplicationClass();
My Research:
The problem was because of IIS Server not having appropriate rights to launch a Microsoft Word-document DCOM object.
Below solution worked for me:
a. start the tool "DCOMCNFG.EXE" (Component Services).
b. Go to Console Root --> Component Services --> Computers --> My Computer --> DCOM Config --> Microsoft Word Document --> Properties --> Security --> Launch Permissions --> edit. Add the appropriate user/group, like "NT AUTHORITY \ NETWORK SERVICE"
Error 2. Command Failed.
This error was coming at the below line:
Word.Document aDoc = vk_word_app.Documents.Open(ref fileName, ref missing, ref vk_read_only,ref missing, ref missing,ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref vk_visible, ref missing, ref missing, ref missing,ref missing );
My Research:
The solution worked for me just by adding the below lines in the web.config file.
The same code works fine in a windows application as we have rights to open the word document there but while opening the document on web server (asp.net) the user account should be specified to open the account.
Tuesday, September 9, 2008
Gridview Tips & Tricks
Pop-up a Confirmation box before Deleting a row in GridView.
Add a template field and drop a button in it, using which the user will delete the record. In the OnClientClick event, call the confirm() function as mentioned below:
CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete the record?');" />
Display details of the Row selected in the GridView.
Assuming you have a button called ‘Select’ in your GridView with CommandName ‘Select’, to find out the row clicked and display the row’s details, use this code:
private void GridView1_RowCommand(Object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int idx = Convert.ToInt32(e.CommandArgument);
GridViewRow selrow = GridView1.Rows[idx];
string fstCell = selrow.Cells[0].Text;string scndCell = selrow.Cells[1].Text;
// and so on
// Thanks to Mark Rae (MVP) for pointing the typo. Earlier it was Cells[1] and Cells [2]
}
}
Retrieve Details of the Row being Modified in GridView.
void GridView1_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
{
// Retrieve the row being edited.
int index = GridView1.EditIndex;
GridViewRow row = GridView1.Rows[index];
// Retrieve the value of the first cell
lblMsg.Text = "Updated record " + row.Cells[1].Text;
}
Retrieve Details of the Row being Deleted in GridView.
The ID of the row being deleted must be in the GridView.DataKeyNames collection.
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = (int)GridView1.DataKeys[e.RowIndex].Value;
// Query the database and get the values based on the ID
}
Cancelling Update and Delete in a GridView.
RowUpdating - Occurs when a row's Update button is clicked, but before the GridView control updates the row.
RowDeleting – Occurs when a row's Delete button is clicked, but before the GridView control deletes the row.
protected void gvDetail_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
e.Cancel = true;
}
void GridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
// Check for a condition and cancel the delete
// There should be atleast one row left in the GridView
if (GridView1.Rows.Count <= 1)
{
e.Cancel = true;
}
}
Enable Disable Controls inside a GridView.
There are at times when you have to disable controls on some rows, when a certain condition is satisfied. In this snippet, we will see how to disable editing for rows that have the CategoryName as ‘Confections’. Use the following code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{
Label lblControl = (Label)e.Row.Cells[2].FindControl("lblCategoryName");
if(lblControl.Text == "Confections")
{
e.Row.Cells[0].Enabled = false;
}
}
}
}
How to Add a Row Number to the Gridview
There are a couple of ways to do this. However I will share a very handy tip that was shared by XIII in the asp.net forums. From the following link:
http://forums.asp.net/p/992655/1292440.aspx#1292440
Just add the following tags to your section of your GridView
How to programmatically hide a column in the GridView?
There are two conditions to be checked in the Page_Load to hide a columns in the GridView, let us say the 3rd column:
If ‘AutoGenerateColumns’ = True on the GridView
C#
GridView1.HeaderRow.Cells[2].Visible = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
gvr.Cells[2].Visible = false;
}
VB.NET
GridView1.HeaderRow.Cells(2).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(2).Visible = False
Next gvr
If ‘AutoGenerateColumns’ = False on the GridView
C#
GridView1.Columns[2].Visible = false;
VB.NET
GridView1.Columns(2).Visible = False
Displaying Empty Data in a GridView?
When there are no results returned from the GridView control’s data source, the short and simple way of displaying a message to the user, is to use the GridView’s EmptyDataText property.
DataSourceID="SqlDataSource1" EmptyDataText="No data available"
ShowFooter="true" AllowPaging="True" AllowSorting="True"
PageSize="5" OnRowDataBound="GridView1_RowDataBound">
Note: You can also add style to the EmptyDataText by using the EmptyDataRowStyle property.
if you konw more about EmptyDataRowStyle the following link will be helpful to you:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatarowstyle.aspx
Displaying an Image in case of Empty Data in a GridView?
As an alternative to using the EmptyDataText property, if you need to display an image or any HTML/ASP.NET control, you can use the EmptyDataTemplate. In this snippet below, we are using the image control in the to display an image.
DataSourceID="SqlDataSource1" ShowFooter="true" AllowPaging="True" AllowSorting="True"PageSize="5" OnRowDataBound="GridView1_RowDataBound">
ImageUrl="~/images/NoDataFound.jpg"AlternateText="No data found"
runat="server"/>
Change the color of a GridView Row based on some condition.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
string catName = Convert.ToString(drv["CategoryName"]);
if (catName.Trim() == "Confections")
e.Row.BackColor = System.Drawing.Color.LightBlue;
}
}
How to create an Image Command Field Column and add to the GridView at runtime?
if (!Page.IsPostBack)
{
CommandField cmdField = new CommandField();
cmdField.ButtonType = ButtonType.Image;
cmdField.SelectImageUrl = "~/Images/Home_Np1.GIF";
cmdField.ShowSelectButton = true;
cmdField.HeaderText = "Select";
GridView1.Columns.Add(cmdField);
GridView1.DataBind();
}
How to loop through all the rows in all the pages of a GridView?
One simple way to loop through all the rows in all the pages of a GridView is to access its DataSource. In this example, we will loop through the SQLDataSource to retrieve all the rows in a GridView and access its cell value. You can modify the logic depending on the type of controls you have added to the GridView.
C#
protected void Button1_Click(object sender, EventArgs e)
{
DataSourceSelectArguments dsaArgs = new DataSourceSelectArguments();
DataView view = (DataView)SqlDataSource1.Select(dsaArgs);
DataTable dt = view.ToTable();
for (int i = 0; i <>
{
for (int j = 0; j <>
{
string s = dt.Rows[i][j].ToString();
}
}
}
How to send an E-mail from ASP.NET on Windows XP OS?
Now a days many web application has a requirement to send email. Using ASP.Net we can send emails, but when ever we are developing in WindowsXP environment, It doesnt allows us to send the email. It need some configuration so that we can send Email in WindowsXP with ASP.Net. Here are Steps for configuration.
Step 1: Open IIS, Stop the "Default SMTP Virtual Server" here is screen shot
Step 2: Open Properties of "Default SMTP Virtual Server" in that one, Select "Access" tab.
Step 3: click on "Relay" button. It will open a "Relay Restriction" window. In this window select a radio button with text as "Only the list below".
Step 4: Then click on "Add" button. which allow to enter an IP Address. In that enter local IP Address as 127.0.0.1. Then click "ok" button to close Access tab and propertie window. here is the screen shot:
Tuesday, August 26, 2008
OVERVIEW OF ASP.NET

tags. They contain the runat=server attribute, which tells ASP.NET that these controls can be accessed on the server and on the client. Optionally you can specify the language for the block. The code block itself consists of the definition of member variables and methods.
Render blocks contain inline code or inline expressions enclosed by the character sequences shown here. The language used inside those blocks could be specified through a directive like the one shown before.
You can declare several standard HTML elements as HTML server controls. Use the element as you are familiar with in HTML and add the attribute runat=server. This causes the HTML element to be treated as a server control. It is now programmatically accessible by using a unique ID. HTML server controls must reside within a section that also has the attribute runat=server.
There are two different kinds of custom controls. On the one hand there are the controls that ship with .NET, and on the other hand you can create your own custom controls. Using custom server controls is the best way to encapsulate common programmatic functionality.
Just specify elements as you did with HTML elements, but add a tag prefix, which is an alias for the fully qualified namespace of the control. Again you must include the runat=server attribute. If you want to get programmatic access to the control, just add an Id attribute.
You can include properties for each server control to characterize its behavior. For example, you can set the maximum length of a TextBox. Those properties might have sub properties; you know this principle from HTML. Now you have the ability to specify, for example, the size and type of the font you use (font-size and font-type).
The last attribute is dedicated to event binding. This can be used to bind the control to a specific event. If you implement your own method MyClick(), this method will be executed when the corresponding button is clicked if you use the server control event binding shown in the slide.
You can create bindings between server controls and data sources. The data binding expression is enclosed by the character sequences . The data-binding model provided by ASP.NET is hierarchical. That means you can create bindings between server control properties and superior data sources.
If you need to create an instance of an object on the server, use server-side object tags. When the page is compiled, an instance of the specified object is created. To specify the object use the identifier attribute. You can declare (and instantiate) .NET objects using class as the identifier, and COM objects using either progidor classid.
With server-side include directives you can include raw contents of a file anywhere in your ASP.NET file. Specify the type of the path to filename with the pathtype attribute. Use either File, when specifying a relative path, or Virtual, when using a full virtual path.
To prevent server code from executing, use these character sequences to comment it out. You can comment out full blocks—not just single lines.
First ASP.NET Program.
Now let us have our First ASP.NET program.
Let’s look at both the markup and the C# portions of a simple web forms application that generates a movie line-up dynamically through software. Markup Portion :Web form application part 1 -- SimpleWebForm.aspx
Supermegacineplexadrome! Welcome to Supermegacineplexadrome! Showtimes for
And this is where the C# part of a web forms application comes in.
Web form application part 2 -- SimpleWebForm.cs
using System;using System.Web.UI;using System.Web.UI.WebControls;public class MoviePage:Page{ protected void WriteDate(){ this.Response.Write(DateTime.Now.ToString()); } protected void WriteMovies(){ this.Response.Write( "The Glass Ghost (R) 1:05 pm, 3:25 pm, 7:00 pm"); this.Response.Write( "Untamed Harmony (PG-13) 12:50 pm, 3:25 pm, 6:55 pm"); this.Response.Write( "Forever Nowhere (PG) 3:30 pm, 8:35 pm"); this.Response.Write( "Without Justice (R) 12:45 pm, 6:45 pm"); }}
Execution Cycle :
Now let's see what’s happening on the server side. You will shortly understand how server controls fit in.
A request for an .aspx file causes the ASP.NET runtime to parse the file for code that can be compiled. It then generates a page class that instantiates and populates a tree of server control instances. This page class represents the ASP.NET page.
Now an execution sequence is started in which, for example, the ASP.NET page walks its entire list of controls, asking each one to render itself.
The controls paint themselves to the page. This means they make themselves visible by generating HTML output to the browser client.
We need to have a look at what’s happening to your code in ASP.NET.
Compilation, when page is requested the first time
The first time a page is requested, the code is compiled. Compiling code in .NET means that a compiler in a first step emits Microsoft intermediate language (MSIL) and produces metadata—if you compile your source code to managed code. In a following step MSIL has to be converted to native code.
Microsoft intermediate language is code in an assembly language–like style. It is CPU independent and therefore can be efficiently converted to native code.
The conversion in turn can be CPU-specific and optimized. The intermediate language provides a hardware abstraction layer.
MSIL is executed by the common language runtime.
The common language runtime contains just-in-time (JIT) compilers to convert the MSIL into native code. This is done on the same computer architecture that the code should run on.
The runtime manages the code when it is compiled into MSIL—the code is therefore called managed code.
Like ASP, ASP.NET encapsulates its entities within a web application. A web application is an abstract term for all the resources available within the confines of an IIS virtual directory. For example, a web application may consist of one or more ASP.NET pages, assemblies, web services configuration files, graphics, and more. In this section we explore two fundamental components of a web application, namely global application files (Global.asax) and configuration files (Web.config).
Global.asax is a file used to declare application-level events and objects. Global.asax is the ASP.NET extension of the ASP Global.asa file. Code to handle application events (such as the start and end of an application) reside in Global.asax. Such event code cannot reside in the ASP.NET page or web service code itself, since during the start or end of the application, its code has not yet been loaded (or unloaded). Global.asax is also used to declare data that is available across different application requests or across different browser sessions. This process is known as application and session state management.
The Global.asax file must reside in the IIS virtual root. Remember that a virtual root can be thought of as the container of a web application. Events and state specified in the global file are then applied to all resources housed within the web application. If, for example, Global.asax defines a state application variable, all .aspx files within the virtual root will be able to access the variable.
Application directives are placed at the top of the Global.asax file and provide information used to compile the global file. Three application directives are defined, namely Application, Assembly, and Import. Each directive is applied with the following syntax:
Web.config :
In ASP, configuration settings for an application (such as session state) are stored in the IIS metabase. There are two major disadvantages with this scheme. First, settings are not stored in a human-readable manner but in a proprietary, binary format. Second, the settings are not easily ported from one host machine to another.(It is difficult to transfer information from an IIS’s metabase or Windows Registry to another machine, even if it has the same version of Windows.)
Web.config solves both of the aforementioned issues by storing configuration information as XML. Unlike Registry or metabase entries, XML documents are human-readable and can be modified with any text editor. Second, XML files are far more portable, involving a simple file transfer to switch machines.
Unlike Global.asax, Web.config can reside in any directory, which may or may not be a virtual root. The Web.config settings are then applied to all resources accessed within that directory, as well as its subdirectories. One consequence is that an IIS instance may have many web.config files. Attributes are applied in a hierarchical fashion. In other words, the web.config file at the lowest level directory is used.
Since Web.config is based on XML, it is extensible and flexible for a wide variety of applications. It is important, however, to note that the Web.config file is optional. A default Web.config file, used by all ASP.NET application resources, can be found on the local machine at:\%winroot%\Microsoft.Net\Framework\version\CONFIG\machine.config
Summary :
ASP.NET is an evolution of Microsoft’s Active Server Page (ASP) technology. Using ASP.NET, you can rapidly develop highly advanced web applications based on the .NET framework. Visual Studio Web Form Designer, which allows the design of web applications in an intuitive, graphical method similar to Visual Basic 6. ASP.NET ships with web controls wrapping each of the standard HTML controls, in addition to several controls specific to .NET. One such example is validation controls, which intuitively validate user input without the need for extensive client-side script.
In many respects, ASP.NET provides major improvements over ASP, and can definitely be considered a viable alternative for rapidly developing web-based applications.