Thursday, November 15, 2012

Telerik Rad Window :- Refresh Parent Window when Child Window Closed.

Default.aspx
------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DUMMY_Default" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>

<!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></title>                            


</head>

<body>
    <form id="form1" runat="server">
    <div>
     <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />

    <asp:LinkButton ID="lnkbt1" runat="server" Text="Link1" OnClick="lnkbt1_Click"></asp:LinkButton>


    <telerik:RadWindow ID="RadWindow2" runat="server" Height="300px" Width="300px" Left="500"
            EnableShadow="true" Top="300" NavigateUrl="http://localhost:2607/Default2.aspx" VisibleOnPageLoad="false"
            ShowContentDuringLoad="false" />
    </div>
    </form>
</body>
</html>


-----------------------------------------------------
Default.aspx.cs
-------------------
 protected void lnkbt1_Click(object sender, EventArgs e)
    {
        lnkbt1.Text = "Clicked";
        RadWindow2.VisibleOnPageLoad = true;


    }


Default2.aspx
-----------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="DUMMY_Default2" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>

<!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 runat="server">
    <title></title>


   <script type="text/javascript">
       function GetRadWindow() {
           var oWindow = null;
           if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
           else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; //IE (and Moz az well) 
           return oWindow;
       }

       function CloseOnReload() {
           //alert("Dialog is about to close itself");
           GetRadWindow().close();
           RefreshParentPage();
       }

       function RefreshParentPage() {
           //alert("Dialog is about to reload parent page");
           GetRadWindow().BrowserWindow.location.reload();
       }

       function RedirectParentPage(newUrl) {
           alert("Dialog is about to redirect parent page to " + newUrl);
           GetRadWindow().BrowserWindow.document.location.href = newUrl;
       }

       function CallFunctionOnParentPage(fnName) {
           alert("Calling the function " + fnName + " defined on the parent page");
           var oWindow = GetRadWindow();
           if (oWindow.BrowserWindow[fnName] && typeof (oWindow.BrowserWindow[fnName]) == "function") {
               oWindow.BrowserWindow[fnName](oWindow);
           }
       }

       function RefreshParentPageWithoutWarning() {
           GetRadWindow().BrowserWindow.document.forms[0].submit();
       } 
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:LinkButton ID="lnk2" runat="server" Text="Close" OnClick="lnk2_Click"></asp:LinkButton>
    </div>
    </form>
</body>
</html>


-------------------------------------------------------------------------
Default2.aspx.cs
-------------------
 protected void lnk2_Click(object sender, EventArgs e)
    {
        Page page = HttpContext.Current.Handler as Page;



        ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "CloseOnReload();", true);
  
    }

Wednesday, November 14, 2012

Asp.net Warning Session TimeOut

Here is the link which has the source file for Asp.net Session Timeout

Download

Asp.Net Warning Message Before Session TimeOut

Web.Config
--------------
<configuration>
<appSettings>
    <add key ="SessionWarning" value ="1"/>
  </appSettings>    
</configuration>


-------------------------------------------------
SimpleAlert.aspx
-------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimpleAlert.aspx.cs" Inherits="SimpleAlert" %>

<!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 runat="server">
    <title></title>
    <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        var sessionTimeoutWarning = "<%= System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString()%>";
        var sessionTimeout = "<%= Session.Timeout %>";
        var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
        setTimeout('SessionWarning()', sTimeout);

        function SessionWarning() {
            var message = "Your session will expire in another " + (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)) + " mins! Please Save the data before the session expires";
            alert(message);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Post Back" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>


