I have the following web control. When I tab out of the TextBox of this control, I have an alert to display the value of the control. However, when the alert in the ClientCallback() is executed, it displays undefined. This means I some how do not get the control. And suggestions? Looks like there is some problem with the way I am gettting the value out of the control.
Here is the javascript function which gets generated:
function ClientCallback()
{
alert(document.getElementById('WebCustomControl1_1 ').value);
args = document.getElementById('WebCustomControl1_1').val ue;
WebForm_DoCallback('WebCustomControl1_1',args,Call backHandler,null,ErrorHandler,true)
}
Here is the code for this control:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : WebControl, ICallbackEventHandler
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
[Themeable(false)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.RenderBeginTag(HtmlTextWriterTag.Div);
output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
output.AddAttribute(HtmlTextWriterAttribute.Name, String.Concat(this.ClientID, "zz"));
output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
output.AddAttribute("OnBlur", "ClientCallback()");
output.RenderBeginTag(HtmlTextWriterTag.Input);
AddAttributesToRender(output);
output.RenderEndTag();
output.RenderEndTag();
}
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterClientScriptResource(typ eof(WebControlLibrary1.WebCustomControl1), "WebControlLibrary1.Utility.
js");
Page.ClientScript.RegisterClientScriptBlock(
typeof(Page),
"ClientCallback",
"function ClientCallback() { alert(document.getElementById('" + this.ClientID + "').value); " +
"args = document.getElementById('" + this.ClientID + "').value;" +
Page.ClientScript.GetCallbackEventReference(this, "args", "CallbackHandler", null, "ErrorHandler", true) + "}",
true);
}
public void RaiseCallbackEvent(string eventArguments)
{
int result;
if (!Int32.TryParse(eventArguments, out result))
{
throw new Exception("The method or operation not implemented");
}
}
public string GetCallbackResult()
{
return "Valid Data";
}
}
}
Javascript file:
function ValidateText(ctl)
{
if (ctl.value=='') {
alert('Please enter a value.');
ctl.focus();
}
}
function CallbackHandler(args,ctx)
{
alert("The data is valid");
}
function ErrorHandler(args,ctx)
{
alert("Please enter a number");
}
:(:(