Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Button Stopwatch

Status
Not open for further replies.

bam720

Technical User
Sep 29, 2005
289
US
I'm still very new to asp. I'm trying to create a page that has a few buttons on it. The first is a start/stop button. When you click it it starts a stopwatch timer. When you click it again, the timer is stopped. Ideally a label displays the clock while it is running. I'm coming from a CS world, and I have the changing start to stop, however, when I'm clicking the button the page "refreshes" and I lose the timer data. My guess is something to do with view state, but not really sure. Any help qould be greatly appreciated.

Page Code
Code:
<%@ Page Language="C#" MasterPageFile="Login.master" AutoEventWireup="true" CodeFile="roping_page.aspx.cs" Inherits="roping_page" Title="NCS Roping" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainBody" Runat="Server">

    <div style="text-align: center">
        NCS Roping Event<br />
        <asp:SiteMapPath ID="SiteMapPath1" runat="server" Font-Names="Verdana" Font-Size="0.8em"
            ParentLevelsDisplayed="1" PathSeparator=" : ">
            <PathSeparatorStyle Font-Bold="True" ForeColor="#5D7B9D" />
            <CurrentNodeStyle ForeColor="#333333" />
            <NodeStyle Font-Bold="True" ForeColor="#7C6F57" />
            <RootNodeStyle Font-Bold="True" ForeColor="#5D7B9D" />
        </asp:SiteMapPath>
        <br />
        <asp:Menu ID="Menu1" runat="server" BackColor="#FFFBD6" DataSourceID="SiteMapDataSource1"
            DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000"
            StaticSubMenuIndent="10px">
            <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <DynamicHoverStyle BackColor="#990000" ForeColor="White" />
            <DynamicMenuStyle BackColor="#FFFBD6" />
            <StaticSelectedStyle BackColor="#FFCC66" />
            <DynamicSelectedStyle BackColor="#FFCC66" />
            <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <Items>
                <asp:MenuItem Text="Roping Mgmt" Value="Roping Mgmt">
                    <asp:MenuItem Text="New Roping" Value="New Roping"></asp:MenuItem>
                    <asp:MenuItem Text="Match Mgmt" Value="Match Mgmt"></asp:MenuItem>
                    <asp:MenuItem Text="Progressive Settings" Value="Progressive Settings"></asp:MenuItem>
                </asp:MenuItem>
                <asp:MenuItem Text="Roping DB Admin" Value="Roping DB Admin">
                    <asp:MenuItem Text="Clean Up DB" Value="Clean Up DB"></asp:MenuItem>
                </asp:MenuItem>
                <asp:MenuItem Text="Roping Payout" Value="Roping Payout">
                    <asp:MenuItem Text="Calculate Payout" Value="Calculate Payout"></asp:MenuItem>
                </asp:MenuItem>
                <asp:MenuItem Text="Roper Mgmt" Value="Roper Mgmt">
                    <asp:MenuItem Text="Add/Edit Roper" Value="Add/Edit Roper"></asp:MenuItem>
                </asp:MenuItem>
            </Items>
            <StaticHoverStyle BackColor="#990000" ForeColor="White" />
        </asp:Menu>
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />
        <br />
        <asp:GridView ID="GridView" runat="server">
        </asp:GridView>
        <br />
        <br />
 [b]           <asp:Label ID="Clock" runat="server" Text="00:00:00"></asp:Label>
            <br />
            <asp:Button ID="btnClock" runat="server" OnClick="Start_Click" Text="Start" /><br />
            <asp:Button ID="OneLeg" runat="server" Text="OneLeg" OnClick="OneLeg_Click" UseSubmitBehavior="False" /><br />
            <asp:Button ID="NoShow" runat="server" Text="NoShow" UseSubmitBehavior="False" /><br />
            <br />
            <asp:Button ID="Post" runat="server" Text="Post" OnClick="Post_Click" /><br />
            <br />[/b]
        </div>
</asp:Content>




C# Code

{
    Stopwatch SwClock = new Stopwatch();

    protected void Start_Click(object sender, EventArgs e)
    {

        if (btnClock.Text == "Start")
        {
            SwClock.Reset();
            SwClock.Start();
            btnClock.Text = "Stop";
        }
        else
        {
            SwClock.Stop();
            btnClock.Text = "Start";
        }
        string time = SwClock.Elapsed.ToString();
        Clock.Text = time;
    }
 
when I'm clicking the button the page "refreshes" and I lose the timer data. My guess is something to do with view state, but not really sure. Any help qould be greatly appreciated.

Any server side button will always cause a post back. You can use an HTML button to start the timer using javascript. Also, I am sure there are AJAX solutions as well.
 
Hi there,

You could use an AJAX update panel and timer which will only cause whatever is in the panel to postback. this may help you.

Regards

Alex

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top