HS.
HS.

Reputation: 15720

Determining ASP.NET client ids

This question is similar to Forcing client ids in ASP.NET but not quite.

ASP.NET generates clientids according to its own internal mechanism. I would like to run a xmlhttprequest query in the background to do an update and selectively reload some of the controls. My plan is to run the query and regenerate the page in the background, but only selectively rendering the controls I want to update - then just replace the html of those controls with the new ones.

My question is, can I expect the clientids to be the same (since I'm generating the same page) and is there any thing else I need to be aware of if I want to update asp.net-generated html through javascript?

Upvotes: 0

Views: 122

Answers (3)

Atanas Korchev
Atanas Korchev

Reputation: 30661

I use the following trick to get the ClientID of an ASP.NET control:

<script type="text/javascript">
//textBox will contain the DOM element rendered for the <asp:TextBox /> control
var textBox = document.getElementById("<%= nameTextBox.ClientID %>");
</script>
<asp:TextBox runat="server" ID="TextBox1" />

Upvotes: 1

annakata
annakata

Reputation: 75872

Whilst the system is predictable, it's ill-advised to build a dependency on these names as they are generated and it has the ability to create both a difficult maintenance burden and the possibility for some subtle bugs when your pages do change, which they will.

This is all part of the problem with working with asp.net generally, I suggest you find a more reliable way to target nodes, like targeting non-asp wrapper nodes and then looking at the children, or targeting classnames.

Upvotes: 1

Cerebrus
Cerebrus

Reputation: 25775

ASP.NET generates control names and ID's according to the position of the control in the page hierarchy.

If this hierarchy is not going to change between successive page regenerations,then you can be sure that the control ID generated will not differ. Another notable point is that control IDs have random names on each request when you use Fragment caching.

Another idea would be store the ClientIDs (available server-side) into hidden field(s) and then retrieve this information via script.

Upvotes: 1

Related Questions