Total Pageviews

Friday, March 25, 2011

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

Bummer. I've been mucking around with some more custom databinding that integrates validation into the databinding process. One of the things the control does is automatically add 'notification' icons or error text to the page. The idea is this:

 

  • Control(s) unbind
  • If there are binding errors an Icon or Message is injected into the Control Collection

 

Basically the code looks something like this:

public bool AddBindingError(string ErrorMessage, wwDataBindingItem BindingItem)

{

    // *** Associated control found - add icon and link id

    if (BindingItem.ControlInstance != null)

        this.BindingErrors.Add(new BindingError(ErrorMessage, BindingItem.ControlInstance.ClientID));

    else

    {

        // *** Just set the error message

        this.BindingErrors.Add(new BindingError(ErrorMessage) );

        return false;

    }

 

    BindingItem.BindingErrorMessage = ErrorMessage;

 

    // *** Insert the error text/icon as a literal

    if (this.ShowBindingErrorsOnControls && BindingItem.ControlInstance != null)

    {

        LiteralControl Literal = new LiteralControl(this.GetBindingErrorMessageHtml(BindingItem));

        int CtlIdx = BindingItem.ControlInstance.Parent.Controls.IndexOf(BindingItem.ControlInstance);

        try

        {

            // *** Can't add controls to the Control collection if <%= %> tags are on the page

            BindingItem.ControlInstance.Parent.Controls.AddAt(CtlIdx + 1, Literal);

        }

        catch {;}

    }

 

    return true;

}

 

And it works beautifully in most situations. The literal control creates markup that loads the appropriate image and adds the error text (if configured that way).

 

This all works great until you happen to have some script markup on the page using. For example if the page contains something as simple as this:

 

<%= DateTime.Now    %>

 

The Controls.AddAt() call will fail with:

 

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

 

Now I feel like a complete schmuck to never have run into this before. That's one hell of an assumption to miss about ASP.NET it seems. I have tons of pages where there's <%= %> markup on it. Especially in script code to get the appropariate ClientID:

 

<%= this.txtCompany.ClientID %>

 

Luckily there's a workaround for code like this by using DataBinding expressions instead:

 

<%# this.txtCompany.ClientID %>

 

This works fine for simple expressions. The difference here is that <%= %> expressions are embedded into the ASP.NET output as part of the generated Parse Tree class, whereas the <%# %> expressions are embedded at runtime.

 

Another workaround is to force any script code into the content of a server control and remove it from the Page class or the Content container that you're adding controls to.

 

   <div runat="server">

    <script type="text/javascript">

      function ShowCreditCard()

      {

         var IsPayPal = false;

         for(x=0; x<4; x++ )

         {

                var ctl = $("<%= this.txtCCType.ClientID %>_" + x.toString());

                if (ctl == null)

                     break;

               

                if (ctl.value == "PP" && ctl.checked)

                {

                     IsPayPal = true;

                     break;

                }

         }

        

         var loCC = $("<%= this.trCreditCard.ClientID %>");

         if (loCC == null)

            return;

         var loCC2 = $("<%= this.trCreditCardExpiration.ClientID %>");

 

         if (IsPayPal)

         {

            loCC.style.display = "none";

            loCC2.style.display = "none";

         }           

         else

         {

            loCC.style.display = "";

            loCC2.style.display = "";

         }

      }

</script>

</div>

 

This works as well, although this is also pretty ugly. In my case this is probably the easier solution though, because most of my markup expressions are doing exactly what's happening above: Embedding ClientScript Ids into JavaScript.

 

I still don't see why the control collection can't be modified if there are <% %> blocks on the page. Those blocks are just turned into Response.Write() commands, or raw code blocks. I don't see how this affects the Controls collection that would require this sort of error.

 

In my situation here I was able to get by just switching to <%# %> or wrapping sections with a server tag. Even if that's not an option the above code captures the error and goes on. This means the warning icons don't show up, but the rest of the error handling showing the summary and error control linking etc. all still works.

 

