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.

2
  • Try removing runat=”server” from your control. Commented Feb 19, 2016 at 11:03
  • this worked, but the link no longer works. onserverclick doesnt seem to have any affect when the runat is removed Commented Feb 19, 2016 at 11:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.