How to set a control ID in ListView Template dynamically?
A have a ListView that is rendered with multiple items.
Now I want to toggle some HTML attributes with JQuery. Therefore it would be best to have access to these elements via an unique ID.But trying to create a "dynamic" and therefore unique ID by
<tr runat="server" ID='<%# this.GetUniqueID() %>'>
</tr>
results in an error that tells me that the ID needs to be simple and cannot be set by a call to a method.
I know tha开发者_开发问答t I can dynamically create controls in the code-behind and set the ID there. But in this case, I'd rather like to let the content be rendered by the ListView itself.
That brings me to the conclusion that the idea of setting a dynically ID in the Template is totally wrong. How can I achieve the desired behaviour?
Edit: Ok I just found out, that I can set the ID with a BindingExpression, like
ID='<%# Eval("MyColumnWithUniqueID") %>'
Still, is there another, or even better solution to this?
You can do this:
<tr id='<%# "row" + Container.DataItem %>'>
This will give it a numerical ID with a "row" prefix using the position of the item in the list.
So you'll just get row0
, row1
, etc.
精彩评论