views:

482

answers:

2

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() ?

+1  A: 

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();");
Tim S. Van Haren
I modified my answer with a response.
Tim S. Van Haren
+1  A: 

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" />
Nick