Cally K

Cally K

  • 1.2k
  • 507
  • 15.6k

Adding a Javascript function from code-behind in Page_Load()

Feb 4 2019 12:15 AM
HI,
 
Why must we add Javascript function from code behind only at the Page_load function
  1. public partial class WebForm5 : System.Web.UI.Page  
  2.     {  
  3.         protected void Page_Load(object sender, EventArgs e)  
  4.         {  
  5.             //this works  
  6.             TextBox1.Attributes.Add("onkeydown""javascript:alert('keydown')");  
  7.             //TextBox1.Attributes.Add("onchange", "javascript:alert('change')");  
  8.         }  
  9.   
  10.         protected void TextBox1_TextChanged(object sender, EventArgs e)  
  11.         {  
  12.              //this does not  
  13.             TextBox1.Attributes.Add("OnKeyDown""alert('Hello')");  
  14.             //TextBox1.Attributes.Add("onkeydown", "javascript:alert(this);");  
  15.         }  
  16.     }  
 The first example added to the Page_Load() works whereas the second one does not

Answers (7)