views:

171

answers:

2

Hi,

In a module i have a sub that create modalpopup and show result of action..I dont wanna drop & drag modalpopup extender and make html complex therefore i do that in code-behind.I wonder that Can i cache my panel and then get it from cache?It takes time to crate these panels.Or do u have recommodations to improve performance of this Sub?I use Script Combining,JS minify and Css minify.My sub's code is here.

Public Sub Raise_Alarm(ByVal p_Page As Page, ByRef p_AssignedButton As System.Web.UI.WebControls.Button, ByVal p_Message As String, Optional ByVal p_Type As MessageType = MessageType.Success)

    Dim placeHolder As PlaceHolder = p_Page.FindControl("plcMsg")

    Dim lblStatus As System.Web.UI.WebControls.Label
    lblStatus = p_Page.FindControl("lblStatus")
    lblStatus.Width = Unit.Percentage(100)
    lblStatus.Font.Name = "verdana"
    lblStatus.Font.Size = System.Web.UI.WebControls.FontUnit.XSmall


    'Create Modalpopup extender
    Dim mdlMessage As New AjaxControlToolkit.ModalPopupExtender

    'Create Divs
    Dim pnlMessage As New System.Web.UI.WebControls.Panel
    Dim pnlFrame As New System.Web.UI.WebControls.Panel
    Dim pnlContainer As New System.Web.UI.WebControls.Panel
    Dim pnlHeader As New System.Web.UI.WebControls.Panel
    Dim pnlMsg As New System.Web.UI.WebControls.Panel
    Dim pnlBody As New System.Web.UI.WebControls.Panel
    Dim pnlFooter As New System.Web.UI.WebControls.Panel
    Dim pnlRight As New System.Web.UI.WebControls.Panel
    '*******

    'Create Ok Buttons

    Dim btnOk As New System.Web.UI.WebControls.Button

    'Create Message Label

    Dim lblMessage As New System.Web.UI.WebControls.Label
    Dim myheader As New System.Web.UI.WebControls.Label
    'Assign Properties 

    pnlMessage.ID = "pnlMessage"
    pnlMessage.CssClass = "modal-dialog"
    pnlMessage.Style.Add("display", "none")

    pnlFrame.ID = "pnlframe"
    pnlFrame.CssClass = "frame"
    pnlContainer.ID = "container"
    pnlContainer.CssClass = "container"
    pnlHeader.ID = "header"
    pnlHeader.CssClass = "header"

    myheader.ID = "headerlabel"
    myheader.Text = "Warning"

    pnlMsg.ID = "msg"
    pnlMsg.CssClass = "msg"

    pnlBody.ID = "body"
    pnlBody.CssClass = "body"

    pnlFooter.ID = "footer"
    pnlFooter.CssClass = "footer"

    pnlRight.ID = "right"
    pnlRight.CssClass = "right"

    btnOk.ID = "btnOk"
    btnOk.Width = Unit.Pixel(50)
    btnOk.Text = "Ok"
    btnOk.CssClass = "but"      
    btnOk.OnClientClick = "Dispose()"



    lblMessage.ID = "lblMessage"
    lblMessage.Text = p_Message
    lblMessage.CssClass = "body"

    If p_Type = MessageType.Success Then

        lblStatus.ForeColor = System.Drawing.Color.Blue
        lblStatus.Text = p_Message
        lblStatus.Visible = True

    ElseIf p_Type = MessageType.Error Then

        lblStatus.ForeColor = System.Drawing.Color.Red

    End If


    pnlRight.Controls.Add(btnOk)
    pnlFooter.Controls.Add(pnlRight)
    pnlBody.Controls.Add(lblMessage)
    pnlBody.Controls.Add(pnlFooter)
    pnlMsg.Controls.Add(myheader)
    pnlHeader.Controls.Add(pnlMsg)
    pnlContainer.Controls.Add(pnlHeader)
    pnlContainer.Controls.Add(pnlBody)
    pnlFrame.Controls.Add(pnlContainer)
    pnlMessage.Controls.Add(pnlFrame)


    With mdlMessage


        .TargetControlID = p_AssigedButton.ID
        .PopupControlID = pnlMessage.ID
        .ID = "mdlMessage"
        .BackgroundCssClass = "modalBackground"
        .OkControlID = btnOk.ID
        .Page = p_Page

    End With

    placeHolder.Controls.Add(pnlMessage)
    placeHolder.Controls.Add(mdlMessage)
    mdlMessage.Show()
End Sub
+1  A: 

Have you measured the performance of this sub?

We looked at this once and found that even 1000's of create objects were still only a few nano seconds.

If you want to cache something it must be serializable. For that to give you an improvement it must take less time to deserialize, than what it takes to create a new fresh object.

It does not look like you are getting any values from a database, so I do not think that you can optimize this code with caching.

EDIT

There are a couple of other reasons not to do the caching:

  • It complicates your program, you need to check if a value is in cache before you can use it.
  • There are possibilities for introducing errors. Take for example lblStatus, this changes value depending on the parameters. If you just read it from the cache you could get one that does not match your parameters.
Shiraz Bhaiji
Hi Shiraz, i have measured performance ofcourse it doesnt take even 1 second to create these objects.But imagine that 1000 users open the page and create these object,doesnt it deteriorate performance?I tried Alison's cache recommendiation,but still thinking that does it give me performance benefit to take div's from cache?You know asp.net ajax's controls sometimes can influence application badly,i dont wanna come across a performance problem therefore i want to try cache solution
Alexander
You should only think about performance, when it becomes a problem. Do a stress test of your app with number of users compared to the real planned amount, and see what happens. In most cases it's enough to just use per-page caching mechanisms in ASP.Net.
HeavyWave
A: 

To insert the control into the cache:

//Make sure to adjust the expiration of the cache with a contstant
Cache.Insert("pnlPanelMessage", pnlMessage, null, DateTime.MaxValue, TimeSpan.FromMinutes(15));

To retrieve the control fromt he cache:

System.Web.UI.WebControls.Panel pnlMessage = (System.Web.UI.WebControls.Panel)Cache.Get("pnlPanelMessage")

MSDN: ASP.NET Caching: Techniques and Best Practices

Alison