C # asp.net text control extension
The "input" element of the html element has no load event. 'body', 'img' and 'window' do. There may be others.
I suggest you add a css class to your custom inputs (textboxes) and then use something like jQuery to handle the page load event to execute your function on all textboxes with that css class.
The following example assumes you have jQuery and jQueryUI loaded on your page.
public class MyTextBox: TextBox
{
public string DatePickerOptions { get; set; }
public string DateFormatString { get; set; }
public string EmptyDateText { get; set; }
public DateTime? Date
{
get
{
DateTime? date = null;
if ( string.IsNullOrEmpty(Text) )
return date;
DateTime outDateTime;
if (DateTime.TryParse(Text, out outDateTime))
date = outDateTime;
return date;
}
set
{
DateTime? date = value;
if ( date == null || ((DateTime)date).Year < 2 )
Text = EmptyDateText;
else
Text = ((DateTime) date).ToString(DateFormatString);
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Text = EmptyDateText;
}
public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
{
StringBuilder javaScriptBuilder = new StringBuilder();
javaScriptBuilder.Append("$(function() { $(\"#");
javaScriptBuilder.Append(ClientID);
javaScriptBuilder.Append("\").datepicker(");
javaScriptBuilder.Append(DatePickerOptions);
javaScriptBuilder.Append("); });");
base.RenderEndTag(writer);
writer.WriteLine();
writer.Write("<script type=\"text/javascript\">");
writer.WriteLine();
writer.Indent++;
writer.Write(javaScriptBuilder.ToString());
writer.WriteLine();
writer.Indent--;
writer.Write("</script>");
writer.WriteLine();
writer.Close();
}
}
a source to share
I think you are confused ... Do you want to trigger a javascript function for the onload event on the textbox? Then you shouldn't be doing it on the server side, but on the client side. However, you don't need to override the onload event. If you add this attribute to the constructor, it should do the trick!;)
public class MyTextBox: TextBox
{
public MyTextBox()
{
this.Attributes.Add("OnLoad", "jsFunction();");
}
}
a source to share
Based on your answer on Pedro MM, you suspect you want:
public partial class MyTextBox: TextBox
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Attributes.Add("OnLoad", "jsFunction();");
}
}
OnLoad (EventArgs e) is an event on the server. It has nothing to do with the web page's JavaScript OnLoad event.
a source to share
Hope I understood your question correctly,
You can use this function. PageRegisterStartupScript Method which will make your javascript code run as soon as the page is reloaded.
More info and example at msdn http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx
a source to share