Solved

javascript, asp.net

Posted on 2011-03-15
2
309 Views
Last Modified: 2012-08-14
Question:
I would like to allow user to select time in and time out.
and calcaluate the min. and land the value to
ddlmeetingdurationmin?

Does anyone know how to put it code behind / javascript frontend...?

Thanks,
<asp:Label Width="150" ID="Label1" runat="server" Text="Time In:"></asp:Label><asp:DropDownList ID="ddlmeetingstarthour" runat="server">
    <asp:ListItem Value="01" Text="01"></asp:ListItem>
    <asp:ListItem Value="02" Text="02"></asp:ListItem>
    <asp:ListItem Value="03" Text="03"></asp:ListItem>
    <asp:ListItem Value="04" Text="04"></asp:ListItem>
    <asp:ListItem Value="05" Text="05"></asp:ListItem>
    <asp:ListItem Value="06" Text="06"></asp:ListItem>
    <asp:ListItem Value="07" Text="07"></asp:ListItem>
    <asp:ListItem Value="08" Text="08"></asp:ListItem>
    <asp:ListItem Value="09" Text="09"></asp:ListItem>
    <asp:ListItem Value="10" Text="10"></asp:ListItem>
    <asp:ListItem Value="11" Text="11"></asp:ListItem>
    <asp:ListItem Value="12" Text="12"></asp:ListItem>
    <asp:ListItem Value="13" Text="13"></asp:ListItem>
    <asp:ListItem Value="14" Text="14"></asp:ListItem>
    <asp:ListItem Value="15" Text="15"></asp:ListItem>
    <asp:ListItem Value="16" Text="16"></asp:ListItem>
    <asp:ListItem Value="17" Text="17"></asp:ListItem>
    <asp:ListItem Value="18" Text="18"></asp:ListItem>
    <asp:ListItem Value="19" Text="19"></asp:ListItem>
    <asp:ListItem Value="20" Text="20"></asp:ListItem>
    <asp:ListItem Value="20" Text="21"></asp:ListItem>
    <asp:ListItem Value="20" Text="22"></asp:ListItem>
    <asp:ListItem Value="20" Text="23"></asp:ListItem>
    <asp:ListItem Value="20" Text="24"></asp:ListItem>
    </asp:DropDownList>&nbsp;
    <asp:DropDownList ID="ddlmeetingstartminute" runat="server">
    <asp:ListItem Value="00" Text="00"></asp:ListItem>
    <asp:ListItem Value="15" Text="15"></asp:ListItem>
    <asp:ListItem Value="30" Text="30"></asp:ListItem>
    <asp:ListItem Value="45" Text="45"></asp:ListItem>
    </asp:DropDownList>&nbsp;
    <asp:DropDownList ID="ddlmeetingstarttimezone" runat="server">
    <asp:ListItem Value="EST" Text="EST"></asp:ListItem>
    <asp:ListItem Value="CST" Text="CST"></asp:ListItem>
    <asp:ListItem Value="PST" Text="PST"></asp:ListItem>

    </asp:DropDownList>&nbsp;<font color="red">*</font>
<br>
<asp:Label Width="150" ID="lbltimeout" runat="server" Text="Time Out:"></asp:Label><asp:DropDownList ID="ddlmeetingendhour" runat="server">
    <asp:ListItem Value="01" Text="01"></asp:ListItem>
    <asp:ListItem Value="02" Text="02"></asp:ListItem>
    <asp:ListItem Value="03" Text="03"></asp:ListItem>
    <asp:ListItem Value="04" Text="04"></asp:ListItem>
    <asp:ListItem Value="05" Text="05"></asp:ListItem>
    <asp:ListItem Value="06" Text="06"></asp:ListItem>
    <asp:ListItem Value="07" Text="07"></asp:ListItem>
    <asp:ListItem Value="08" Text="08"></asp:ListItem>
    <asp:ListItem Value="09" Text="09"></asp:ListItem>
    <asp:ListItem Value="10" Text="10"></asp:ListItem>
    <asp:ListItem Value="11" Text="11"></asp:ListItem>
    <asp:ListItem Value="12" Text="12"></asp:ListItem>
    <asp:ListItem Value="13" Text="13"></asp:ListItem>
    <asp:ListItem Value="14" Text="14"></asp:ListItem>
    <asp:ListItem Value="15" Text="15"></asp:ListItem>
    <asp:ListItem Value="16" Text="16"></asp:ListItem>
    <asp:ListItem Value="17" Text="17"></asp:ListItem>
    <asp:ListItem Value="18" Text="18"></asp:ListItem>
    <asp:ListItem Value="19" Text="19"></asp:ListItem>
    <asp:ListItem Value="20" Text="20"></asp:ListItem>
    <asp:ListItem Value="20" Text="21"></asp:ListItem>
    <asp:ListItem Value="20" Text="22"></asp:ListItem>
    <asp:ListItem Value="20" Text="23"></asp:ListItem>
    <asp:ListItem Value="20" Text="24"></asp:ListItem>
    </asp:DropDownList>&nbsp;
    <asp:DropDownList ID="ddlmeetingendminute" runat="server">
    <asp:ListItem Value="00" Text="00"></asp:ListItem>
    <asp:ListItem Value="15" Text="15"></asp:ListItem>
    <asp:ListItem Value="30" Text="30"></asp:ListItem>
    <asp:ListItem Value="45" Text="45"></asp:ListItem>
    </asp:DropDownList>&nbsp;
    <asp:DropDownList ID="ddlmeetingendtimezone" runat="server">
    <asp:ListItem Value="EST" Text="EST"></asp:ListItem>
    <asp:ListItem Value="CST" Text="CST"></asp:ListItem>
    <asp:ListItem Value="PST" Text="PST"></asp:ListItem></asp:DropDownList>&nbsp;<font color="red">*</font>
