Aug 16

Ny using HTML/Javascript
<meta http-equiv=”refresh” content=”10″>

Using .NET
Response.AppendHeader(”Refresh”, “10;URL=Nextpage.aspx”);

Aug 16

private void Button1_Click(object sender, System.EventArgs e)
    {
        Control myForm = Page.FindControl("Form1");

        foreach (Control ctl in myForm.Controls)
        {
            if (ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
            {
                ((TextBox)ctl).Text = "";
            }
        }
    }


This will clear EVERYTHING from the textboxes - even if you had them pre-populated
with data. A simple way to just reset it to the condition at Page_Load time, just
do this in the Reset SubRoutine: Server.Transfer(”YourPageName.aspx”)

Aug 16
private void Page_Load(object sender, System.EventArgs e)
{
  if(!Page.IsPostBack)
  {
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.Cache.SetAllowResponseInBrowserHistory(false);
  }
}
Aug 16
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidateCheckBoxList.aspx.cs"
   Inherits="ValidateCheckBoxList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <asp:CheckBoxList ID="CheckBoxList1" runat="server">
               <asp:ListItem Text="a" Value="b"></asp:ListItem>
               <asp:ListItem Text="a" Value="b"></asp:ListItem>
           </asp:CheckBoxList>
           <asp:CustomValidator ID="CustomValidator1" runat="server"
 ErrorMessage="CustomValidator"
               OnServerValidate="CustomValidator1_ServerValidate">
           </asp:CustomValidator>
           <asp:Button ID="Button1" runat="server" Text="Validate" /></div>
   </form>
</body>
</html>


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 ValidateCheckBoxList : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {

   }
   protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
   {
       int i;
       i = 0;

       foreach (ListItem item in CheckBoxList1.Items)
       {
           if (item.Selected)
           {
               i = i + 1;

           }
       }
       if (i > 1)
       {
           args.IsValid = false;
           CustomValidator1.ErrorMessage = "more than one item is selected!";
       }
       else
       {
           args.IsValid = true;
       }

   }
}

Aug 16

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”

Aug 16

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);

}

Aug 16

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” />

Aug 16

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

Aug 16

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.

Aug 16

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