A
Andrew Jocelyn
Hi
I have a custom server control which basically adds a client side character
counter to display with an Html TextArea. When I use the control inside an
UpdatePanel the JavaScript counter disappears.
Please see code below.
Any ideas?
Thanks
Andrew
protected override void OnPreRender(EventArgs e)
{
if (IsRequired)
_reqValidator.Visible = true;
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptInclude("DisplayLengthFunctions",
cs.GetWebResourceUrl(this.GetType(),
"MyControls.WebControls.DisplayLengthFunctions.js"));
cs.RegisterStartupScript(this.GetType(), this.ID + "DisplayLength",
"DisplayCounter('" + _textBox.ClientID + "','" + this.ID + "_Counter'," +
this.MaxLength.ToString() + ");", true);
base.OnPreRender(e);
}
///////////////////////////////
// DisplayLengthFunctions.js
////////////////////////////////
function DisplayCounter(textId, counterId, maxLength) {
var W3CDOM = document.createElement && document.getElementsByTagName &&
document.getElementById;
if (!W3CDOM) return;
var x = document.getElementById(textId);
x.setAttribute('maxlength', maxLength);
var counter = document.getElementById(counterId);
counter.innerHTML = '<span>0</span>/' + maxLength;
counter.relatedElement = x;
x.relatedElement = counter.getElementsByTagName('span')[0];
x.onkeyup = x.onchange = CheckMaxLength;
x.onkeyup();
}
// event handler for textarea onkeyup and onchange
function CheckMaxLength() {
var maxLength = this.getAttribute('maxlength');
var currentLength = this.value.length;
if (currentLength > maxLength)
this.relatedElement.style.color = 'red';
else
this.relatedElement.style.color = '';
this.relatedElement.firstChild.nodeValue = currentLength;
}
I have a custom server control which basically adds a client side character
counter to display with an Html TextArea. When I use the control inside an
UpdatePanel the JavaScript counter disappears.
Please see code below.
Any ideas?
Thanks
Andrew
protected override void OnPreRender(EventArgs e)
{
if (IsRequired)
_reqValidator.Visible = true;
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptInclude("DisplayLengthFunctions",
cs.GetWebResourceUrl(this.GetType(),
"MyControls.WebControls.DisplayLengthFunctions.js"));
cs.RegisterStartupScript(this.GetType(), this.ID + "DisplayLength",
"DisplayCounter('" + _textBox.ClientID + "','" + this.ID + "_Counter'," +
this.MaxLength.ToString() + ");", true);
base.OnPreRender(e);
}
///////////////////////////////
// DisplayLengthFunctions.js
////////////////////////////////
function DisplayCounter(textId, counterId, maxLength) {
var W3CDOM = document.createElement && document.getElementsByTagName &&
document.getElementById;
if (!W3CDOM) return;
var x = document.getElementById(textId);
x.setAttribute('maxlength', maxLength);
var counter = document.getElementById(counterId);
counter.innerHTML = '<span>0</span>/' + maxLength;
counter.relatedElement = x;
x.relatedElement = counter.getElementsByTagName('span')[0];
x.onkeyup = x.onchange = CheckMaxLength;
x.onkeyup();
}
// event handler for textarea onkeyup and onchange
function CheckMaxLength() {
var maxLength = this.getAttribute('maxlength');
var currentLength = this.value.length;
if (currentLength > maxLength)
this.relatedElement.style.color = 'red';
else
this.relatedElement.style.color = '';
this.relatedElement.firstChild.nodeValue = currentLength;
}