views:

122

answers:

3

Hi, my question is really simple. I have a asp.net button. I can use it to call the simpleModal and have a dialog displayed. Now, I added a label control in the dialog, and would like this label to display some value. What should I do?

Here is my codes

$('#<%= btnOpen.ClientID %>').click(function(e) {
            e.preventDefault();

            $('#content').modal({
                onOpen: function(dialog) {
                    dialog.overlay.fadeIn('slow', function() {
                        dialog.data.hide();
                        dialog.container.fadeIn('slow', function() {
                            dialog.data.slideDown('slow');

                        });
                    });
                },
                onClose: function(dialog) {

                    dialog.data.fadeOut('slow', function() {
                        dialog.container.slideUp('slow', function() {
                            dialog.overlay.fadeOut('slow', function() {
                                $.modal.close(); // must call this!

                            });
                        });
                    });
                }

            });
            e.preventDefault();
            // return false;
        });
<asp:Button ID="btnOpen" runat="server" Text="ASP.NET Open"/>

<div id="content" style="display: none;">

    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

</div>
+1  A: 

I assume since you said that your question is simple that you just have an unfamiliarity with jQuery. You can put this in your click function, or in the $(document).ready function, depending on your full requirements:

var yourValue = ; // put your function or value here
$('#Label1').text(yourValue);

Note: You'll need to use .html instead of .text if you have a string with tags, but .text is faster.

mVChr
+1  A: 

Lol, I am answering my own question again, but I will give credit to mNVhr tho.

I finally get the whole thing work. The trick for asp.net button to fire a postback, along with javascript's postback, is to put the asp.net button into an update panel. Here is the code I have

For the javascript part:

<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>

<script src="js/jquery.simplemodal-1.3.5.js" type="text/javascript"></script>

<script type="text/javascript">

    function myOpen() {
        $('#content').modal({
            onOpen: function(dialog) {
                dialog.overlay.fadeIn('slow', function() {
                    dialog.data.hide();
                    dialog.container.fadeIn('slow', function() {
                        dialog.data.slideDown('slow');

                    });
                });
            },
            onClose: function(dialog) {

                dialog.data.fadeOut('slow', function() {
                    dialog.container.slideUp('slow', function() {
                        dialog.overlay.fadeOut('slow', function() {
                            $.modal.close(); 

                        });
                    });
                });
            }

        });


    }

    function myClose() {
        $.modal.close();


    }


</script>

For the HTML markup

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="btnOpen" runat="server" Text="Open" OnClick="btnOpen_Click" OnClientClick="myOpen();" />
    </ContentTemplate>
</asp:UpdatePanel>
<div id='content' style="display: none">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
            <input id="Button2" type="button" value="Close" onclick="myClose();" />
            <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

For the code behind:

 protected void Page_Load(object sender, EventArgs e)
{

}
private void CloseDialog()
{

    string script = string.Format(@"myClose()");
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
}
protected void btnSave_Click(object sender, EventArgs e)
{
    if (TextBox1.Text == "1")
        CloseDialog();
    else
        Label2.Text = TextBox1.Text;

}
protected void btnOpen_Click(object sender, EventArgs e)
{
    TextBox1.Text = DateTime.Now.ToString();
    UpdatePanel1.Update();
}

I hope this tiny code can help those asp.net developer who want to use the nice jQuery in their projects.

+1  A: 

As you can see, from the above codes.

When I click on the btnOpen button, two postbacks fired. One is from the asp.net code behind, which assign current datetime to the textbox control inside the modal dialog. The second postback is from the javascript, which open the modal dialog. The asp.net button has to be inside the update panel. Otherwise, the modal dialog will only stay for about 0.5 second.

When I click on the btnSave inside the modal dialog. Postback also occurred. I have a little logic here. When the textbox's value is 1, I call the closeDialog() function. When the value is other numbers, the modal dialog stay opening, and the label control inside the dialog will display the number from the text box.

jQuery is nice, but as a .Net developer, it is just new, and sometimes difficult for me to understand it, especially for the conflict of postbacks between javascript and .net.

I hope this answer is helpful.

If you are using .NET, make sure to add the appendTo option on the modal call:$('#content').modal({ appendTo: 'form', ...})
Eric Martin