Calling ClientID from aspx

euther picture euther · Feb 24, 2010 · Viewed 14.7k times · Source

"")" />

that doesn't work, the error says: Parser Error Message: Server tags cannot contain <% ... %> constructs.

Any aproaches to solve this ? Thank you ;)

Answer

jball picture jball · Feb 24, 2010

You're calling a JS event (onchange), not a server event, so just pass in this.id.

<input type="checkbox" id="chbSaveState" runat="server" tabindex="3"  
onchange="SaveState(this.id)" /> 

To be clear, this.id and <%=chbSaveState.ClientID%> will return the same value in this case. Since you're calling this on an event of chbSaveState, you can just use the easily accessible JS property here, rather than <%=chbSaveState.ClientID%>, which requires the server to return the id which is generated by the server for that control.