Can anybody think of another more reliable way to inject markup into the page dynamically from outside of the control rendering? In a previous rev of my databinding tools I had custom controls and I simply took over post rendering which was reliable. But this is not so easily done externally… I can think of possibly hooking up the Render method and calling back into my custom control, but man does that seem ugly.

Thursday, March 24, 2011

BE THE CEO OF YOU INC.


BE THE CEO OF YOU INC.  
 
 
 

Just off the tour as I write this. It was a genuine joy to meet so many LWTs (Leaders Without Titles) on the tour stops in Germany + Norway + Kuwait + Qatar + India + Dubai + South Africa. Which brings me to one event in particular, as it relates to the 6 leadership ideas I want to share.

DU Telecom, the upstart telco in Dubai, that in just four years has become truly world-class, brought me in to speak at a special function for a few hundred of their CEO clients. My job was to inspire them, to challenge them and to help them get their leadership genius to its next level of excellence. As I finished, I offered them 6 challenges:

1. GET OUT AND EXPLORE: Extracurricular activities are not a waste of time. The best leaders are interesting people. They pursue passions. They love art. They experience unforgettable travel. And they engage in conversations with fascinating people. This allows them to stay inspired. And hungry. And offers them a steady stream of ideas that actually makes their businesses more successful.

2. LISTEN TO THE PEOPLE WITH THE DIRTIEST HANDS: Want to know what your customers love - and can't stand - about your business? Listen to the people on the front line. The grocery clerk hears exactly what's being said about the products on the shelves. The person answering the phone knows what people are most dissatisfied with. The technician gets exactly what needs to be fixed for the brand to grow. Learn to listen to the people who are closest to your customers.

The data they carry is priceless.

3. YOU ARE PAID NOT ONLY TO WORK. YOU ARE PAID TO BE SCARED:
  It's easy to do what you do every day until it becomes second nature. But what leadership's truly about is having the courage to out-think + out-perform who you were yesterday. And that's scary. Because you need to consistently do what's uncomfortable. But all growth lies on the outer edges of your comfort zone. Commit to not just doing your work but accepting the challenges that frighten you.

4. HEALTH IS YOUR WEALTH: These were peak-performing CEOs leading 24/7 careers. There was utter silence in the room when I shared this statement: "Health is the crown on the well man's head that only the ill person can see". Why be the richest person in the graveyard? And what's the point of getting to the mountaintop but reaching it sick? Get serious about becoming superfit. Then watch the caliber of your work and the quality of your life fly.

5.  FAMILY FIRST: With children, we have a little window of opportunity. And once it closes, it's very hard to open it up again. Having a strong family foundation of deep relationships with those you love makes you a more effective businessperson. And who wants to get to the end of your career and realize you're all alone?

6. BE VALUABLE: Business, to me, is nothing more than a breathtakingly great vehicle to deliver unusual value to as many people as possible. Want to double your sales? Then double the value you bring to your customers. And the whole game of life's about much of the same thing: contribution. Being of value. Making a difference. No one on their deathbed wished they had made more money. Most of us do wish we have had a greater impact. As a seminar participant in Qatar shared with me: "The measure of the greatness of a person is the length their shadow casts on the future."

Why do I share the 6 challenges I offered to the CEO crowd with you? Because you are the CEO of your own career and the Leader of your own life. I encourage - and challenge - you to reflect on these ideas and then to act on them with speed. There's never been a bigger need for leaders in our organizations and within our world. And whether you have a title or not, that need applies to you.

Keep Leading Without a Title,
 
 
   

Friday, March 4, 2011

FAITH & CONFIDENCE UNDAMAGED

A businessman lost everything in fire. Next day Placed sign board "EVERYTHING BURNT but luckily FAITH & CONFIDENCE UNDAMAGED Business starts TOMORROW"....

Think

Think Big,Think Fast,Think Ahead. Ideas are no one's monopoly.