views:

116

answers:

2

Hi All,

I've been working on an usercontrol with a jquery timer in it. At first I had the jquery within the usercontrol. But when I add 2 of those controls to my page the second usercontrol isn't showing it's data very well.

Now I've put the jquery into the mainpage and the usercontrol only uses the id's of the jquery.

Here is my usercontrol:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"
    Inherits="WebUserControl" %>
<style type="text/css">
    #mainpanel
    {
        width: 145px;
        height: 123px;
    }
</style>
<div id="mainpanel">
    <div>
        test</div>
    <div id="shortly" style="background-color: #FFFFFF">
    </div>
    <button id="resetButton">
        Reset
    </button>
</div>

Mainpage:

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head runat="server">
        <title>hoi</title>
        <script src="Scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
        <script src="Scripts/jquery-ui-1.8.1.custom.min.js" type="text/javascript"></script>
        <link href="Css/jquery-ui-1.8.1.custom.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"&gt;&lt;/script&gt;
        <script type="text/javascript" src="Scripts/jquery.countdown.js"></script>
        <script type="text/javascript">


            $(document).ready(function () {

            $(function() {
            $("#mainpanel");
        });

                shortly = new Date();
                shortly.setSeconds(shortly.getSeconds() + 5.5);
                $('#shortly').countdown({ until: shortly,
                    onExpiry: liftOff, layout: '{sn}', 
                });
                $('#resetButton').click(function () {
                 $('#mainpanel').effect("highlight", {}, 700 );
                 $('#shortly').effect("highlight", {}, 700 );
                    shortly = new Date();
                    shortly.setSeconds(shortly.getSeconds() + 5.5);
                    $('#shortly').countdown('change', { until: shortly });

                });


                function liftOff() {
                    // refresh the page  
                    window.location = window.location;
                }


            });

        </script>
    </head>
    <body>
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
<uc1:WebUserControl ID="WebUserControl2" runat="server" />

    </body>
    </html>

But still my usercontrols are acting weird, now my question is: "How can I make the SAME usercontrols work properly on ONE page?"

Both of those use the same jquery code but the buttons etc. should only work within the usercontrol itself.

+1  A: 

Your user control is using id="" on elements. Id's must be unique within a single html page. If you need to attach css styles or something to query via jquery, you need to use classes instead of id's so that the second control doesn't break the convention that id's must be unique.

David
The ID is unique of the control itself, forgot to paste the whole mainpage code. Editted now and you see both of the usercontrols get their own ID.
Julian
No, I mean the markup inside the usercontrol itself is setting id's on elements like <div id="shortly"></div>, you need to use classes like <div class="shortly"></div> then use jquery class selectors instead of id selectors
David
I'm going to look into this, thanks! If somebody knows other ways to do the same thing, please still comment. David, you maybe got good example?
Julian
Answers still needed people... cant figure the jquery classes out. And how to add a script to them.
Julian
+4  A: 

As David suggests, seeing as you are repeating the user controls and you are explicitly setting their id, you will end up with several controls that share the same id on the page. I imagine that $("#someid") will return the first element in the page and not necessarily the one you really wanted.

David is suggesting adding CSS classes to the elements in your user control (this has nothing to do with jQuery - although the jQuery you write will refer to them).

For example

<%@ Control Language="C#"  %>
<div class="mainpanel">
    <div>
        test
    </div>
    <div class="shortly">
        ??
    </div>
    <button class="resetButton">
        Reset
    </button>
</div>

[PS. note that you should probably remove your CSS from your user control as repeating each time the control appears on the page is bad practise e.g. include this either in your main page or a separate CSS file]

<style type="text/css">
    #mainpanel
    {
        width: 145px;
        height: 123px;
    }
    #shortly
    {
        background-color: #FFFFFF
    }
</style>

Your script can then be something like

$(function () { 
    // function that does something exciting?
    var liftOff = function() { 
        // .... 
    };

    // Get a date that is some (short) time in the future
    var getDeadline = function() {
        var shortly = new Date();
        shortly.setSeconds(shortly.getSeconds() + 5.5);
        return shortly;
    };

    // Attach click handler to all our buttons
    $("div.mainpanel button.resetButton").click(function(event){

        // I am assuming that you will not be nesting these controls?
        var $mainpanel = $(this).parents("div.mainpanel")   // this will find the mainpanel div that contains the pressed button
                .effect("highlight", {}, 700 );

        $("div.shortly", $mainpanel) // this will find any div with the class = shortly inside mainpanel
                .countdown('change', { until: getDeadline() });    
    });

    // Start all countdowns going on page load
    $('#shortly').countdown({ 
        until: getDeadline(),
        onExpiry: liftOff, 
        layout: '{sn}'
    });
});
Chris F
+1 Good job on providing a detailed example.
David
Thanks man! Really appriciate this!
Julian