Redirection after some time delay in asp.net using c#

In this tutorial you will learn how to redirect user after some time delay in asp.net using c#. Normally people use redirection process after showing some message to their users, for example a message can be "Thank you for visiting our website" or after getting feedback from user you can display a message "Thank you for providing us your valuable feedback" and then redirect user to another page. It's just up to you in which scenario you want to use it. In asp.net normally we have three options to redirect user after some time delay.
1) Use javascript code in your c# code
2) Use Meta tag in your c# code
3) Use Response.AddHeader().

Redirection after some time delay in asp.net using c#

Let's have a look over these methods

1) Use javascript function in your c# code.
I have already written a post related how to write and execute the javascript code from c# code behind. You just have to put your javascript code in c# code behind and that's it. Let’s have a look over example given below

Yourpage1.aspx

<asp:Button ID="btnRedirect" runat="server" Text="Redirect" OnClick="btnRedirect_Click" />
In my .aspx page I have an asp:button control and using it’s onClick event I am redirecting user.

Yourpage1.aspx.cs
protected void btnRedirect_Click(object sender, EventArgs e)
{
string redirectionScript = "<script language='javascript'>" +
"function Delayer(){"+
"setTimeout('Redirection()', 5000);"+
"}"+
"function Redirection(){" +
"window.location = 'yourpage2.aspx';" +
"}" +
"Delayer()" +
"</script>";
Page.RegisterStartupScript("Startup", redirectionScript);
} 
It will redirect user to yourpage2.aspx after 5 seconds
 

2) Use meta tag in your c# code
I have already written a post for Implementing meta tags with master page in asp.net using c#. Let’s have a look over how to redirect user after some time delay by using meta tag in c# code behind.
protected void btnRedirect_Click(object sender, EventArgs e)
{
HtmlMeta equiv = new HtmlMeta();
equiv.HttpEquiv = "refresh";
equiv.Content = "5; url= yourpage2.aspx";
Header.Controls.Add(equiv);
}
OR
protected void btnRedirect_Click(object sender, EventArgs e)
{
Page.Header.Controls.Add(new LiteralControl("<meta http-equiv='refresh' content='5; url=yourpage2.aspx'/>"));
} 

It will redirect user to yourpage2.aspx after 5 seconds.

3) Use Response.AddHeader()


Use c# built-in method AddHeader() of Response Class to redirect user after some time delay. Let's have a look over how to do so

protected void btnRedirect_Click(object sender, EventArgs e)
{
Response.AddHeader("REFRESH", "5;URL=yourpage2.aspx");
}

It will redirect user to yourpage2.aspx after 5 seconds.


This AddHeader()
function takes two Parameters. First one is HeaderName which is a string indicating the name of the new header. The second parameter is HeaderValue which is a string that indicates the initial value of the new header. It doesn't return any value.

So that's it.



Happy Coding
Read more...

How to restrict special characters being entered in textbox using javascript?

In this tutorial you will learn how to restrict the special characters being entered in textbox. Sometimes you want to restrict some special characters being entered in textbox of form to prevent the sql injection that can harm your database very badly. So Let's have a look over how to do so.


How to restrict special characters being entered in textbox using javascript?


<script language="javascript" type="text/javascript">
function check(e) {
var keynum
var keychar
var numcheck
// For Internet Explorer
if (window.event)
{
keynum = e.keyCode
}
// For Netscape/Firefox/Opera
else if (e.which)
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
//List of special characters you want to restrict
if (keychar == "'" || keychar == "`")
{

return false;
}
else {
return true;
}
}
</script>

Now let's have a look over how to call this special characters restriction function in a textbox.

For aspx server side textbox control
<asp:TextBox ID="txtName" runat="server" onkeypress="return check(event)" ></asp:TextBox>

For html textbox control
<input type="text" name="txtName" id="txtName" onkeypress="return check(event)">

You can restrict any other character or number other than special characters too, just by simply add it in the list of characters to be restricted such as for example I want to restrict the character a too then the code will be
if (keychar == "'" || keychar == "`" || keychar == "a")

So this is the way to handle and restrict the special characters being entered in textbox using javascript.

Keep Coding…

Read more...

Learn how to write and execute the javascript code from c# code behind

