Monday, May 28, 2012

ASP.NET(Caching With Web User Control)


// First Step: Create WebUser Control(.ascx) File
Ctrl + Shift + A à WebUserControl à Add

// Second Step: Create Cache With 60Sec Time Duration
<%@ OutputCache Duration="60" VaryByParam="none" %>

// Third Step: Your Code. (ExampleCode) Given Below
<%
   
    #region LoadMenus

    try
    {
        string strSql = "SELECT <column_name> FROM <table_name>";
        string strError = string.Empty;

        DALayer layer = new DALayer();
        System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(strSql);

        System.Data.DataSet ds = layer.FetchRecords(command, ref strError);

        if (strError != "T")
        {
            throw new Exception(strError);
        }

        System.Data.DataView dv = ds.Tables[0].AsDataView();
        dv.RowFilter = "isHeader=1";

        foreach (System.Data.DataRowView row in dv)
        {
            sb.AppendLine(" <dt>");
            sb.AppendLine("     <b>" + row["Column_Name"].ToString() + "</b>");
            sb.AppendLine(" </dt>");

            sb.AppendLine("<dd>");
            System.Data.DataView dv1 = ds.Tables[0].AsDataView();
            dv1.RowFilter = "ParentId=" + int.Parse(row["Column_Name"].ToString());

            if (dv1.Count > 0)
            {
                sb.AppendLine(" <ul>");
                foreach (System.Data.DataRowView row1 in dv1)
                {
                  sb.AppendLine("<li><a href='" + row1["Column_Name"].ToString() + "'>" + row1["Name"].ToString() + "</a></li>");
                }
                 sb.AppendLine("</ul>");
            }
             sb.AppendLine(" </dd>");
        }
        Response.Write(sb.ToString());
     }
    catch (Exception ex)
    {
        return;
    }
    #endregion
%>

// Fourth Step: Write the header file of the Web User Control on your ASP page
<%@ Register tagprefix="LeftNavigation" tagname="Menu" src="~/UserControls/userControlName.ascx" %>

// Fifth Step: Write the TAG With the desired location
<LeftNavigation:Menu runat="server" />

No comments:

Post a Comment