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 2017

CollapsiblePanelExtender set collapsed from codebehind

   <asp:CollapsiblePanelExtender ID="cpeTest" runat="server" TargetControlID="Panel1"
                                            CollapsedSize="0"
                                            Collapsed="True"
                                            ExpandControlID="lbutton1"
                                            CollapseControlID="lbutton1">

 cpeTest.ClientState = "true";
 cpeTest.Collapsed = true;

Posted by vijay on Friday, March 2, 2012 6:27 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Asp.Net Security ebook

If you want to read one book about Asp.Net security, read OWASP Top 10 for .NET developer by Troy Hunt

                There’s a harsh reality web application developers need to face up to; we don’t do security very well. A report from WhiteHat Security last year reported “83% of websites have had a high, critical or urgent issue”. That is, quite simply, a staggeringly high number and it’s only once you start to delve into to depths of web security that you begin to understand just how easy it is to inadvertently produce vulnerable code.


Posted by vijay on Thursday, December 29, 2011 6:05 PM
Permalink | Comments (0) | Post RSSRSS comment feed

AsyncFileUpload events with Update Panel

Problem:

 

Last week I came across this bug (?) with AsyncFileUpload control in updatepanel. The control is not firing OnUploadedComplete server side event when it’s in updatepanel.

 

Solution:

 

After doing some research on this issue, came across these solutions.

 

1)     Add another AsyncFileUpload control on the page and set its’ style to display:none; 

 

       

<div style="display: none;">

  <cc1:AsyncFileUpload ID="AsyncFileUpload1" runat="server" />

</div>

 

 

2)     Another workaround is to add the following attributes to the <form> tag of the page..

enctype="multipart/form-data" method="post"

 

 

Both the solutions are working fine.

 

 

Thanks to obout_teo and MikeMelendez of Asp.Net forums


Categories: AJAX | ASP.Net 3.5
Posted by Vijay on Monday, September 20, 2010 6:22 PM
Permalink | Comments (5) | Post RSSRSS comment feed

Disabled button not grayed out in Firefox and Chrome

When button is disabled, it’s not-clickable in all browsers. However in Firefox and Chrome, the button looks enabled, though the user cannot click it.

Reason:

From W3Scholl, "Enabled" Property isn't standard property of XHTML 4.

Solution:

You can modify the way they look with CSS

button[disabled] { 
 color:Grey;
/* Add other  styles here for disable button */ 
} 

Posted by vijay on Thursday, August 12, 2010 9:08 PM
Permalink | Comments (3) | Post RSSRSS comment feed

Alert user before session timeout

Here is a quick & dirty trick to alert users on session timeout. This is not a perfect solution. But it will give reader an idea to work on...

Some facts before we start:

Session doesn't end

  • When the user closes his browser
  • When the user navigates away from your page
  • When user connection lost.

Session ends, when the server hasn't gotten a request from the user in a specific time (Session timeout value).

In this solution, I am using ajaxtoolkit’s modalpopupextender control to alert user about expiring session.

Each time a page is rendered back to the client, I am injecting JavaScript that will show modalpopup two minutes before session timeout. I am passing the session expiry value to the client side java script. This will execute a countdown, and at the end display the Popup.

I added modalpopupextender to the page and set its target control id to a panel. That panel contains alert message and two buttons.

clip_image002

The Page_Load code looks like this.

clip_image004

I added two java script functions, one for showing alert message and second one is for hiding that message.

Here are javascript functions

clip_image006

That’s it. Run the application and it will check 2 minutes before the timeout and provide user the option to "slide" the session. If user clicks “OK” it will refresh page, which in turn will slide the session. If user clicks “Cancel” the popup will hide.

clip_image008

 

You can improve this code on each step. Like for example, to renew session you don’t have to refresh the page. You can just call web service from client side etc.

If you have any questions, leave a comment.


Posted by Vijay on Thursday, May 20, 2010 8:51 PM
Permalink | Comments (16) | Post RSSRSS comment feed

Asp.net session on browser close

How to capture logoff time when user closes browser?

Or

How to end user session when browser closed?

These are some of the frequently asked questions in asp.net forums.

In this post I'll show you how to do this when you're building an ASP.NET web application.

Before we start, one fact:

There is no full-proof technique to catch the browser close event for 100% of time. The trouble lies in the stateless nature of HTTP. The Web server is out of the picture as soon as it finishes sending the page content to the client. After that, all you can rely on is a client side script. Unfortunately, there is no reliable client side event for browser close.

Solution:

