C # asp.net text control extension

I am extending a textBox control and I want to call a javascript function in 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?               
    }  
}  

      

+2


a source to share


7 replies


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

      

+2


a source


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

      

+1


a source


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.

+1


a source


Do you want to overload an event OnLoad

(C #) with javascript? I don't think you can do this. Or do you want to display javascript in the OnLoad event?

0


a source


Try the following:

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

      

0


a source


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

      

0


a source


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

0


a source







All Articles