In this tutorial you will learn how to write and execute the javascript code from c# code behind. Sometimes you may want to execute the javascript code directly from c# code behind. For example, you want to do so when a condition matches in your c# code like this
string name;
string std_name;

if(name==std_name)
{
//Now here you want to execute your javascript code
}

Learn how to write and execute the javascript code from c# code behind


In my .aspx page I am using a <span id="showMessage">and <asp:button>. When you will click the <asp:button>, focus will go to the server side then there I have written my code that will alert a message and display my name in the <span id="showMessage">. Let’s have a look over how to do so
protected void submit_Click(object sender, EventArgs e)
{

string popupScript = "<script language='javascript'>" +
"function GetAlert(){" +
"document.getElementById('showMessage').innerHTML = 'Adeel Fakhar';" +
"alert('Function is being called');" +
"}" +
"GetAlert()" +
"</script>";
Page.RegisterStartupScript("Startup", popupScript);
}

Page.RegisterStartupScript is used for registering and executing the client side script. This method takes two parameters. First one is key of type System.String and it acts as a unique key that identifies a script block. Second parameter is script which type is also System.String, it is content of script that will be sent to the client, means it contains your javascript code.

Page.RegisterStartupScript automatically attach the script just before the closing tag of your aspx page object’s <form runat="server">
element.

Page.RegisterStartupScript method uses a unique key to identify the script block; the script block does not have to be emitted to the output stream every time it is requested by a different server control instance.

Any script block with the same key parameter values is considered duplicate. You must remember to include HTML Comment Tags around your script so that it will not be rendered if the requesting browser does not support javascript.

So this is the way to write and execute the javascript code from c# code behind.
Read more...

How to fire the client side onclick() event of asp radio button?

In this tutorial you will learn how to fire the client side onclick() event of asp radio button. Using same technique you can fire the onclick() event of asp textbox, asp button, etc too. It is not a big deal. Let's have a look over how to do so.

How to fire the client side onclick() event of asp radio button?


The method to do this is very simple, you just have to write the
onclick="yourjavascriptfunction();"
and that’s it.

One thing remember that the intelliSense of visual studio will not show you the onClick() event in the list of available events for asp radio button when you will press the spacebar but you have to write this yourself in order to execute your javascript code using client side onclick() event of asp radio button because when your aspx page is rendered then asp radio button will become input radio button.

Now let's have a look over the complete example of the topic that we are discussing.

Home.aspx

<script language="javascript" type="text/javascript">
function ShowAlert()
{

alert("client side onClick event of asp radio button has been fired");

}
</script>
<table cellpadding="0" cellspacing="2" border="0" width="100%">
<tr>
<td width="28%">First Name:</td><td><asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Last Name:</td><td><asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Do you want to join us?</td><td><asp:RadioButton ID="rdoYes" runat="server" GroupName="Group1"
Text="Yes" onclick="ShowAlert();"/> <asp:RadioButton ID="rdoNo" runat="server" GroupName="Group1" Text="No"  onclick="ShowAlert();"/></td>
</tr>
</table>
So that's it. I hope you will find this tutorial very informative.
Read more...

Format the Phone Number in asp.net using c#

In this tutorial you will learn how to format the phone number entered in a textbox. Phone number can be entered in a textbox as 1234567890 but you can format it as (123)456-789 and this is our topic.

Let's have a look how to do so.


Format the Phone Number in asp.net using c#

Let's suppose we have an asp:textbox in our .aspx page in which user will enter the phone number. The id of asp:textbox is txtPhoneNumber.

public string PhoneNoFormation()
{
string formatedPhoneNo;
if (!string.IsNullOrEmpty(txtPhoneNumber.Text.ToString()) && txtPhoneNumber.Text.Length == 10)
{
string Phone1 = "(" + txtPhoneNumber. Text.Substring(0, 3) + ")";
string Phone2 = " " + txtPhoneNumber. Text.Substring(3, 3);
string Phone3 = "-" + txtPhoneNumber. Text.Substring(6, 4);
formatedPhoneNo = Phone1 + Phone2 + Phone3;
}

else
{
formatedPhoneNo = txtPhoneNumber.Text;
}

return formatedPhoneNo;
}

Now your phone number is in formatted form and you can save it in database if you want to do so.

So that's it. I hope you will find this tutorial very informative.

I love your feedback.

Read more...