The first thing you need to do is create the web service. I've added web service and named it AsynchronousSave.asmx. 

 Open Dialog

Make this web service accessible from Script, by setting class qualified with the ScriptServiceAttribute attribute... 

clip_image004

Add a method (SaveLogOffTime) marked with [WebMethod] attribute. This method simply accepts UserId as a string variable and writes that value and logoff time to text file. But you can pass as many variables as required. You can then use this information for many purposes.

clip_image006

To end user session, you can just call Session.Abandon() in the above web method.

To enable web service to be called from page’s client side code, add script manager to page. Here i am adding to SessionTest.aspx page

clip_image008

When the user closes the browser, onbeforeunload event fires on the client side. Our final step is adding a java script function to that event, which makes web service calls. The code is simple but effective

clip_image010

My Code

HTML:( SessionTest.aspx )

clip_image012

C#:( SessionTest.aspx.cs )

clip_image014

That’s’ it. Run the application and after browser close, open the text file to see the log off time.

clip_image016

The above code works well in IE 7/8. If you have any questions, leave a comment.


Posted by vijay on Thursday, April 29, 2010 6:09 PM
Permalink | Comments (22) | Post RSSRSS comment feed

To access local IIS Web sites, you must install the following IIS components: Internet Information Services IIS 6 Metabase and IIS 6 Configuration Compatibility

If you are getting following error after converting asp.net project to latest version..

To access local IIS Web sites, you must install the following IIS components: Internet Information Services IIS 6 Metabase and IIS 6 Configuration Compatibility

Try this solution:

open Control Panel, click Programs and Features

WindowsFeatures

Expand Web Management Tools, expand IIS 6 Management Compatibility, and then select the IIS 6 Metabase and IIS 6 configuration compatibility check box.

IIS 6 Metabase and IIS 6 configuration compatibility


Expand World Wide Web Services, expand Application Development Features, and then select the ASP.NET check box.

aspNet

 

To enable Visual Studio to debug applications, you must configure IIS 7.0 with the Windows Authentication module. Expand World Wide Web Services, expand Security, and then select the Windows Authentication check box.

 

WindowdsAuthentication

Reference: http://msdn.microsoft.com/en-us/library/aa964620.aspx


Categories: ASP.NET | ASP.Net 3.5 | Windows 7
Posted by vijay on Thursday, April 8, 2010 3:01 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Convert time from one timezone to another timezone in Asp.Net

Handling timzone is quite simple in Asp.Net applications. First thing,   you should always store DateTime values in the DB in one timezone (UTC is most commonly used).  This eliminates many conversion issues.


Here is the sample code..

DateTime time1 = new DateTime(2008, 12, 11, 6, 0, 0);  // your DataTimeVariable 
TimeZoneInfo timeZone1 = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); 
TimeZoneInfo timeZone2 = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"); 
DateTime newTime = TimeZoneInfo.ConvertTime(time1, timeZone1, timeZone2);

Get all available/supported Timezones in .Net TimeZoneInfo class

      foreach (var tz in TimeZoneInfo.GetSystemTimeZones())
            {
                Response.Write(tz.DisplayName + " is Id =','" + tz.Id + "'");
                Response.Write("<br>");
            }


Posted by Vijay on Wednesday, January 6, 2010 8:24 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Auto save Asp.net form values Asynchronously

In this article, I will explain how to save Asp.Net page values asynchronously (aka Gmail style of saving mail drafts).  

Introduction:

In the past, Web applications were known for having less usable, less responsive user interfaces. AJAX changed all of that. The application can work asynchronously and the user doesn't have to sit and wait for pages to refresh. :

What is Ajax?

Ajax (Asynchronous JavaScript and XML) is an approach to web development that uses client-side scripting to exchange data with a web server. 

Solution:

There are several ways of achieve it. In this article I am using AJAX functionality to call ASP.NET Web services from the browser by using client script. Yes, no updatepanel.  

Start by adding a web service to the project as shown below, name it as AsynchronousSave.asmx. Make this web service accessible from Script, by setting class qualified with the ScriptServiceAttribute attribute...  

[System.Web.Script.Services.ScriptService]    
public class AsynchronousSave : System.Web.Services.WebService 


