1

Im trying to add a dynamic ID to an asp server control. Im running a loop where i have a dynamic variable called "elementID". What i want to do is create a Hyperlink that has a dynamic ID.

If i use <%= elementID%> on the line above, the value gets output, but if i run <% codeBehindMethod(elementID); %> i get an error saying the variable does not exist in the current context. It clearly does since i was able to output it if i just write the variable.

Im using the answer from here to render the element ASP.NET how to Render a control to HTML? since i cannot render it within the .aspx file. The reason i am doing it this way is because i cannot do it inline as i get the following error when i use this code.

<a runat="server" href="#" onserverclick="deleteScheduleField" id=<%# elemendID %>>

The ID property of a control can only be set using the ID attribute in the tag and a simple value

If i try to put the variable as a class i.e. class=<%# elementID %> i get the same error about the element not existing in the current context.

Anyone know whats going on here and why i cannot get a simple variable to get displayed as an attribute for a hyperlink. Im using this as a delete button. A table of rows from a DB i want the hyperlink to call deleteElement() in the code behind. The issue is i cant access the element ID that needs to be deleted.

Community
  • 1
  • 1
Dan Hastings
  • 3,241
  • 7
  • 34
  • 71

0 Answers0