Question

I have a page where user controls are loaded dynamically into a div/panel. I don't know anything about the content of these user controls, besides they have a button.

When the user is inside this div / panel (using the user control), I want the button inside the div/panel, to be the default button. Right now, my sidewide search button is the default button no matter what.

I can't add "defaultButton=mybutton" in the user controls, and I can't add a class to the buttons.

How do I solve the problem?

Thanks :)

Was it helpful?

Solution

I ended up doing the following:

 $("#CalculatorDiv").keypress(function (e) {
                if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                    var div = $("#CalculatorDiv");
                    var firstInput = div.find("input[type=submit]");
                    firstInput.click();
                    return false;
                } else {
                    return true;
                }
            });

:-) So a combination of find and keypress event.

OTHER TIPS

You have to be more specific. Better yet, post the code.

By default button, do you mean pressing "Return" will make the button submit? In that case, you can enclose it in a form.

To access the button, you can do that via javascript's document.getElementById

https://developer.mozilla.org/en-US/docs/DOM/document.getElementById

You can also use jQuery to access the element by class, type or nearest button element to the div tag. Maybe use the "find" method in jQuery:

http://api.jquery.com/find/

I'm not certain how you are accomplishing this based upon your description but what you could do is set the Control.ID and then you can use FindControl to locate the UserControl.

UserControl uc = (UserControl)PANELID.FindControl(Control.ID);

I'm not sure, but can't you search for <input type="button" .../> and than add the default code part to it?


EDIT: read your question wrong, asumed it was a webpage 'cuz of the 'div'.
Normally in C#, you can select the Controls inside a panel and then search on the type to find the button

$("your div").children().blur(function(){
$(#button).focus();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top