Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4016560
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:45:17+00:00 2026-05-20T09:45:17+00:00

I am building in some custom javascript functionality in an existing ASP.NET usercontrol. The

  • 0

I am building in some custom javascript functionality in an existing ASP.NET usercontrol. The usercontrol needs to know about a property of the control it is nested in. So, I ended up opting for using an expando attribute rather than a global javascript variable like this:

Page.ClientScript.RegisterExpandoAttribute(Me.ClientID, "validatorsenabled", Me.ValidatorsEnabled)

However, although the expando is output to the client properly, the usercontrol itself doesn’t have a corresponding element in HTML. So, the following JavaScript that is autogenerated by ASP.NET gets an “object is null” error:

var ctl00_MainContentHolder_Payment_CreditCardInput1 = document.all ? document.all["ctl00_MainContentHolder_Payment_CreditCardInput1"] : document.getElementById("ctl00_MainContentHolder_Payment_CreditCardInput1");
ctl00_MainContentHolder_Payment_CreditCardInput1.validatorsenabled = "False";

I did a little investigating and discovered this page indicating that implementing the IWebPart interface might do it, but I tried with no luck.

Is there any way to make a usercontrol output a tag like a server control? Or is the only option converting the entire thing to a server control (which goes against the grain of the website design in this case)?

I am open to other suggestions for declaring a shared JavaScript property in a user control if anyone has any other ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:45:17+00:00Added an answer on May 20, 2026 at 9:45 am

    I found a better solution listed after my original one

    I found a workaround.

    It seems (by the lack of response and lack of results when I search for the term) that the vast majority of people are unaware of expando attributes and their usefulness. You know when you add validation controls to an ASP.NET page and you get that rather large looking block of JavaScript at the bottom of the page which sets a bunch of properties (such as enabled and errormessage)? Those are what I am referring to. They can be output to the page using the Page.ClientScript.RegisterExpandoAttribute() method – essentially they are to make your markup valid HTML, while allowing you to add additional (meaningful) properties for use with javascript. They are added and can be accessed through the HTML DOM, but they are not actually there in the HTML.

    Anyway, back to my solution. I simply added 2 literal controls to the usercontrol – one at the very beginning, and one at the very end, and in codebehind I added the markup for a control element with the ID of the usercontrol (since a usercontrol doesn’t output one). So the markup looks like this:

    <%@ Control Language="VB" AutoEventWireup="false" CodeFile="CreditCardInput.ascx.vb" Inherits="BVModules_Controls_CreditCardInput" %>
    <asp:Literal ID="litControlBegin" runat="server" />
    
    <!-- User Control Content Here -->
    
    <asp:Literal ID="litControlEnd" runat="server" />
    

    Then in codebehind, I did this:

    Me.litControlBegin.Text = String.Format("<div id=""{0}"" class=""creditcardinput"">", Me.ClientID)
    Me.litControlEnd.Text = "</div>"
    

    Now an element will exist in the HTML that corresponds to the ID (ClientID) of the UserControl. Which essentially gives me a unique namespace to add expando attributes – and a way for other controls to easily identify these attributes and associate them with my usercontrol. The expando attribute is then added using this code:

    Page.ClientScript.RegisterExpandoAttribute(Me.ClientID, "validatorsenabled", Me.ValidatorsEnabled)
    

    Which no longer crashes, thanks to the HTML element I added manually. It would be nice if the plumbing were there in a usercontrol to take care of this detail like it is with a server control, but this workaround is a suitable substitute.

    Better solution

    As always, when you have to resort to string parsing you should take a closer look whether there is a better way. This time there happened to be one – override the Render method (just like a server control would) and use the built-in methods on the HtmlTextWriter to produce the output.

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
        writer.RenderBeginTag(HtmlTextWriterTag.Div)
        MyBase.Render(writer)
        writer.RenderEndTag()
    End Sub
    

    This is functionally equivalent to the code in my other solution – it produces a DIV tag with the ID of my usercontrol. However, it doesn’t require extra code to be in Page_Load like the original did, utilizes the framework better, and doesn’t require any string formatting or concatenation.

    Now, this line will add an expando attribute (in javascript) directly to the usercontrol:

    Page.ClientScript.RegisterExpandoAttribute(Me.ClientID, "validatorsenabled", Me.ValidatorsEnabled)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a custom control in Silverlight by deriving from ContentControl and doing some
I'm building some custom tools to work against a JIRA install, and the exposed
When building some of my PHP apps, a lot of the functionality could be
We're building some software for an in-house Kiosk. The software is a basic .net
I'd like to create a custom control in javascript. The goal is to create
I'm building some functional tests for a web site using Selenium, and I'm stuck
I'm looking at building some web user controls with an eye toward re-use, but
Is there a compression API available for use on the iPhone? We're building some
Building the same project (without any changes) produces binary different exe-files: some small regions
Some time ago I got this error when building ANY Visual Studio Deployment project.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.