Showing posts with label Issues and Solutions. Show all posts
Showing posts with label Issues and Solutions. Show all posts

Sunday, January 12, 2014

How to avoid multiple button clicks while post back in ASP.NET

 
Many time we face the issue how to prevent user from clicking the same button before completing the post back.

A simple java script can be added for this. this will change the text of button and verify. A Dot Net 2.0 example given below


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DropDownlist_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>CheckBoxTest</title>
</head>
<body>
    <form id="Form1" runat="server">
    <div>
        <asp:Button ID="btnAction" runat="server" 
OnCommand="ActionEvent" 
 OnClientClick="
{
var res=this.value=='Processing...'?false:true;
 this.value='Processing...';
 return res;
}"
 Text="Do Action" Width="125px" />
    </div>
    </form>
</body>
</html>


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class DropDownlist_Default : System.Web.UI.Page
{
   
    protected void ActionEvent(object sender, CommandEventArgs e)
    { 
      //System.Threading.Thread.Sleep(10000);        
    }
}


 
The text of button is changed to 'Processing...' so that the user even knoWS something is going on and even if he clicks it will not post back as our client script returns false if the caption is 'Processing...'.

Handling page refresh (F5) in ASP.NET post backs

Handling page refresh (F5) on post backs is one of the biggest problem for many web applications.Usually we used the simplest solution by redirecting to the same page after the action, but not always usefull.
In the previous post we saw how to handle a multiple user clicks on the same button.
but still user can overdo things by page refresh (F5). 
I use a header user control in all my applications which I found is the best place to handle this. The theory is F5 will post a older viewstate so if we have anything to compare we can skip the action from being perfomed once again.
The below code in .NET2.0 goes well with IE.
Insert it in your page or some common header/banner control 
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["PostID"] = "1001";
        ViewState["PostID"] = Session["PostID"].ToString();
    }
}
public bool IsValidPost()
{
    if (ViewState["PostID"].ToString() 
        == Session["PostID"].ToString())
    {
        Session["PostID"] = 
        (Convert.ToInt16(Session["PostID"]) + 1).ToString();
        ViewState["PostID"] = Session["PostID"].ToString();
        return true;
    }
    else
    {
        ViewState["PostID"] =
         Session["PostID"].ToString();
        return false;
    }
}
Now in your button click verify if the postback is real button click
public void ActionEvent(object sender, CommandEventArgs e)
{
 if (IsValidPost())
 {
  DoPocessing();
 }
}

F5 problem in Asp.net

F5 problem..asp.net
Whenever we press f5 insert query between page execution whole page execute again.
Solution for this
1) Capcha
2)Maintaining session state and viewstate
How to maintain session and view state
protected void Page_PreRender(Object sender, EventArgs e)
{
ViewState["update"] = Session["update"];
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());//to avoid problem of refresh frm Browser
}
}
protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
{
if (Session["update"].ToString() == ViewState["update"].ToString())
{
//Code
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
}
}
if user press f5 again then the buttonclick will execute again but it will not enter in
if (Session["update"].ToString() == ViewState["update"].ToString())
bcoz now session["update"] value has change.

Saturday, January 11, 2014

Jquery Was loaded on page postback

Scenario :
   Jquery was not loading when the page is partially loaded.(update panel).
  Script used :
    <script type="text/javascript">
        $(window).load(function () {
            $('#nivo-slider').nivoSlider();
        });
    </script>
  Solution : (use pageLoad instead of $...)
     <script type="text/javascript">
        function pageLoad()
          {
             $('#nivo-slider').nivoSlider();
         }
    </script>