How To Prevents users to go back to the previous pages

Sometimes we are in a situation that we don't want the user to visit the previous pages. This can be a scenario when the user logs out but uses the back button to navigate to the pages. In this article I will show you some simple ways you can use to prevent user from going back.

Using History(+1) Method:

Let's say that you have a page called Default.aspx which is the secure page and when you click the button you are redirected to another page Default2.aspx. Now from Default2.aspx. you don't want to go back to the Default.aspx.. You can achieve this easily using few lines of code.

Let's first catch the button click event and send the user to the Default2.aspx page.

protected void Button1_Click(object sender, EventArgs e)

{

Response.Redirect("Default2.aspx");

}

Now if you press the button you will be redirected to the Default2.aspx page. If you press the back button you will be taken to the Default.aspx page. Now we will implement the functionality that will prevent the user to go to the Default2.aspx page.

In the Default.aspx html write the following code:

<body onLoad="if(history.length>0)history.go(+1)">
That's it. It means every time you will go to the Default.aspx page you will be forwarded to the Default2.aspx

How To Fill Dropdownlist With XML in ASP.NET

In this article we will see that how we can populate a DropDownList control with XML file as a source.Our XML file looks something like below which simply contains the name of the clients.
<?xml version="1.0" encoding="utf-8" ?>
<names>
 <name>
   <client>hank</client>
 </name>
 <name>
   <client>corry</client>
 </name>
 <name>
   <client>david</client>
 </name>
 <name>
   <client>james</client>
 </name>
</names>
Filling a DropDownList with XML file: Now we want to fill the DropDownList with the contents contained in the XML file. Don't forget to include the namespace System.XML.
private void FillDropDownList()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("Menu.xml"));
        XmlNodeList nodeList = doc.SelectNodes("names/name");
        foreach (XmlNode node in nodeList)
            DropdownList1.Items.Add(new ListItem(node.SelectSingleNode("client").InnerText));
    }
All we are doing is making an object of the XmlDocument class. Than we read the XML file, dig down in the nodes and selects the nodes that we want. And finally add those node's inner text to the DropDownList items.

How To Change The color of GridView Row Color by Using Simple Mouse Click

In this article I will demonstrate that how you can change the color of the GridView row by using simple mouse click and change it back to its original color by clicking the row twice. Take a look at the RowCreated event below to have a better idea: protected void MyGridView_RowCreated(object sender, GridViewRowEventArgs e) { string rowID = String.Empty; if (e.Row.RowType == DataControlRowType.DataRow) { rowID = "row"+e.Row.RowIndex; e.Row.Attributes.Add("id","row"+e.Row.RowIndex); e.Row.Attributes.Add("onclick","ChangeRowColor(" +"'" + rowID + "'" + ")"); } } JavaScript Function: <script language ="javascript" type="text/javascript"> document.body.style.cursor = 'pointer'; var oldColor = ''; function ChangeRowColor(rowID) { var color = document.getElementById(rowID).style.backgroundColor; if(color != 'yellow') oldColor = color; if(color == 'yellow') document.getElementById(rowID).style.backgroundColor = oldColor; else document.getElementById(rowID).style.backgroundColor = 'yellow'; } </script> The ChangeRowColor function takes in the id of the row and finds the color of the row. Then it checks that if the color is 'yellow' which, is the color of the highlight row. If it is not yellow then it simply assigns the color to the public variable oldColor which is used to save the previous color of the GridView row. If the color is yellow then we get the previous color and assign to the row so that it can come back to its original state.

What is Diffrence between Src and Codebehind

You may have noticed that Visual Studio.NET adds an interesting keyword to the Page directive of an ASP.NET web form: CodeBehind. However, you will not find this documented in the .NET documentation for the Page directive. Visual Studio.NET implements CodeBehind by precompiling the base class, then uses the Inherits keyword to refer to that class. The CodeBehind keyword allows the IDE to remember the file containing your base class. However, what if you do not want to precompile your class into an assembly?

According to the .NET documentation, the Src attribute of the Page directive:

Specifies the source file name of the code-behind class to dynamically compile when the page is requested. Note: RAD designers, such as Visual Studio.NET, do not use this attribute. Instead, they precompile code-behind classes and then use the Inherits attribute.

So, by using the Src attribute to indicate the file, and the Inherits keyword to indicate the class, you can accomplish Codebehind. Here is an example Web Form Inheriting from the HelloWorld class in the HelloWorld.cs file:

<%@ Page Language="C#" Inherits="DotNetCoders.HelloWorld" Src="HelloWorld.cs" trace="true" %> <html> <body> <asp:Label id="ProgrammersMessage" runat="server" /> </body> </html>
As you can see, the differences in the page directives are as follows:

Visual Studio.NET

<%@ Page language="C#" Inherits="DotNetCoders.HelloWorld" Codebehind="HelloWorld.aspx.cs" %>
Inherits keyword

How To Generate Random Image Using HttpHandler in ASP.NET

How to construct an image generator used to create a visual security code that helps prevent automated sign ups in your web applications.The text generated is stored in Session to be used for comparison to the user's input.
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Web.SessionState;

public class Handler : IHttpHandler, IRequiresSessionState
{

   public void ProcessRequest(HttpContext context)
   {
       context.Response.ContentType = "image/gif";
       Bitmap b = new Bitmap(200, 60);
       Graphics g = Graphics.FromImage(b);
       g.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);
       Font font = new Font(FontFamily.GenericSansSerif, 48, FontStyle.Bold,
 GraphicsUnit.Pixel);
       Random r = new Random();
       string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       string letter;
       StringBuilder sb = new StringBuilder();
       for (int x = 0; x < 5; x++)
       {


           letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
           sb.Append(letter);
           g.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));
       }
       Pen linepen = new Pen(new SolidBrush(Color.Black), 2);
       for (int x = 0; x < 6; x++)
           g.DrawLine(linepen, new Point(r.Next(0, 199), r.Next(0, 59)),
 new Point(r.Next(0, 199), r.Next(0, 59)));
       b.Save(context.Response.OutputStream, ImageFormat.Gif);
       context.Session["image"] = sb;
       context.Response.End();



   }

   public bool IsReusable
   {
       get
       {
           return true;
       }
   }

}
How To Use <img src="Handler.ashx" alt="Random Image" />

How To Create Text2Speech Application in ASP.NET 2.0

1. Open new C# Web Application.
2. Add the Reference Microsoft object speech application
(SAPI is a COM component that needs to be referenced in the project.)
3. Add the namespace : using SpeechLib; 

SpVoice is the class that is used for text to speech conversion. The speak method takes in a string that needs to be spoken along with a set of flags. The flag that I have used, which is SVSFlagsAsync tells the TTS engine that the conversion of the text to speech needs to be done in a Asynchronous mode. The control quickly returns after this call. protected void Button1_Click(object sender, EventArgs e) { SpVoice voice = new SpVoice(); voice.Speak(TextBox1.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync); }

How To 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"