HI,
Why must we add Javascript function from code behind only at the Page_load function The first example added to the Page_Load() works whereas the second one does not
- public partial class WebForm5 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- //this works
- TextBox1.Attributes.Add("onkeydown", "javascript:alert('keydown')");
- //TextBox1.Attributes.Add("onchange", "javascript:alert('change')");
- }
- protected void TextBox1_TextChanged(object sender, EventArgs e)
- {
- //this does not
- TextBox1.Attributes.Add("OnKeyDown", "alert('Hello')");
- //TextBox1.Attributes.Add("onkeydown", "javascript:alert(this);");
- }
- }

Salman BegRank #284 · 8.8K reputationPosted Feb 3, 2019, 7:30 PM EST
Jignesh KumarRank #91 · 22.9K reputationPosted Feb 3, 2019, 7:56 PM EST
Cally KRank #3,073 · 467 reputationPosted Feb 3, 2019, 7:43 PM EST
Cally KRank #3,073 · 467 reputationPosted Feb 3, 2019, 8:21 PM EST
Salman BegRank #284 · 8.8K reputationPosted Feb 3, 2019, 8:28 PM EST
Jignesh KumarRank #91 · 22.9K reputationPosted Feb 3, 2019, 9:21 PM EST
Jignesh KumarRank #91 · 22.9K reputationPosted Feb 3, 2019, 7:34 PM EST