Reputation: 8697
I have found out a scripting language for word count in some reference on the net. And the scripting is like this
<script language = "Javascript">
function tbLimit() {
var tbObj = event.srcElement;
if (tbObj.value.length == tbObj.maxLength * 1) return false;
}
function tbCount(visCnt) {
var tbObj = event.srcElement;
if (tbObj.value.length > tbObj.maxLength * 1) tbObj.value = tbObj.value.substring(0, tbObj.maxLength * 1);
if (visCnt) visCnt.innerText = tbObj.maxLength - tbObj.value.length;
}
</script>
I have inserted this into my aspx pages just after my content place holder. And in order to link and call this script in my page i type this code into my page load
protected void Page_Load(object sender, EventArgs e)
{
if (Session["LoginAs"] != "PoliceStaff")
{
Response.Redirect("Login.aspx");
}
else
{
if (!Page.IsPostBack)
{
LoadGrid();
tbOR.Attributes.Add("onkeypress", "return tbLimit();");
tbOR.Attributes.Add("onkeyup", "return tbCount(" + lblCounts.ClientID + ");");
tbOR.Attributes.Add("maxLength", "500");
}
}
}
I have also added a label which will run the word count like this
You have <asp:Label ID="lblCounts" runat="server" Text="500"></asp:Label> characters left.
Can anyone see what is going wrong which is preventing this from running?
Upvotes: 0
Views: 269
Reputation: 78545
Very close, you need to tell Javascript the ClientID is a string by adding quotes inside the parameter to tbCount
:
tbOR.Attributes.Add("onkeyup", "return tbCount('" + lblCounts.ClientID + "');");
Then in your Javascript, look for it using document.GetElementById
:
function tbCount(visCnt) {
visCntInput = document.getElementById(visCnt);
var tbObj = event.srcElement;
if (tbObj.value.length > tbObj.maxLength * 1) tbObj.value = tbObj.value.substring(0, tbObj.maxLength * 1);
if (visCntInput) visCntInput.innerText = tbObj.maxLength - tbObj.value.length;
}
At the moment, you are assuming ClientID
returns a reference to a DOM object where in reality it returns you a client-side ID
so that you can locate the object.
Upvotes: 1