How to log each User Controls load time using C# in ASP.Net

Last Reply 2 months ago By born2achie

Posted 3 months ago

Hi,

I have 6 users controls in my page and i want to log the each user controls load time in backend. Any suggestion how to achieve this? 

Posted 2 months ago

Parent page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StartStopParent.aspx.cs" Inherits="TestingWeb.usercontrol.StartStopParent" %>
<%@ Register Src="~/usercontrol/StartEnd.ascx" TagPrefix="uc1" TagName="StartEnd" %>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestingWeb.usercontrol
{
    public partial class StartStopParent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void StartEnd_Init(object sender, EventArgs e)
        {
            StartEnd.StartTime.Text = DateTime.Now.ToString();
            Thread.Sleep(1000);
        }

        protected void StartEnd_PreRender(object sender, EventArgs e)
        {
            StartEnd.EndTime.Text = DateTime.Now.ToString();
        }

    }
}

User Control

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="StartEnd.ascx.cs" Inherits="TestingWeb.usercontrol.StartEnd" %>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestingWeb.usercontrol
{
    public partial class StartEnd : System.Web.UI.UserControl
    {
        public Label StartTime
        {
            get
            {
                return Start;
            }
        }
        public Label EndTime
        {
            get
            {
                return End;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

    }
}

But the problem here is i need to create init and pre render even for each usercontrol. How do i avoid the code repetition here.how to make generic event handler and call that. Any sample please 

 


Posted 2 months ago Modified on 2 months ago

Hi born2achie,

You can do it like below.

StartEnd.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="StartEnd.ascx.cs" Inherits="StartEnd" %>
<asp:Label ID="Start" runat="server" />
<br />
<asp:Label ID="End" runat="server" />

StartEnd1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="StartEnd1.ascx.cs" Inherits="StartEnd1" %>
<asp:Label ID="Start" runat="server" />
<br />
<asp:Label ID="End" runat="server" />

StartEnd2.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="StartEnd2.ascx.cs" Inherits="StartEnd2" %>
<asp:Label ID="Start" runat="server" />
<br />
<asp:Label ID="End" runat="server" />

StartEnd3.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="StartEnd3.ascx.cs" Inherits="StartEnd3" %>
<asp:Label ID="Start" runat="server" />
<br />
<asp:Label ID="End" runat="server" />

StartEnd.ascx.cs

StartEnd1.ascx.cs

StartEnd2.ascx.cs

StartEnd3.ascx.cs

public Label StartTime
{
    get { return Start; }
}
public Label EndTime
{
    get { return End; }
}

protected void Page_Load(object sender, EventArgs e)
{

}  

StartStopParent.aspx

<%@ Register Src="~/StartEnd.ascx" TagPrefix="uc" TagName="StartEnd" %>
<%@ Register Src="~/StartEnd1.ascx" TagPrefix="uc1" TagName="StartEnd1" %>
<%@ Register Src="~/StartEnd2.ascx" TagPrefix="uc2" TagName="StartEnd2" %>
<%@ Register Src="~/StartEnd3.ascx" TagPrefix="uc3" TagName="StartEnd3" %>
<!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>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        table
        {
            border: 1px solid #ccc;
        }
        table th
        {
            background-color: #F7F7F7;
            color: #333;
            font-weight: bold;
        }
        table th, table td
        {
            padding: 5px;
            border-color: #ccc;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="1">
            <tr>
                <td>
                    <uc:StartEnd ID="StartEnd" runat="server" OnInit="StartEnd_Init" OnPreRender="StartEnd_PreRender" />
                </td>
            </tr>
            <tr>
                <td>
                    <uc1:StartEnd1 ID="StartEnd1" runat="server" OnInit="StartEnd_Init" OnPreRender="StartEnd_PreRender" />
                </td>
            </tr>
            <tr>
                <td>
                    <uc2:StartEnd2 ID="StartEnd2" runat="server" OnInit="StartEnd_Init" OnPreRender="StartEnd_PreRender" />
                </td>
            </tr>
            <tr>
                <td>
                    <uc3:StartEnd3 ID="StartEnd3" runat="server" OnInit="StartEnd_Init" OnPreRender="StartEnd_PreRender" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

StartStopParent.aspx.cs

protected void StartEnd_Init(object sender, EventArgs e)
{
    StartEnd.StartTime.Text = "Start : " + DateTime.Now.ToString();
    Thread.Sleep(200);
    StartEnd1.StartTime.Text = "Start1 : " + DateTime.Now.ToString();
    Thread.Sleep(200);
    StartEnd2.StartTime.Text = "Start2 : " + DateTime.Now.ToString();
    Thread.Sleep(200);
    StartEnd3.StartTime.Text = "Start3 : " + DateTime.Now.ToString();
}

protected void StartEnd_PreRender(object sender, EventArgs e)
{
    StartEnd.EndTime.Text = "End : " + DateTime.Now.ToString();
    Thread.Sleep(200);
    StartEnd1.EndTime.Text = "End1 : " + DateTime.Now.ToString();
    Thread.Sleep(200);
    StartEnd2.EndTime.Text = "End2 : " + DateTime.Now.ToString();
    Thread.Sleep(200);
    StartEnd3.EndTime.Text = "End3 : " + DateTime.Now.ToString();
}

Output

Start : 12/28/2016 9:17:00 PM End : 12/28/2016 9:17:03 PM
Start1 : 12/28/2016 9:17:00 PM End1 : 12/28/2016 9:17:03 PM
Start2 : 12/28/2016 9:17:01 PM End2 : 12/28/2016 9:17:03 PM
Start3 : 12/28/2016 9:17:01 PM End3 : 12/28/2016 9:17:03 PM

 


Posted 2 months ago

thank you dharmendr and sorry for the late reply as i was on vacation. thank you and i will try and get back

I agree, here is the link: http://e-iceblue.com/free-api