<br>
<asp:Label Width="150" ID="lblmeetingduration" runat="server" Text="Duration (HH/MM)"></asp:Label><asp:Textbox ID="ddlmeetingdurationmin" runat="server" />

Open in new window

0
Comment
Question by:Webboy2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 35143936
heres the javascript function, it still needs work and is a bit basic at the moment, e.g. timezone is ignored.
    <script type="text/javascript" >
    function calcDuration()
    {
        var starthour = document.getElementById("<%=ddlmeetingstarthour.ClientId %>");
        var startmin = document.getElementById("<%=ddlmeetingstartminute.ClientId %>");
        var starttz = document.getElementById("<%=ddlmeetingstarttimezone.ClientId %>");
        var endhour = document.getElementById("<%=ddlmeetingendhour.ClientId %>");
        var endmin = document.getElementById("<%=ddlmeetingendminute.ClientId %>");
        var endtz = document.getElementById("<%=ddlmeetingendtimezone.ClientId %>");
        var duration = document.getElementById("<%=txtmeetingdurationmin.ClientId %>");
        
        if ( starthour.selectedIndex != 0 &&
            startmin.selectedIndex != 0 &&
            starttz.selectedIndex != 0 &&
            endhour.selectedIndex != 0 &&
            endmin.selectedIndex != 0 &&
            endtz.selectedIndex != 0) {

            var hourmins = ((endhour.options[endhour.selectedIndex].value - starthour.options[starthour.selectedIndex].value) * 60);
            var mins = endmin.options[endmin.selectedIndex].value - startmin.options[startmin.selectedIndex].value;
            duration.value = hourmins + mins;
        }
    }
    </script>

Open in new window


