Water Cooler v2
Water Cooler v2

Reputation: 33850

Why are control id's important to ASP.NET Web Forms?

Some web technologies such as ASP.NET MVC and any web technology, Web Forms, including use the control's name attribute to identify the control in a post-back, for instance, to read posted values.

However, ASP.NET uses control Id's for creating control (heap object) instances in the Page class instance.

What else does ASP.NET use the control's id attribute for?

I vaguely recall there being some issue about id's of dynamically generated controls. Some conflict or some such. Or some dependency. I am not sure what.

Why are id's important to ASP.NET Web Forms and how does it use them, other than to name its Page class' properties that represent controls?

Upvotes: 2

Views: 171

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34846

Control id values are of critical importance for dynamically created controls and merging the ViewState data back to these dynamically created controls. This makes it appear to the user that the page just updated, when in reality the controls were rebuilt and the ViewState data was sent back to the server that had the id and value paired together and, finally, rebound to the control for display to the user.

If you recreate the controls with different id values, then the ViewState will not properly bind to the controls and it will look to the user that the page "reset", because there changes were unable to be applied to the proper controls.

Note: The id value is also used for statically created controls, but in the end id values are the keys for ViewState to put the correct value into the correct control.

Upvotes: 3

Related Questions