Reputation: 14565
I am extending the textBox control, and i want to call a javascript function on its OnLoad(EventArgs e). how can i do this?
public partial class MyTextBox: TextBox
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//call to a javascript function?
}
}
Upvotes: 2
Views: 2430
Reputation: 113
The html element 'input' does not have a load event. 'body', 'img' and 'window' do. There may be others.
I suggest you add a css class to the custom inputs (textboxes) then use something like jQuery to handle the load event of the page to perform your function on all the textboxes with that css class.
The following example assumes you have jQuery and jQueryUI loaded in 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();
}
}
Upvotes: 2
Reputation: 113
Based on your response to Pedro MM is suspect what you want is this:
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. Its has no relation to a web page's JavaScript OnLoad event.
Upvotes: 1
Reputation: 410
I think you are confused... You want to fire a javascript function to the onload event of the textbox, right? Then you shouldn't be doing it in server-side but in client-side. That said, you don't need to override the onload event. If you add this attribute in the constructor, it should do the trick! ;)
public class MyTextBox: TextBox
{
public MyTextBox()
{
this.Attributes.Add("OnLoad", "jsFunction();");
}
}
Upvotes: 1
Reputation: 69022
Hope i understood your question right,
You can use this function Page.RegisterStartupScript Method , which will make your javascript code run just after the page loads again.
More information and example on msdn http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx
Upvotes: 0
Reputation: 16018
public partial class MyTextBox: TextBox
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Attributes.Add("OnLoad","alert('Im Loaded!');");
}
}
Upvotes: 0
Reputation: 5015
Try this:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Response.Write("<script>your script</script>");
}
Upvotes: 0
Reputation: 463
You want to overload the OnLoad
event(C#) with javascript?
I don't think you can do it.
Or you want to render some javascript in the OnLoad event?
Upvotes: 0