What the Attributes collection property for ASP.NET controls/forms are used for?
Can we use this property for achieving the same functionality as RegisterStartupScript() ?
What the Attributes collection property for ASP.NET controls/forms are used for?
Can we use this property for achieving the same functionality as RegisterStartupScript() ?
It's used for adding client-side attributes to a server-side control. For instance:
ButtonSubmit.Attributes.Add("onclick", "return confirm('Are you sure?');");
If you can get a reference to the HTML page's body
tag, you should be able to do the same thing by adding an attribute like so:
BodyTag.Attributes.Add("onload", "myJavaScriptFunction();");
You can add attributes to a control that gets output as html.
So for this C# code:
ButtonSubmit.Attributes.Add("onclick", "return confirm('Are you sure?');");
Something like this html will be output:
<input type="button" onclick="return confirm('Are you sure?');" value="Submit" />