---------------------------------------------------
SimpleAlert.aspx.cs
------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ASPNET_jQueryUI_Dialog_Timeout
{
    public partial class _Default : System.Web.UI.Page
    {
        public bool TimeoutControlEnabled
        {
            get { return Timeout1.Enabled; }
            set { Timeout1.Enabled = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Timeout1.TimeoutMinutes = HttpContext.Current.Session.Timeout;
            Timeout1.AboutToTimeoutMinutes = HttpContext.Current.Session.Timeout - 1;
        }
    }
}



-----------------------------------------------------------------------------------------
SimpleAlertAndRedirect.aspx
--------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimpleAlertAndRedirect.aspx.cs" Inherits="SimpleAlertAndRedirect" %>

<!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 runat="server">
    <title></title>
    <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
     <script language="javascript" type="text/javascript">
        var sessionTimeoutWarning = "<%= System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString()%>";
        var sessionTimeout = "<%= Session.Timeout %>";
        var timeOnPageLoad = new Date();
 
        //For warning
        setTimeout('SessionWarning()', parseInt(sessionTimeoutWarning) * 60 * 1000);
        //To redirect to the welcome page
        setTimeout('RedirectToWelcomePage()',parseInt(sessionTimeout) * 60 * 1000);

        //Session Warning
        function SessionWarning() {
            //minutes left for expiry
            var minutesForExpiry =  (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning));
            var message = "Your session will expire in another " + minutesForExpiry + " mins! Please Save the data before the session expires";
            alert(message);
            var currentTime = new Date();
            //time for expiry
            var timeForExpiry = timeOnPageLoad.setMinutes(timeOnPageLoad.getMinutes() + parseInt(sessionTimeout)); 

            //Current time is greater than the expiry time
            if(Date.parse(currentTime) > timeForExpiry)
            {
                alert("Session expired. You will be redirected to welcome page");
                window.location = "../Welcome.aspx";
            }
        }

        //Session timeout
        function RedirectToWelcomePage(){
            alert("Session expired. You will be redirected to welcome page");
            window.location = "../Welcome.aspx";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>


-------------------------------------------------------------------------
SimpleAlertAndRedirect.aspx.cs
--------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SimpleAlertAndRedirect : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Session.IsNewSession.ToString());
        if (!IsPostBack)
        {
            Session["Test"] = "Some Value";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(Session["Test"].ToString());
    }
}


-----------------------------------------------------------------------
ExtendSession.aspx
---------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExtendSession.aspx.cs" Inherits="ExtendSession" %>

<!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 runat="server">
    <title></title>
    <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        var sessionTimeoutWarning = "<%= System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString()%>";
        var sessionTimeout = "<%= Session.Timeout %>";
        var timeOnPageLoad = new Date();
        var sessionWarningTimer = null;
        var redirectToWelcomePageTimer = null;
        //For warning
        var sessionWarningTimer = setTimeout('SessionWarning()', parseInt(sessionTimeoutWarning) * 60 * 1000);
        //To redirect to the welcome page
        var redirectToWelcomePageTimer = setTimeout('RedirectToWelcomePage()',parseInt(sessionTimeout) * 60 * 1000);

        //Session Warning
        function SessionWarning() {
            //minutes left for expiry
            var minutesForExpiry =  (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning));
            var message = "Your session will expire in another " + minutesForExpiry + " mins. Do you want to extend the session?";

            //Confirm the user if he wants to extend the session
            answer = confirm(message);

            //if yes, extend the session.
            if(answer)
            {
                var img = new Image(1, 1);
                img.src = 'KeepAlive.aspx?date=' + escape(new Date());

                //Clear the RedirectToWelcomePage method
                if (redirectToWelcomePageTimer != null) {
                    clearTimeout(redirectToWelcomePageTimer);
                }
                timeOnPageLoad =  new Date();
                sessionWarningTimer = setTimeout('SessionWarning()', parseInt(sessionTimeoutWarning) * 60 * 1000);
                //To redirect to the welcome page
                redirectToWelcomePageTimer = setTimeout('RedirectToWelcomePage()',parseInt(sessionTimeout) * 60 * 1000);
            }

            //*************************
            //Even after clicking ok(extending session) or cancel button, if the session time is over. Then exit the session.
            var currentTime = new Date();
            //time for expiry
            var timeForExpiry = timeOnPageLoad.setMinutes(timeOnPageLoad.getMinutes() + parseInt(sessionTimeout)); 

            //Current time is greater than the expiry time
            if(Date.parse(currentTime) > timeForExpiry)
            {
                alert("Session expired. You will be redirected to welcome page");
                window.location = "../Welcome.aspx";
            }
            //**************************
        }

        //Session timeout
        function RedirectToWelcomePage(){
            alert("Session expired. You will be redirected to welcome page");
            window.location = "../Welcome.aspx";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

------------------------------------------------------------------------
ExtendSession.aspx.cs
-------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ExtendSession : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Session.IsNewSession.ToString());
        if (!IsPostBack)
        {
            Session["Test"] = "Some Value";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(Session["Test"].ToString());
    }
}

