views:

1010

answers:

2

So I am trying to get CLientID inside the .ascx (user control mark-up) file.

While this

My id is: <%=this.ClientID%>

renders as My id is: fracTemplateCtrl

This:

        <asp:Button ID="btnSave" runat="server" Text="Save Template" onclick="btnSave_Click" OnClientClick="return confirmSave('<%=this.ClientID%>');" />

renders as (inside Source code):

        <input type="submit" name="fracTemplateCtrl$btnSave" value="Save Template" onclick="return confirmSave('&lt;%=this.ClientID%>');" id="fracTemplateCtrl_btnSave" />

Clearly, ClientId property doesn't get evaluated in the second case. WHat gives? How do I overcome this issue? (aside from hardcoding, which is not the answer, I would like to make the user control independent)

+2  A: 

Try this instead

<asp:Button ID="btnSave" runat="server" Text="Save Template" onclick="btnSave_Click" OnClientClick="return confirmSave(this.id);" />
John MacIntyre
ok, can see it now, thanks!
gnomixa
Actually, your first answer got me thinking in the right direction. However, OnClientClick="return confirmSave(this.id);" actually gets evaluated as a button id (not the control id). So you were right the first time.:)
gnomixa
@gnomixa-Thanks, but the first time I was still wrong. I was unable to explain it accurately and concisely, so I just threw together code that I thought would work, but by then, I'd forgotten the point! ;-) But thanks for the upvote anyway.
John MacIntyre
+2  A: 

You could set the OnClientClick property's value server-side like this:

btnSave.OnClientClick = "return confirmSave('" + this.ClientID + "')";
Per Hornshøj-Schierbeck