Here is the AsynchronousSave webservice class: 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebService2 : System.Web.Services.WebService
{

    [WebMethod]
    public string HelloWorld()
    {

        return "Hello World";

    }

    [WebMethod]
    public string SaveInput(String input)
    {
        string StrInput = Server.HtmlEncode(input); if (!String.IsNullOrEmpty(StrInput))
        {

            string[] strFields = StrInput.Split('+');

            //code to save all input values
            // you can easily savethese values to temp DB table, temp file or xml file
            //Dispaly last saved values

            return String.Format("Last saved text: FirstName {0} ,<br/> Last name {1} <br/> Last "

            + "saved draft {2} at {3}.", strFields[0], strFields[1], strFields[2], DateTime.Now);

        }

        else
        {

            return ""; //if input values are empty..retrun empty string
        }
    }
}
 
Nothing fancy here, just methods marked with [WebMethod] attribute. The Saveinput method takes an input string with “+” as delimiter between form values.  

To enable web service to be called from client side, add script manager to page  

<asp:ScriptManager runat="server" ID="scriptManager">
    <Services>
        <asp:ServiceReference Path="~/AsynchronousSave.asmx" />
    </Services>
</asp:ScriptManager>


And here is HTML of page

<div>
    First Name:<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></br> 
    LastName:<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></br> 
    Draft :<asp:TextBox ID="txtDraft" runat="server"></asp:TextBox></br></div>
<div id="Preview">

The following example shows the java script that makes service calls

<script language ="javascript" >
    //This function calls the web service    
    function SaveUserInput() {
        var fName = document.getElementById("txtFirstName");
        var lName = document.getElementById("txtLastName");
        var draft = document.getElementById("txtDraft");
        //Saving all input in a single value         
        var input = fName.value + "+" + lName.value + "+" + draft.value;
        //ProjName.WebServiceName.WebMethod         
        SampleApplication1.AsynchronousSave.SaveInput(input, SucceededCallback);
    }
    // This is the callback function that processes the Web Service return value.     
    function SucceededCallback(result) {
        var divPreview = document.getElementById("Preview");
        divPreview.innerHTML = result;
    }
    // execute SaveUserInput for every 10 sec, timeout value is in miliseconds
    window.setInterval('SaveUserInput()', 10000); 
    
</script>

Reference:

http://msdn.microsoft.com/en-us/library/bb398998.aspx

         


Tags:
Posted by vijay on Monday, November 23, 2009 2:17 PM
Permalink | Comments (0) | Post RSSRSS comment feed

ASP.Net AJAX AsyncFileUpload Control

AsyncFileUpload is a new ASP.NET AJAX Control (Ajax Control Toolkit (v 3.0.30930) released for .NET 3.5 SP1) that allows you asynchronously upload files to server. You don’t need a separate upload button for this control.  Add the AsyncFileUpload control and a label to the web form for the uploading and displaying messages respectively.  HTML:

<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
<cc1:AsyncFileUpload ID="AsyncFileUpload1" runat="server" CompleteBackColor="White" 
OnUploadedComplete="AsyncFileUpload1_UploadedComplete" OnUploadedFileError="AsyncFileUpload1_UploadedFileError" 
OnClientUploadComplete="Success" OnClientUploadError="Error" /> 
<asp:Label ID="Label1" runat="server"></asp:Label> 

Server side events:

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) 
{ 
 //Fired on the server side when the file successfully uploaded 
   if (AsyncFileUpload1.HasFile) 
   { 
         AsyncFileUpload1.SaveAs(@"C:\Images\" + AsyncFileUpload1.FileName ); 
         Label1.Text = "Received " + AsyncFileUpload1.FileName + " Content Type " + AsyncFileUpload1.PostedFile.ContentType ; 
   } 
 } 
protected void AsyncFileUpload1_UploadedFileError(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) 
 { 
 //Fired on the server side when the loaded file is corrupted 
 //Display some error message here 
 } 

Client side events:

OnClientUploadError - The name of a javascript function executed in the client-side if the file uploading failed  

OnClientUploadComplete - The name of a javascript function executed in the client-side on the file uploading completed

function Success() { 
document.getElementById("Label1").innerHTML = "File Uploaded Successfully !!"; 
} 
function Error() { 
document.getElementById("Label1").innerHTML = "File upload failed"; 
} 

We can use  OnClientUploadComplete to clear fileupload control selction,

function Success() { 
document.getElementById ("Label1").innerHTML = "File Uploaded Successfully !!"; 
var fu = document.getElementById("AsyncFileUpload1"); 
document.getElementById("AsyncFileUpload1").innerHTML = fu.innerHTML; 
} 

Reference:  http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AsyncFileUpload/AsyncFileUpload.aspx


Posted by vijay on Friday, October 2, 2009 2:48 PM
Permalink | Comments (0) | Post RSSRSS comment feed