ASP.NET Validator is removing client onChange event handler for custom server control

Discussion in 'ASP .Net' started by Lewis Holmes, Mar 10, 2009.

  1. Lewis Holmes

    Lewis Holmes Guest

    Hi

    I have developed a custom server control that basically renders one or more
    drop down lists (select html controls) to display a hierarchical lookup
    list. So for example you may have lookup data structure that is made up of
    3 levels and each level is displayed as a seperate drop down on my web form.
    If the user changes the selected value for one of the drop downs, some
    javascript executes which dynamically populates the data for the next level
    of the hierarchy. This JavaScript is added to the onChange event of the html
    select control (drop down).

    So for example you may have 3 levels defined as "Car Manufacturer", "Model"
    and "Trim". After selecting a car manufacturer, the models available for
    that selected manufacturer are then loaded into the Model drop down using
    JavaScript.

    This all works nicely but then when I add a Compare validator to my web form
    dynamically and set it to validate this custom server control, it appears as
    if my onChange JavaScript event handler script is being removed. I have been
    using IE Developer Toolbar to inspect the DOM and when I dynamically add the
    validator, the onChange I already have is being removed. I am 99% sure it is
    the presence of the validation control that is removing my onChange
    JavaScript.

    I decided to have a look into the ASP.NET validation JavaScript and I can
    see there is a function that hooks into the onchange events for controls to
    make sure the validation script runs when a values is changed. I think this
    is the JavaScript that is removing my onChange script.

    function ValidatorHookupEvent(control, eventType, functionPrefix) {
    var ev;
    eval("ev = control." + eventType + ";");
    if (typeof(ev) == "function") {
    ev = ev.toString();
    ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
    }
    else {
    ev = "";
    }
    var func;
    if (navigator.appName.toLowerCase().indexOf('explorer') > -1) {
    func = new Function(functionPrefix + " " + ev);
    }
    else {
    func = new Function("event", functionPrefix + " " + ev);
    }
    eval("control." + eventType + " = func;");
    }

    Please could somebody provide me with some help with this. I am not sure why
    my onChange script is being removed and cannot figure out how I can work
    around this.
     
    Lewis Holmes, Mar 10, 2009
    #1
    1. Advertising

  2. Hi Lewis,

    My name is Allen Chen. It's my pleasure to work with you on this issue.

    From your description, when CompareValidator is used the JS function hooked
    to onchange event doesn't fire, right?

    I did a simple test but it worked fine. Here's my test code:

    Aspx:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script type="text/javascript">
    function Test() {
    var d = new Date();
    document.getElementById("input1").value = d.getTime();
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    This input is for test. Don't input value in it.
    <input id="input1" />
    <br />
    TextBox1:
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    TextBox2:
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:CompareValidator ControlToValidate="TextBox1"
    ControlToCompare="TextBox2" ValueToCompare="Text"
    ID="CompareValidator1" runat="server"
    ErrorMessage="CompareValidator"></asp:CompareValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
    </body>
    </html>

    Aspx.cs:
    protected void Page_Init(object sender, EventArgs e)
    {
    this.TextBox1.Attributes.Add("onchange", "Test()");
    this.TextBox2.Attributes.Add("onchange", "Test()");

    }

    When you input value into TextBox1 and TextBox2 you can see the Test()
    function is always called when the value is changed.

    Could you compare your code with mine to see if there's any difference? You
    can also send me a demo that can reproduce this problem. My email is
    . Please update here after sending the project in case
    I missed that email.

    Regards,
    Allen Chen
    Microsoft Online Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 2 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions. Issues of this
    nature are best handled working with a dedicated Microsoft Support Engineer
    by contacting Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Allen Chen [MSFT], Mar 11, 2009
    #2
    1. Advertising

  3. Hi Lewis,

    Do you have any progress on this issue? Have you sent me the project?

    Regards,
    Allen Chen
    Microsoft Online Support
     
    Allen Chen [MSFT], Mar 13, 2009
    #3
  4. Hi Lewis,

    Do you have any progress on this issue? Have you sent me the project?

    Regards,
    Allen Chen
    Microsoft Online Support
     
    Allen Chen [MSFT], Mar 13, 2009
    #4
  5. Lewis Holmes

    Lewis Holmes Guest

    Hi Allen

    Thanks for your reply. I agree that I ned to reproduce this in a sample
    application but that will take some time due to complexity of the code
    involved. I will try and strip the code down to the bare minimum needed to
    reproduce the issue.

    I am aiming to spend sometime to do this next week so I should be able to
    send you a sample then. I wish it could be quicker but I have other work
    that needs to be completed urgently.

    Regards

    Lewis

    "Allen Chen [MSFT]" <> wrote in message
    news:...
    > Hi Lewis,
    >
    > Do you have any progress on this issue? Have you sent me the project?
    >
    > Regards,
    > Allen Chen
    > Microsoft Online Support
    >
     
    Lewis Holmes, Mar 13, 2009
    #5
  6. Hi Lewis,

    Just check back if you've created the demo. Please let me know if you've
    done it.

    Regards,
    Allen Chen
    Microsoft Online Support
     
    Allen Chen [MSFT], Mar 18, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TW9ua2V5Qm95?=
    Replies:
    2
    Views:
    443
    =?Utf-8?B?TW9ua2V5Qm95?=
    Jun 6, 2004
  2. Replies:
    1
    Views:
    705
    Damien
    Feb 22, 2007
  3. Zoe Hart
    Replies:
    1
    Views:
    371
    Scott Wisniewski
    Jan 8, 2004
  4. Bill
    Replies:
    1
    Views:
    155
    Thomas 'PointedEars' Lahn
    Oct 23, 2003
  5. David McNerney
    Replies:
    4
    Views:
    171
    David McNerney
    Apr 7, 2006
Loading...

Share This Page