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

Web Application Project vs Web Site Project in Visual Studio

Web Site Project is deployed with source code to the server and all compilation takes place at runtime.

Web Application Projects, the code behind classes are compiled to dll. That dll is deployed and at runtime, the compiled code in the dll and the markup is combined to create a class which is used by the server to render output.

Update:

Good post by anthony on this

post by Stephen on this topic


Posted by vijay on Sunday, July 6, 2008 9:04 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Tools to help you make pages faster

Response times, availability, and stability are vital factors to bear in mind when creating and maintaining a web application. Check this excellent list of tools

Categories: General | Web Development
Posted by vijay on Wednesday, June 18, 2008 3:30 PM
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

Change back ground color of Asp.net Textbox on validation failure

Here is a way of changing back ground color for Textbox on validation failure. The key here is using Page_ClientValidate function in javascript.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter a value"
    ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="btnClick();"
    OnClick="Button1_Click1" />

<script type="text/javascript"> 

    function btnClick() 
    { 
        if (!Page_ClientValidate()) 
        { 
            document.getElementById("TextBox1").style.backgroundColor="red"; 
        } 
    } 

    function ClearBackGround() 
    { 
        document.getElementById("TextBox1").style.backgroundColor=""; 
    } 

</script>
In Code behind

TextBox1.Attributes.Add("onkeypress", "ClearBackGround()");


Tags:
Categories: ASP.NET | JavaScript
Posted by vijay on Thursday, June 5, 2008 9:08 AM
Permalink | Comments (1) | 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

Comparing Time part in DateTime SQL

if a column in DB table is a datetime and you need to compare TIME only (without DATE) , there is no built in function for that.

Here is small Query to perform that task. The key is converting both to same Date

@time1 datetime  
@time2 datetime 
select @time1 = '1900-01-01 07:30:00.000',
 @time2 = '1899-12-30 07:30:00.000' 
-- This convert both @time1 & @time2 into same date (1900-01-01) such that you can compare the time. 
-- Note : Resuls is a datetime
select tm1 = dateadd(day, datediff(day, 0, @time1) * -1, @time1),  
-- This uses convert function + substring to extract the time. 
-- Note : Result will be a varchar
tm2 = dateadd(day, datediff(day, 0, @time2) * -1, @time2)
select tm1 = substring(convert(varchar(25), @time1, 121), 12, 12), tm2 = substring(convert(varchar(25), @time2, 121), 12, 12) 
---Comapre tm1 & tm2 

 

Thanks to tip from "khtan" of SqlTeam


Categories: SQL server
Posted by vijay on Wednesday, May 14, 2008 11:46 AM
Permalink | Comments (2) | 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

Detecting Asp.Net page close event

Sometimes we have to capture page close event or navigation event to alert users. For example, Alerting user for navigating away from input page without saving it. Check this java script code..

window.onbeforeunload= function(){
if((window.event.clientX<0) || (window.event.clientY<0))
    {
      event.returnValue = "Are you sure you want to navigate away from this page? You haven't saved this form.";
    }
};

This code works only in Ie6/7. And also keep in mind, we can never catch the browser close event for 100%. For example, what if the user kills the browser process or shut down OS.


Tags:
Posted by vijay on Thursday, March 20, 2008 2:49 PM
Permalink | Comments (0) | Post RSSRSS comment feed