views:

32

answers:

2

I'm having some trouble with jQuery UI. I've created a dialog with jQuery UI: so far, so good. I set up a form inside the jQuery UI dialog and then it's another story. I've written a very simple page to illustrate:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testpage.aspx.cs"     Inherits="RiverCascade.testpage" %>
<head runat="server">    
<link rel="Stylesheet" type="text/css" href="Stylesheet1.css" />
<link rel="Stylesheet" type="text/css" href="css/smoothness/jquery-ui-1.8.5.custom.css" />
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.5.custom.min.js"></script>
<script type="text/javascript">
function showModal() {
    $("#modalBox").dialog();
}
$(document).ready(showModal);    
</script>
</head>
<body>
<form id="form1" runat="server">
    <div>    
    <asp:Button runat="server" ID="button1" OnClick="button1_click" />                
        <div style="display:none" id="modalBox">
        <asp:Button runat="server" ID="button2" OnClick="button2_click" />        
        </div>

    </div>
    </form>
</body>
</html>

In the code above, see that I've just imported the jQuery, jQuery UI, and stylesheets and set up a page with a dialog box. There is an asp control button1 outside the dialog box and an asp control button2 inside the dialog box.

When I click on button1, the event handler gets called and all is well. However, when on click on button2, the button inside the jQuery dialog, nothing happens.

Why is my web page behaving this way? How can I fix it?

+1  A: 

The following is from the jQuery UI Dialog source (you gotta scroll pretty far down).

// ... more code here ...
uiDialog = (self.uiDialog = $('<div></div>'))
            .appendTo(document.body)
            .hide()
            .addClass(uiDialogClasses + options.dialogClass)
// ... more code here ...

Basically the reason you're seeing the behavior that you are because jQuery Dialog creates a new element and attaches it to the body of the page (outside the ASP.NET form). Since your button is now outside the ASP.NET form it won't cause regular submits.

Tim B James's solution should cover up the problem.

R0MANARMY
+1: Thanks for your help. However, once I get the button working, I add some text boxes inside the dialog box. Guess what? They are all blank when I submit the form? What about that?
Rice Flour Cookies
@Rice Flour Cookies: Same reason, those textboxes are not inside the form tag when the form is submitted. Since they aren't in the form, their data isn't submitted. A lot of people have had [similar](http://stackoverflow.com/search?q=jquery-ui+dialog+form+asp.net) problems. Sorry I can't help more, I haven't had to deal with this in a while and don't remember what the best solution is.
R0MANARMY
To Solve the textbox issue, I would create a whole new set of textboxes outside the Dialog, but hidden. Then when you fill in the textboxes within the Dialog, it will push the values into the other textboxes. When submitting the form then, it will have values that you can get.
Tim B James
@Tim B James: That's one approach, I would probably go with the approach taken [here](http://stackoverflow.com/questions/757232/jquery-ui-dialog-with-asp-net-button-postback) and have jQuery UI attach the dialogs to the form instead of to the body.
R0MANARMY
Ah that is a nice solution! :)
Tim B James
+1  A: 

try adding this to button2

UseSubmitBehavior="false"
Tim B James