For the aspx code I've added a static blank listitem for each dropdown, updated some of the values of the listitems as there were duplicates (20 was duplicated for listitems with text 21 - 23).
also changed the name of the duration textbox
aspx code:
    <asp:Label Width="150" ID="Label1" runat="server" Text="Time In:"></asp:Label>
    <asp:DropDownList ID="ddlmeetingstarthour" runat="server" > 
    <asp:ListItem Value="0" Text=""></asp:ListItem>
    <asp:ListItem Value="01" Text="01"></asp:ListItem> 
    <asp:ListItem Value="02" Text="02"></asp:ListItem> 
    <asp:ListItem Value="03" Text="03"></asp:ListItem> 
    <asp:ListItem Value="04" Text="04"></asp:ListItem> 
    <asp:ListItem Value="05" Text="05"></asp:ListItem> 
    <asp:ListItem Value="06" Text="06"></asp:ListItem> 
    <asp:ListItem Value="07" Text="07"></asp:ListItem> 
    <asp:ListItem Value="08" Text="08"></asp:ListItem> 
    <asp:ListItem Value="09" Text="09"></asp:ListItem> 
    <asp:ListItem Value="10" Text="10"></asp:ListItem> 
    <asp:ListItem Value="11" Text="11"></asp:ListItem> 
    <asp:ListItem Value="12" Text="12"></asp:ListItem> 
    <asp:ListItem Value="13" Text="13"></asp:ListItem> 
    <asp:ListItem Value="14" Text="14"></asp:ListItem> 
    <asp:ListItem Value="15" Text="15"></asp:ListItem> 
    <asp:ListItem Value="16" Text="16"></asp:ListItem> 
    <asp:ListItem Value="17" Text="17"></asp:ListItem> 
    <asp:ListItem Value="18" Text="18"></asp:ListItem> 
    <asp:ListItem Value="19" Text="19"></asp:ListItem> 
    <asp:ListItem Value="20" Text="20"></asp:ListItem> 
    <asp:ListItem Value="21" Text="21"></asp:ListItem> 
    <asp:ListItem Value="22" Text="22"></asp:ListItem> 
    <asp:ListItem Value="23" Text="23"></asp:ListItem> 
    <asp:ListItem Value="24" Text="24"></asp:ListItem> 
    </asp:DropDownList>&nbsp; 
    <asp:DropDownList ID="ddlmeetingstartminute" runat="server" > 
    <asp:ListItem Value="0" Text=""></asp:ListItem>
    <asp:ListItem Value="00" Text="00"></asp:ListItem> 
    <asp:ListItem Value="15" Text="15"></asp:ListItem> 
    <asp:ListItem Value="30" Text="30"></asp:ListItem> 
    <asp:ListItem Value="45" Text="45"></asp:ListItem> 
    </asp:DropDownList>&nbsp; 
    <asp:DropDownList ID="ddlmeetingstarttimezone" runat="server" > 
    <asp:ListItem Value="0" Text=""></asp:ListItem>
    <asp:ListItem Value="EST" Text="EST"></asp:ListItem> 
    <asp:ListItem Value="CST" Text="CST"></asp:ListItem> 
    <asp:ListItem Value="PST" Text="PST"></asp:ListItem> 
 
    </asp:DropDownList>&nbsp;<font color="red">*</font> 
<br> 
<asp:Label Width="150" ID="lbltimeout" runat="server" Text="Time Out:"></asp:Label>
<asp:DropDownList ID="ddlmeetingendhour" runat="server" > 
    <asp:ListItem Value="0" Text=""></asp:ListItem>
    <asp:ListItem Value="01" Text="01"></asp:ListItem> 
    <asp:ListItem Value="02" Text="02"></asp:ListItem> 
    <asp:ListItem Value="03" Text="03"></asp:ListItem> 
    <asp:ListItem Value="04" Text="04"></asp:ListItem> 
    <asp:ListItem Value="05" Text="05"></asp:ListItem> 
    <asp:ListItem Value="06" Text="06"></asp:ListItem> 
    <asp:ListItem Value="07" Text="07"></asp:ListItem> 
    <asp:ListItem Value="08" Text="08"></asp:ListItem> 
    <asp:ListItem Value="09" Text="09"></asp:ListItem> 
    <asp:ListItem Value="10" Text="10"></asp:ListItem> 
    <asp:ListItem Value="11" Text="11"></asp:ListItem> 
    <asp:ListItem Value="12" Text="12"></asp:ListItem> 
    <asp:ListItem Value="13" Text="13"></asp:ListItem> 
    <asp:ListItem Value="14" Text="14"></asp:ListItem> 
    <asp:ListItem Value="15" Text="15"></asp:ListItem> 
    <asp:ListItem Value="16" Text="16"></asp:ListItem> 
    <asp:ListItem Value="17" Text="17"></asp:ListItem> 
    <asp:ListItem Value="18" Text="18"></asp:ListItem> 
    <asp:ListItem Value="19" Text="19"></asp:ListItem> 
    <asp:ListItem Value="20" Text="20"></asp:ListItem> 
    <asp:ListItem Value="21" Text="21"></asp:ListItem> 
    <asp:ListItem Value="22" Text="22"></asp:ListItem> 
    <asp:ListItem Value="23" Text="23"></asp:ListItem> 
    <asp:ListItem Value="24" Text="24"></asp:ListItem> 
    </asp:DropDownList>&nbsp; 
    <asp:DropDownList ID="ddlmeetingendminute" runat="server" > 
    <asp:ListItem Value="0" Text=""></asp:ListItem>
    <asp:ListItem Value="00" Text="00"></asp:ListItem> 
    <asp:ListItem Value="15" Text="15"></asp:ListItem> 
    <asp:ListItem Value="30" Text="30"></asp:ListItem> 
    <asp:ListItem Value="45" Text="45"></asp:ListItem> 
    </asp:DropDownList>&nbsp; 
    <asp:DropDownList ID="ddlmeetingendtimezone" runat="server" > 
    <asp:ListItem Value="" Text=""></asp:ListItem>
    <asp:ListItem Value="EST" Text="EST"></asp:ListItem> 
    <asp:ListItem Value="CST" Text="CST"></asp:ListItem> 
    <asp:ListItem Value="PST" Text="PST"></asp:ListItem></asp:DropDownList>&nbsp;<font color="red">*</font> 
<br> 
<asp:Label Width="150" ID="lblmeetingduration" runat="server" Text="Duration (HH/MM)"></asp:Label><asp:Textbox ID="txtmeetingdurationmin" runat="server" />

Open in new window


code-behind, that needs to be put in your page load
vb.net:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ddlmeetingendhour.Attributes.Add("onChange", "javascript:calcDuration();")
        ddlmeetingendminute.Attributes.Add("onChange", "javascript:calcDuration();")
        ddlmeetingendtimezone.Attributes.Add("onChange", "javascript:calcDuration();")
        ddlmeetingstarthour.Attributes.Add("onChange", "javascript:calcDuration();")
        ddlmeetingstartminute.Attributes.Add("onChange", "javascript:calcDuration();")
        ddlmeetingstarttimezone.Attributes.Add("onChange", "javascript:calcDuration();")
    End Sub

Open in new window


c#:
protected void Page_Load(object sender, System.EventArgs e)
{
	ddlmeetingendhour.Attributes.Add("onChange", "javascript:calcDuration();");
	ddlmeetingendminute.Attributes.Add("onChange", "javascript:calcDuration();");
	ddlmeetingendtimezone.Attributes.Add("onChange", "javascript:calcDuration();");
	ddlmeetingstarthour.Attributes.Add("onChange", "javascript:calcDuration();");
	ddlmeetingstartminute.Attributes.Add("onChange", "javascript:calcDuration();");
	ddlmeetingstarttimezone.Attributes.Add("onChange", "javascript:calcDuration();");
}

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35144028
Seems like you have this question open twice...?  Anyway, I made a comment in your other question, http:Q_26888706.html#a35143877 with a server-side solution:


This example uses two text boxes for the times; the CustomValidator's function attempts to parse the text into a DateTime, requiring the text fits the format H:mm - where "H" is a 1 or 2 digit hour between 0 and 23, and "mm" is a 2 digit minute between 00 and 59.

The button's click event handler first checks if the controls on the page passed validation by checking Page.IsValid, and then parses the text of each text box into a DateTime. Subtracting one DateTime object from another gets you a TimeSpan object, and the TotalMinutes property tells you the length of the TimeSpan object in minutes.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
	private void calculateDurationButton_Click(object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			DateTime timeIn = DateTime.Parse(timeInTextBox.Text);
			DateTime timeOut = DateTime.Parse(timeOutTextBox.Text);

			if (timeIn > timeOut)
			{
				timeOutBeforeTimeIn.Visible = true;
				return;
			}
				
			TimeSpan duration = timeOut - timeIn;

			durationLabel.Text = duration.TotalMinutes + " minutes.";
			timeOutBeforeTimeIn.Visible = false;
		}
	}

	protected void timeValidator_ServerValidate(object source, ServerValidateEventArgs args)
	{
		DateTime time;
		args.IsValid = DateTime.TryParseExact(args.Value, "H:mm", null, System.Globalization.DateTimeStyles.None, out time);
	}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		Time in: <asp:TextBox ID="timeInTextBox" runat="server" />
		<asp:CustomValidator ID="timeInValidator" runat="server"
			ControlToValidate="timeInTextBox" ErrorMessage="Invalid Time In" 
			OnServerValidate="timeValidator_ServerValidate" ForeColor="Red" />
		<br />
		Time out: <asp:TextBox ID="timeOutTextBox" runat="server" />
		<asp:CustomValidator ID="timeOutValidator" runat="server"
			ControlToValidate="timeOutTextBox" ErrorMessage="Invalid Time Out"
			OnServerValidate="timeValidator_ServerValidate" ForeColor="Red" />
		<asp:Label ID="timeOutBeforeTimeIn" runat="server"
			ForeColor="Red" Visible="false" Text="Time IN must be less than time OUT." />
		<hr />
		Duration: <asp:Label ID="durationLabel" runat="server" /><br />
		<asp:Button ID="calculateDurationButton" runat="server" Text="Calc Duration"
			OnClick="calculateDurationButton_Click" />
    </div>
    </form>
</body>
</html>

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 44
Prevent certain words from being typed in a form 6 31
Connect Gridview column to Textbox in C# 2 41
CSS for Popup in ASP.NET 4 19
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question