开发者

Get User control id within user control at runtime

is it possible to get the开发者_如何学编程 ID assigned to User Control from the control using javascript or jquery.

Thanks


What ASP.NET normally does is prefix your control's ID with a string that it uses to determine where in ASP.NET's control tree your actual control resides.

With that in mind, what I normally do is to use jQuery's 'ends with' selector to get the full ASP.NET-parsed ID at runtime.

Something like:

// get a handle on your original control
var myControl = $('[id$="<myOriginalId>"]');
// and then access it's properties
var myRuntimeId = myControl.eq(0).attr('id');

As you can most probably imagine, that's not going to cut it when you've got UserControls with the same ID used in different places of the form. I just jump in and put in some tweaks here and there (probably with using the .eq() function) to suit my business need.


You could put a class on the usercontrol, and then use something like $(".myUC").attr("id")


This might help you to look at it from a different point of view: In .Net you can get the generated ID by using myControl.ClientID.

If you put that in a javascript variable - I know it's not neat - you can then easily fetch it.

<!--mypage.aspx-->
<script>
  var myIdVar = "<%=myControl.ClientID%>";
  if(myIdVar == "foo")
  {
    alert("bar");
  }
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