ross dickinson: web and desktop software developer

EnsureID, ClientID and ClientIDMode

by Ross on September 16, 2011 at 11:39 AM under ASP.Net

Noticed this annoying quirk today while working on a custom WebForms control. Calling EnsureID will not populate the ClientID property if the control's effective ClientIDMode is set to Static. No idea why! EnsureID will still populate the ID properties, but if you need the ClientID for client-side javascript, you'll need to find other ways around. I opted to go this route:

public override string ClientID
{
    get
    {
        EnsureID();
        if (!string.IsNullOrWhiteSpace(ClientID))
        {
            return ClientID;
        }
        if (!string.IsNullOrWhiteSpace(ID))
        {
            return ID;
        }
        throw new InvalidOperationException("Where's the ClientID for this DateTimePicker?");
    }
}

ASP.Net 4 ClientIDMode and Validators

by Ross on March 24, 2011 at 1:51 PM under .NET | ASP.Net

Just a quick note. ASP validators(like RequiredFieldValidator) do not work when validator's ClientIDMode is set to a value other than AutoID. This happens either by explicitly setting a value, or the validator inheriting it from a parent control. The only work around, so far, is to make sure your validator has AutoID set.

About the author

rossisdead is a 26 year old web and desktop software developer from New Jersey. He has two cats and likes long walks on the beach.

On Stackoverflow

On Stackoverflow Careers

On Codeplex

On Github