2

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?               
    }  
}  
4
  • 1
    What are you trying to achieve? You question mixes client side and server side concepts. Commented Apr 20, 2010 at 9:41
  • i have restated my question. i hope its clearer now. Commented Apr 20, 2010 at 9:46
  • call any javascript function...maybe just alert(''); Commented Apr 20, 2010 at 9:53
  • It's the ASP.NET textbox, not the "C# textbox". Commented Apr 20, 2010 at 13:50

7 Answers 7

2

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();
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

If you let us know what you want to happen to these inputs when the page loads we could possibly give you a clear example of how to achieve it.
actually what i am trying to do , is attaching the the JQuery calendar to MyTexbox. i did exactly what you said (adding a css class....) and everything worked fine. but then i had to update some calendars of certain textboxes in the page (like just setting min and max dates or culture for some calendars). i couldn't do this because this will update the values of all the calendars available for all the textboxes because they all have the same css. and this was the reason i thought of extending the textbox and loading the calendar for each texbox by overriding the onLoad event.
I'm glad you answered my question. I happened to have built one of those recently. The custom web control in the example above will even allow you to set jQueryUI date picker options and a date format string. Hope it helps.
thx a million:D if you still have time, i still have one single question. how can i add a mindate and maxdate to the control , so i can set them programatically? i did it by adding: this.Attributes.Add("onclick", "DefinedSettings(this,'" + this._minDate + "','" + this.maxDate + "');"); while setting the min or max dates. is there any better way?
jQueryUI can handle this for you. You can set the values programatically using the DatePickerOptions property of this control. Check the jQueryUI doco on how to set these values then pass the appropriate string to DatePickerOptions.
1

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();");        
    }   
}

4 Comments

i need to do this on all my textboxes on all of the pages, so i thought it would be much better to override the OnLoad event rather than calling txtBox.Attributes.Add() on every page for every textbox
yes i am sure that this would work, but you are specifying a single textbox. as i said, i have a lot of textboxes in the project and i can't just set an Attribure to every textbox. i need a way to set the attribute for all current texboxes (and future one's) in one single attribute.this is why i am extending the textbox control....
Errr... No, I'm not specifying this to a single textbox... I'm extending the TextBox class! If every textbox in your app is type of MyTextBox, they will all have this attribute! Right? ;)
oh... you are right... anyway i have already tried this and it did not work. i added this.Attributes.Add("OnLoad", "alert('ok!');"); then i launched a page that contains MyTextBox control and no alert occured...
1

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.

2 Comments

it did not work. i tested it by doing : Attributes.Add("OnLoad", "alert('ok!');"); and launching a page that contains MyTextBox control and no alert occured....
See my new answer: "The html element 'input' does not have a load event"
0

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?

2 Comments

no i am using c# to override the method, but i want to call a javascript function inside this method.
stackoverflow.com/questions/1577859/… Please see this one from SO. I think it can help you.
0

Try this:

protected override void OnLoad(EventArgs e)  
{  
    base.OnLoad(e);  
    Response.Write("<script>your script</script>");
}

Comments

0
public partial class MyTextBox: TextBox  
{ 
    protected override void OnLoad(EventArgs e)   
    {   
        base.OnLoad(e);    
        this.Attributes.Add("OnLoad","alert('Im Loaded!');");        
    }   
}   

Comments

0

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

1 Comment

there is no RegisterStartupScript with a partial class that extends textbox. thx anyway...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.