I’ve got a javascript counter on a textbox so I can count down the number of characters are left before a field in a table is full. Also, a second counter that counts down before the user runs out of characters that will fit in an email:
<script type="text/javascript">
function textCounter(field, maxlimit, label, label2) {
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
else {
label.innerHTML = maxlimit - field.value.length;
}
if (field.value.length > maxlimit - 500) {
label.style.color = "#FF0000";
label2.style.color = "#FF0000";
}
else {
label.style.color = "#000000";
label2.style.color = "#000000";
}
}
function textCounter2(field, maxlimit, label, label2) {
if (field.value.length > maxlimit)
// field.value = field.value.substring(0, maxlimit);
field.value = field.value;
else {
label.innerHTML = maxlimit - field.value.length;
}
if (field.value.length > maxlimit - 200) {
label.style.color = "#FF0000";
label2.style.color = "#FF0000";
}
else {
label.style.color = "#000000";
label2.style.color = "#000000";
}
}
</script>
These are the textbox and counter fields in the ASP page:
<asp:Panel ID="Panel_Employee_Follow_Up" runat="server" Visible="false">
<table style="width: 100%;">
<tr>
<td>
<asp:TextBox ID="TextBox_Answer" runat="server" Width="600px" MaxLength="3000" Height="120px" onkeydown="textCounter(TextBox_Answer, 3000, Label_Answer_Char_Count, Label_Answer_Char_Remaining);textCounter2(TextBox_Answer, 865, Label_Answer_Char_Count2, Label_Answer_Char_Remaining2);" onkeyup="textCounter(TextBox_Answer, 3000, Label_Answer_Char_Count, Label_Answer_Char_Remaining);textCounter2(TextBox_Answer, 865, Label_Answer_Char_Count2, Label_Answer_Char_Remaining2);" TextMode="MultiLine">
</asp:TextBox>
<br />
<asp:Label ID="Label_Answer_Char_Count" runat="server" Text="3000"></asp:Label>
<asp:Label ID="Label_Answer_Char_Remaining" runat="server" Text="characters remaining"></asp:Label> :
<asp:Label ID="Label_Answer_Char_Count2" runat="server" Text="865"></asp:Label>
<asp:Label ID="Label_Answer_Char_Remaining2" runat="server" Text="characters remaining for email"></asp:Label>
<br />
</td>
</tr>
</table>
</asp:Panel>
What’s happening is, when a user clicks off the textbox, the counter is getting reset. Presumably because of a postback? There are a number of other fields on this form, but I narrowed down the code to just the ones directly relevant to the question. For example, the user typically fills in this textbox and then clicks a button to generate an email.
After the button has been clicked, the counters reset, and I need to prevent them from resetting. I’m not sure if this requires preventing a postback or storing the counter numbers in a session variable. Can anyone offer some guidance on how to prevent the counters from resetting, or how/when I’d write them to a session variable?