How to use multiple instances of a usercontrol (with jquery) on the same page

Posted by Julian on Stack Overflow See other posts from Stack Overflow or by Julian
Published on 2010-05-19T16:19:44Z Indexed on 2010/05/19 16:20 UTC
Read the original article Hit count: 433

Filed under:
|
|
|

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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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"></script>
    <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" />
</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.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about usercontrols