About the author

Vijay Kodali
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2024

Detecting crawlers,bots,spiders in Asp.Net

Here is a way of detecting search engine (Google, live…) crawlers,spiders etc..

[code:c#]
            System.Web.HttpBrowserCapabilities clientBrowserCaps = Request.Browser;
            if (((System.Web.Configuration.HttpCapabilitiesBase)clientBrowserCaps).Crawler)
            {
                Response.Write ( "Browser is a search engine.");
            }
            else
            {
               Response.Write ("Browser is not a search engine.");
            } 

//Here is another approach..

            if (Request.ServerVariables["HTTP_USER_AGENT"].Contains("Googlebot"))
            {
                //log Google bot visit
            }
            else if (Request.ServerVariables["HTTP_USER_AGENT"].Contains("msnbot"))
            {
                //log MSN bot visit
            }
            else if (Request.ServerVariables["HTTP_USER_AGENT"].Contains("Yahoo"))
            {
                //log yahoo bot visit
            }
            else
            {
                //similarly you can check for other search engines
            }

[/code]


Posted by vijay on Tuesday, July 15, 2008 11:51 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Removing empty spaces and HTML tags from TextBox text on client side

Here is a small code to remove spaces from textbox's text in onblur event.

 

<asp:TextBox ID="TextBox2" runat="server" onblur="javascript:value=value.replace(/\s/g,'');"></asp:TextBox>

 

To remove HTML tags..

<asp:textbox id="TextBox2" runat="server" onblur="this.value = this.value.replace(/<\/?[^>]+>/gi, '');">
</asp:textbox>

 

Please let me know, if you have any issues.


Tags:
Posted by vijay on Thursday, June 12, 2008 1:24 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Asp.Net Time Validation Regular Expression

Regular expression for validating time part: (([0-1][0-9])|([2][0-3])):([0-5][0-9])


Posted by Vijay on Sunday, May 25, 2008 11:18 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Disable Browser Back Button in ASP.NET

The browser back button cannot be disabled as the browser security will not allow this. If you want user to stay on the same page, even if user presses back button, try this java script..

<script language="JavaScript">
javascript:window.history.forward(1);
</script>

What if the JavaScript is disabled by the client, then this code doesn't work. And also it works great in IE, but not in other browsers. So I do not highly recommend using this.

 

Here is another solution for back button problem..

Add this code to Page_load

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now); 

 

The above code will disable page cache. Since the page is not being cached on the browser, the page will be reloaded when the user hits the back button.


Posted by vijay on Thursday, April 24, 2008 9:38 PM
Permalink | Comments (7) | Post RSSRSS comment feed

Detecting Session Timeout and Redirect to Login Page in ASP.NET

HTTP(web page) is stateless, so we cannot know whether the Session has really expired without sending a page request back to the server. If you want to redirect the page immediately(with out any user action) after session expires consider this approach.

 

Response.AppendHeader("Refresh",Convert.ToString((Session.Timeout * 60)+ 10)+";URL=Login.aspx");

 

Add this line of code to page_Load.


Tags:
Categories: ASP.NET | ASP.Net 3.5
Posted by vijay on Friday, April 11, 2008 6:35 PM
Permalink | Comments (2) | Post RSSRSS comment feed

ListView

One of the new controls in ASP.NET 3.5 is ListView. It resembles the GridView control, except that it displays data by using user-defined templates instead of row fields.

The ListView control supports the following features:

ü  Support for binding to data source controls such as SqlDataSource, LinqDataSource, and ObjectDataSource.

ü  Customizable appearance through user-defined templates and styles.

ü  Built-in sorting capabilities.

ü  Built-in update and delete capabilities.

ü  Built-in insert capabilities.

ü  Support for paging capabilities by using a DataPager control.

ü  Built-in item selection capabilities.

ü  Programmatic access to the ListView object model to dynamically set properties, handle events, and so on.

ü  Multiple key fields.

 


Categories: .Net 3.5 | ASP.Net 3.5 | C#
Posted by vijay on Monday, January 14, 2008 8:13 PM
Permalink | Comments (0) | Post RSSRSS comment feed

cursor position after postback

To maintain cursor position on every page, set following code in web.config file

<system.web>             
        <pages maintainScrollPositionOnPostBack="true"></pages>
</system.web> 

Categories: ASP.NET | ASP.Net 3.5
Posted by vijay on Thursday, January 10, 2008 12:09 PM
Permalink | Comments (1) | Post RSSRSS comment feed

Java Script function to only allow integers in the textbox

Here is sample code for TextBox Validation to allow Numbers only.

[code:c#]

<script type = "text/javascript" language = "javascript">

function CheckTextBox(i)
{
    if(i.value.length>0)
    {
    i.value = i.value.replace(/[^\d]+/g, '');
    }
}

</Script>

[/code]

In Code behind add this function to "onkeyup" event for TextBox

[code:c#]

TextBox1.Attributes.Add("onkeypress", "CheckTextBox(this)");

[/code]


Posted by vijay on Wednesday, November 14, 2007 7:54 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Auto refresh a web form asp.net

Some times we need to auto-refresh page after few seconds. you can do it with Meta Refresh tags

<head><meta http-equiv="Refresh" content="180" /></head>

Where content '180' is the number of seconds.

Another approach is by addding HTTP header..

[code:c#]

Response.AppendHeader("Refresh", "180");

[/code]


Posted by vijay on Sunday, October 14, 2007 3:24 PM
Permalink | Comments (1) | Post RSSRSS comment feed

To add Tool Tip for DropDownList items in Asp.Net

Here is sample code for adding tooltip to DropDownList. The key here is adding "title" attribute to the list items in DataBound event.

<asp:DropDownList ID="DropDownList1" runat="server" ondatabound="DropDownList1_DataBound">
</asp:DropDownList>
In code behind ..
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
    DropDownList ddl = sender as DropDownList;
    if (ddl != null)
    {
        foreach (ListItem li in ddl.Items)
        {
           li.Attributes["title"] = li.Text; // setting text value of item as tooltip
        }
    }

}
 

Tags:
Posted by vijay on Thursday, October 11, 2007 9:15 PM
Permalink | Comments (1) | Post RSSRSS comment feed