简体   繁体   English

如何在asp.net中获取运行时ID

[英]How to get the runtime ID in asp.net

I have a asp.net elements when page is run it changes its ID dynamically to add "ctl00_Content" to the ID I tried to call the ID element in this way to avoid this dynamic name 我在运行页面时有一个asp.net元素,它会动态更改其ID,以将“ ctl00_Content”添加到ID中,我尝试以此方式调用ID元素以避免此动态名称

$("#<%=btnCreate.ClientID%>")

but when the page add "ctl00_" only I tried to write it as "#ctl00_MyActionMenuControl_btnCreateNew" and it works well but when I try to write it as $("#<%=MyActionMenuControl_btnCreateNew.ClientID%>") gives me error 但是当页面仅添加“ ctl00_”时,我尝试将其写为“#ctl00_MyActionMenuControl_btnCreateNew”,并且效果很好,但是当我尝试将其写入为$("#<%=MyActionMenuControl_btnCreateNew.ClientID%>") ,我出现了错误

please help 请帮忙

One way to do this as follows 一种方法如下

<asp:Button ID="btnCreate" ClientIDMode="Static" runat="server" Text="Button" />

Javascript Java脚本

<script type="text/javascript">
    var btn = $("#btnCreate");
    var btnText = $("#btnCreate").text();
    alert(btnText);
</script>

But in your case change your code as follows, it will work fine 但是在您的情况下,请按以下方式更改代码,即可正常工作

$("#"+'<%=MyActionMenuControl_btnCreateNew.ClientID%>')

mziaulhaq was right. mziaulhaq是正确的。 If you try to use server controls into client side validation/in funcationality you must use 'ClientIDMode="Static"'. 如果您尝试将服务器控件用于客户端验证/功能验证,则必须使用“ ClientIDMode =“ Static”“。 It helps you to maintain the ID as Static even though it was server side control. 即使它是服务器端控制,它也可以帮助您将ID保持为静态。

 <asp:Button ID="btnCreate" ClientIDMode="Static" runat="server" Text="Button" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM