I would like to code my submit web control button with a timer on it. Its suppose to allow submission until time goes up and the button is disabled from submission. Any directions or tutorials on this?

Dani AI

Generated

A quick, practical follow‑up to ’s question and ’s JavaScript approach: using client script to disable a submit button is fine for the user experience, but ASP.NET server controls render different client IDs and a disabled control can be bypassed or prevent a postback if handled incorrectly. Put the script in the page (or register it from code‑behind), start the timer on page load, and target the rendered element using the control’s ClientID. Also always enforce the time limit on the server — client script is only UX.

A simple pattern (markup + client script) that keeps the client and server pieces separate:

<!-- ASPX: attach a client-side starter -->
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
    OnClientClick="startDisableTimer();" />

<script type="text/javascript">
function startDisableTimer() {
  setTimeout(function(){
    var b = document.getElementById('<%= btnSubmit.ClientID %>');
    if (b) b.disabled = true;
  }, 5000); // disable after 5 seconds
}
</script>

Server‑side enforcement example (C#): set a start time when you render the page and check it during postback. Never rely solely on client disabling.

protected void Page_Load(object sender, EventArgs e) {
  if (!IsPostBack) Session["StartTime"] = DateTime.UtcNow;
}

protected void btnSubmit_Click(object s, EventArgs e) {
  var start = (DateTime)Session["StartTime"];
  if ((DateTime.UtcNow - start).TotalSeconds > 5) {
    // reject or show message: time expired
  } else {
    // proceed with normal processing
  }
}

Troubleshooting notes: if you need the button disabled only after a click (to avoid double submits), disable it in the OnClientClick handler but return true so the postback still happens. If the page uses UpdatePanel/ScriptManager, register startup scripts with ScriptManager.RegisterStartupScript. Finally, remember that disabled inputs aren’t posted and client checks can be bypassed — server validation is mandatory for correctness and security.

Recommended Answers

All 3 Replies

easiest way is javascript

<html>
<head>
<script language="JavaScript">
<!--
function setDisableButtonTimer(){
	 disableButtonTimer = setInterval("disableButton()", 5000);

}

function disableButton(){
document.form1.btnSubmit.disabled = true;
clearInterval(disableButtonTimer);
}

-->
</script>

</head>
<body onload="setDisableButtonTimer();">

<FORM ACTION="test.asp" METHOD="POST" name="form1" >
		<SELECT NAME="MyListBox" Size="11" MULTIPLE>
			<OPTION VALUE="0">CustomerID
			<OPTION VALUE="1">CompanyName
			<OPTION VALUE="2">ContactName
			<OPTION VALUE="3">ContactTitle
			<OPTION VALUE="4">Address
			<OPTION VALUE="5">City
			<OPTION VALUE="6">Region
			<OPTION VALUE="7">PostalCode
			<OPTION VALUE="8">Country
			<OPTION VALUE="9">Phone
			<OPTION VALUE="10">Fax
		</SELECT><P>
<INPUT TYPE=submit name="btnSubmit" >&nbsp;&nbsp
<INPUT TYPE=reset>
</FORM>
</body>

</html>
<!--
function setDisableButtonTimer(){
	 disableButtonTimer = setInterval("disableButton()", 5000);

}

function disableButton(){
document.form1.btnSubmit.disabled = true;
clearInterval(disableButtonTimer);
}

-->
</script>

</head>
<body onload="setDisableButtonTimer();">

Campkev, are the above the essential parts i need to have in the html page? Also how do i run the script for the ASP.NET web control button since i have to call 2 functions?

here is a whole web page to give you the idea

<html>
<head>
<script language="JavaScript">
<!--
function setDisableButtonTimer(){
	 disableButtonTimer = setInterval("disableButton()", 5000);

}

function disableButton(){
document.form1.btnSubmit.disabled = true;
clearInterval(disableButtonTimer);
}

-->
</script>

</head>
<body onload="setDisableButtonTimer();">

<FORM ACTION="test.asp" METHOD="POST" name="form1" >
		<SELECT NAME="MyListBox" Size="11" MULTIPLE>
			<OPTION VALUE="0">CustomerID
			<OPTION VALUE="1">CompanyName
			<OPTION VALUE="2">ContactName
			<OPTION VALUE="3">ContactTitle
			<OPTION VALUE="4">Address
			<OPTION VALUE="5">City
			<OPTION VALUE="6">Region
			<OPTION VALUE="7">PostalCode
			<OPTION VALUE="8">Country
			<OPTION VALUE="9">Phone
			<OPTION VALUE="10">Fax
		</SELECT><P>
<INPUT TYPE=submit name="btnSubmit" >&nbsp;&nbsp
<INPUT TYPE=reset>
</FORM>
</body>

</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.