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

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

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

Add, Delete Items in DropDownList, ListBox using Javascript

I have seen lots of questions in Asp.Net forums for adding/deleting items in drop down list or list box using JavaScript. Here is the code...

<asp:DropDownList ID="DropDownList1" runat="server" Width="182px"> 
<asp:ListItem value="1" Text ="Approve"></asp:ListItem> 
<asp:ListItem value="2" Text ="Accept"></asp:ListItem> 
<asp:ListItem value="3" Text ="Test1"></asp:ListItem> 
<asp:ListItem value="4" Text ="Test2"></asp:ListItem> 
</asp:DropDownList> 

<input type="button" value="Remove selected item" onclick="JavaScript: DeleteItem();" /> 
<input type="text" id="ddlText" name="ddlText" /> 
<input type="text" id="ddlValue" name="ddlValue" /> 
<input type="button" value="Add item" onclick="JavaScript: AddItem();" /> 
<input type="hidden" id="ddlElements" name="ddlElements" runat="server" /> 
<asp:Button ID="Button1" runat="server" Text="Button" />
    <script type="text/javascript"> 
    function DeleteItem() 
    { 

        var dropDownListRef = document.getElementById('<%= DropDownList1.ClientID %>'); 
        var optionsList = ''; 

        if ( dropDownListRef.value.length > 0 ) 
        { 
            var itemIndex = dropDownListRef.selectedIndex;
       if ( itemIndex >= 0 ) 
            dropDownListRef.remove(itemIndex); 
        } 
        else 
        { 
            alert('Please select an item'); 
            dropDownListRef.focus(); 
            dropDownListRef.select(); 
        } 
          

        for (var i=0; i<dropDownListRef.options.length; i++) 
        { 
        var optionText = dropDownListRef.options[i].text;
        var optionValue = dropDownListRef.options[i].value; 
     
        if ( optionsList.length > 0 )
            optionsList += ';'; 
            optionsList += optionText; 
            optionsList += ';'; 
            optionsList += optionValue; 
        } 
        document.getElementById('<%= ddlElements.ClientID %>').value = optionsList; 
    } 


    function AddItem() 
    { 

        var dropDownListRef = document.getElementById('<%= DropDownList1.ClientID %>'); 
        var ddlTextRef = document.getElementById('ddlText'); 
        var ddlValueRef = document.getElementById('ddlValue'); 
        var optionsList = ''; 

        if ( ddlTextRef.value !="" && ddlValueRef.value!="" ) 
        { 
            var option1 = document.createElement("option"); 
            option1.text= ddlValueRef.value; 
            option1.value= ddlTextRef.value ; 
            dropDownListRef.options.add(option1); 
        } 
        else 
            alert('Please enter values'); 
          
        for (var i=0; i<dropDownListRef.options.length; i++) 
        { 

        var optionText = dropDownListRef.options[i].text;
        var optionValue = dropDownListRef.options[i].value; 
          
        if ( optionsList.length > 0 )
        
            optionsList += ';'; 
            optionsList += optionText; 
            optionsList += ';'; 
            optionsList += optionValue; 
        } 
            document.getElementById('<%= ddlElements.ClientID %>').value = optionsList; 
       
    } 

</script>

In Code behind Page_Load, add following code..

if (IsPostBack)
{
    DropDownList1.Items.Clear();
    string[] DropDownListArray = ddlElements.Value.Trim().Split(';'); 
    
    for (int i = 0; i < DropDownListArray.Length; i = i + 2)
    {
        string itemText = DropDownListArray[i];
        string itemValue = DropDownListArray[i + 1]; 
        DropDownList1.Items.Add(new ListItem(itemText, itemValue));
    }

}

string optionsList = string.Empty; 
for (int i = 0; i < DropDownList1.Items.Count; i++)
{
   string optionText = DropDownList1.Items[i].Text;
   string optionValue = DropDownList1.Items[i].Value;
          
    if (optionsList.Length > 0) 
      
    optionsList += ";";
    optionsList += optionText;
    optionsList += ';';
    optionsList += optionValue;
}

ddlElements.Value = optionsList; 

Update:  Client-side changes to a DropDownList/ListBox are not persisted server-side, so any changes made will be lost if a PostBack occurs. Added server-side persistence to code. (Thanks to tip form NC01)

 


Tags:
Posted by vijay on Friday, December 14, 2007 9:40 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Session timeout error in Asp.Net

1.) We were facing timeout issue on one of our servers. Web.config settings had no effect on the time out.

2.) I have set the session to last 24 hours with this statement in my web.config:

		
	
		<sessionState mode="InProc" timeout="720"/>	
	

     But the session still ends after 20 min of inactivity. What am I missing here?

I saw similar type of questions frequently in Asp.Net forums. Here is a post to help those

What is Session State?

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. In order to preserve server memory, ASP.NET implements a rolling timeout mechanism which discards the session information for a user if no request is seen within the timeout period (default 20 minutes which is reset with each request).

You would also have to setup session timeout in IIS in addition to webconfig settings.

In IIS6 change the session timeout setting by going to:
'Configuration' Button -> 'Options' Tab -> Tick Enable Session state, increase value from default 20 mins to desired value

In IIS7 change the session timeout setting by going to:

Features View pane –> Application Development – >Sessions state

IIS 7-1 

IIS 7-2

Don’t see Session state icon in IIS 7? Check this blog post

http://blogs.msdn.com/webdevtools/archive/2006/09/18/761206.aspx

Here are some other reasons for Session loss..

1.) IIS worker process restart or Application Pool recycle.(Check the System logs)

2.) Application Domain restart due to Antivirus scans or changes in Config files.

 

Please leave a comment.


Posted by vijay on Sunday, December 2, 2007 8:37 PM
Permalink | Comments (0) | 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