views:

60

answers:

1

I have an ASP.NET server control that inherits from CheckBoxList that renders the controls with a UL and LIs. The control is set to AutoPostBack.

Markup:

<div id="foo">
    <ul>
        <li>
            <input id="..." 
                   type="checkbox" 
                   name="ctl00$MainContent$FooList$0"    

                   onclick="javascript:setTimeout(
                   '__doPostBack(\'ctl00$MainContent$FooList$0\',\'\')', 0)" />

            <label ...>...</label>
        </li>
        ...
    </ul>
</div>

I would like to trigger the JavaScript that is rendered on the CheckBox when the parent LI is clicked. Here is what I have so far:

$("#foo li").click(function() {
    $(this).find("input:eq(0)").trigger("click");
});

This fires a postback in FF 3.x but the event handler in the codebehind is not fired. In IE 7 a script error comes up and the browser just kind of hangs there for a bit then reloads the page.

How should I be doing this?

+1  A: 

I'd give this a try:

$("#foo li").click(function() {
    $(this).find("input:eq(0)").triggerHandler("click");
});

A trigger on a child will bubble to the parent, resulting in a loop in this case since the parent click is again firing the child click. Firefox sometimes recognizes and stops this, but the result is your box is checked, it bubbles, it's unchecked and so on postback, it's not firing because the check status really didn't change.

.triggerHandler() however won't bubble, and might resolve your issue (unless there's another script error occuring!). As a general rule, use this when triggering the handlers on a child, especially triggering the same event on the child that would bubble.

Nick Craver
The script fires properly in both browsers now, thanks for the explanation. Unfortunately the ASP.NET event handler is not being triggered. I don't know if ASP.NET is your thing, but if it is any ideas?
blu
@blu - Are you clicking on the checkbox when this happens, or the `<li>`? If clicking on the `<li>` this would still toggle the checkbox, bubble up once (the original click) and toggle it back...does it work when clicking anywhere in the `<li>` that's *not* the checkbox?
Nick Craver
@Nick Craver - The checkbox functions normally. Clicking the li causes a postback, does not toggle the selected state of the CheckBox, and does not cause the EventHandler to fire.
blu
@blu - Can you update with the full version of your input's onclick?
Nick Craver
@Nick Craver - I updated the existing markup in the question. Thanks for looking at it.
blu
@blu - Give this a try for your handler: `$("#foo li").click(function() { __doPostBack($(this).find('input:first').attr('name'),''); });`
Nick Craver
@Nick Craver - I gave that try and had the same results. If you have any other thoughts I'd be happy to try it out.
blu
@blu - One more shot: `$("#foo li").click(function() { var name = $(this).find('input:first').attr('name'); setTimeout(function() { __doPostBack(name,''); }, 0); });`
Nick Craver
@Nick Craver - I went one step further down the path and just took the rendered JS from the checkbox and put it in the click handler for the li. The same thing happens. Oddly, when I click on the checkbox now the server side event handler fires twice, but when the li is clicked the handler is not called.
blu
@blu - The check changed shouldn't fire twice...are you using an update panel, and it is the oncheckchanged event correct?
Nick Craver
@Nick Craver - So I tried this out in a trivial side project, and I could reproduce not being able to trigger another control's postback directly from JS. I think you answered my initial question though and that the JQuery is doing what I wanted at this point. I may open another question more specific to the ASP.NET portion of the issue. Thanks for your help.
blu
@blu - Welcome! if you open another question with the aspx markup and code please comment here, I'll definitely take a look.
Nick Craver
I posted a new question: http://stackoverflow.com/questions/2655984/asp-net-call-another-elements-dopostback-function
blu
@blu - Is the label on the checkbox negotiable? :) the jQuery to handle this properly is *much* simpler if there's no `<label for="">` involved.
Nick Craver