Find GridView SelectedRow in OnRowCommand Event

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="DUMMY_Default3" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1"%>


<!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>Collapsible Panel Example</title>
<style type="text/css">
.pnlCSS{
font-weight: bold;
cursor: pointer;
border: solid 1px #c0c0c0;
width:30%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
 
<div>

<asp:GridView ID="GridView1" runat="server" DataKeyNames="AnnouncementID"
 AutoGenerateColumns="false" OnRowDataBound="GridView1_OnRowDataBound"
  OnRowCommand="GridView1_OnRowCommand">
  <Columns>
 <asp:TemplateField>
 <ItemTemplate>
  <asp:Label ID="lblItemIndex" runat="server" Text='<%#Container.DataItemIndex + 1 %>'>
 
 </asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
  
   <asp:TemplateField>
  <ItemTemplate>
  <asp:Label ID="lblAnnouncementID" runat="server" Text='<%#Eval("AnnouncementID") %>' Visible="false"></asp:Label>
  <asp:LinkButton ID="lnkAnnouncementID" runat="server" Text="Edit" CommandName="Editing" CommandArgument='<%#Eval("AnnouncementID") %>'></asp:LinkButton>
  </ItemTemplate>
  </asp:TemplateField>


  <asp:TemplateField>
  <ItemTemplate>
  <asp:Label ID="lblAnnouncementType" runat="server"></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField>
  <ItemTemplate>
  <asp:Label ID="lblTitle" runat="server"></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField>
  <ItemTemplate>
  <asp:Label ID="lblMessage" runat="server"></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>



  </Columns>
  </asp:GridView>

  <asp:Label ID="lblRow" runat="server"></asp:Label>
</div>
</form>
</body>
</html>




Default.aspx.cs
-----------------



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}


public void BindGrid()
{

TList objAnnouncement = //get the datas here...
GridView1.DataSource = objAnnouncement;
GridView1.DataBind();
}


protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ReadAnnouncement rowData = (ReadAnnouncement)e.Row.DataItem;
LinkButton lnkAnnouncementID = (LinkButton)e.Row.FindControl("lnkAnnouncementID");
Label lblAnnouncementType = (Label)e.Row.FindControl("lblAnnouncementType");
Label lblTitle = (Label)e.Row.FindControl("lblTitle");
Label lblMessage = (Label)e.Row.FindControl("lblMessage");

Label lblItemIndex = (Label)e.Row.FindControl("lblItemIndex");

lnkAnnouncementID.Text = Convert.ToString(rowData.AnnouncementId);
lblAnnouncementType.Text = Convert.ToString(rowData.AnnouncementType);
lblTitle.Text = Convert.ToString(rowData.Title);
lblMessage.Text = Convert.ToString(rowData.Message);
string MessageRead = Convert.ToString(rowData.MessageRead);

}
}


protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
//// here we are finding Selected Row......
GridViewRow gvRow = (GridViewRow)((Control)(e.CommandSource)).Parent.Parent;
Label lblTitle = (Label)gvRow.FindControl("lblTitle");

string URL = string.Format("~/Default4.aspx?AnnouncementID={0}", lblTitle);
Response.Redirect(URL);
BindGrid();

}