Posts Tagged ‘asp.net’

Handling Persistance in Asp.Net with Dynamic Controls

March 29th, 2010

I worked on a project a couple of years ago using dynamic controls in an Asp.Net WebForms environment. Managing dynamic controls can be a real hassle.

So when I read Dave Reed’s great article about Viewstate, I wondered if I could do it better now. I worked up a simple example using no Viewstate where values persist across postbacks. Going to the page new resets everything.

The one piece I don’t like is that it uses session for a control count. But it’s a single Int32 per user that get’s stored in session, so it could be worse. Anyway, here’s the code…

Note: the aspx page has a placeholder (phOne) and two buttons (btnAdd) and (btnRemove). EnableViewstate is false on phOne.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class Dynamic : System.Web.UI.Page
    {
        private const string KeyNumControls = "DynamicPage_NumControls";
        private const int DefaultControlCount = 2;

        protected override void OnInit(EventArgs e)
        {
            CreateControls(NumberOfControls);
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CreateControls(DefaultControlCount);
            }
        }

        private void CreateControls(int controlCount)
        {
            NumberOfControls = controlCount;
            while(phOne.Controls.Count > NumberOfControls)
            {
                RemoveLastControl();
            }
            while(phOne.Controls.Count < NumberOfControls)
            {
                CreateControl();
            }
        }

        private void RemoveLastControl()
        {
            phOne.Controls.RemoveAt(phOne.Controls.Count - 1);
        }

        private void CreateControl()
        {
            phOne.Controls.Add(new TextBox { EnableViewState = false });
        }

        protected int NumberOfControls
        {
            get
            {
                Session[KeyNumControls] = Session[KeyNumControls] ??
                                        DefaultControlCount;
                return (int)Session[KeyNumControls];
            }
            set { Session[KeyNumControls] = value; }
        }

        protected void BtnAddClick(object sender, EventArgs e)
        {
            NumberOfControls += 1;
            CreateControl();
        }

        protected void BtnRemoveClick(object sender, EventArgs e)
        {
            NumberOfControls -= 1;
            RemoveLastControl();
        }
    }
}

ASP.Net Comments

July 7th, 2009

Here’s a strange one I’ve never run into. I was recently helping a developer figure out why some controls were not binding and showing on the page.

I noticed they were using html style comments in their page around some controls instead of .net style comments. This was the problem. Server side code still runs, and controls still render inside of html comments. No harm in that, right? The controls can’t do anything because they are in comments.

Wrong. These were validation controls. They added javascript events to the page. Those javascript events expected dom elements that didn’t exist.

You can argue that this shouldn’t have come up in the first place, as I think most ASP.Net developers know to use server side comments, however, I thought the result was interesting.

Also, it was interesting to note that if you had some boiler plate comment you wanted in your source, you could use asp to deliver the content…

    <!--
      #both of these are valid
      #response.write version
    <%=Resources.Resource1.Copyright%>

     #or server control
     <asp:Literal Id="ltlCopyright" Text='<$Resource: Resources1, Copyright >'
                                      Runat="server" />
    -->

SilverLight and Z-Index

July 7th, 2009

When implementing a new SilverLight custom control, it was blocking menu popups (html / javascript) on the page. I set the z-index of the div and object tags that contained SilverLight to no avail. After a lot of googling, I found the following solution.

Set the Windowless property to true. In the case of manually adding SilverLight to the page, that results in the following child tag of the object tag…

<param name="Windowless" value="true" />