开发者

Difference between ID and control.ClientID OR why use control.ClientID if I can access control through ID

This is the code from .aspx file

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login Again</title>

    <script type="text/javascript">
        function Validate() {
            if (document.getElementById("txtLogin").value == "") {
                alert("Enter login name.");
            }

            if (document.getElementById("<%=txtLogin.ClientID%>").value == "") {
                alert("Enter login name.");
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="txtLogin" runat="server"></asp:TextBox>
    <asp:Button ID="btnSubmit" runat="server" Text="Login" OnClientClick="Validate()" />
    </form>
</body>
</html>
  • In function Validate() I can access textbox using Id of control i.e.; getElementById("txtLogin") so should I use the second approach which is accessing control through control.ClientI开发者_如何学编程D and why?

  • My first understanding was that to access server control I have to use this syntax <%= %> but now I come to know from this example that I can access server side control simply through getElementById("ID-of-control").


The ID generated in the final HTML is not guaranteed to remain the same as the one in your aspx source. When you'll put the controls inside naming containers the ID will have prepended one or several parent ids to ensure its uniqueness. The ClientId property will always give you the final form of the ID attribute as it ends up in the HTML so it's always recommended to use that in your javascript.


Read this....

Quote from post...

All ASP.NET server controls include an ID property that uniquely identifies the control and is the means by which the control is programmatically accessed in the code-behind class. Similarly, the elements in an HTML document may include an id attribute that uniquely identifies the element; these id values are often used in client-side script to programmatically reference a particular HTML element. Given this, you may assume that when an ASP.NET server control is rendered into HTML, its ID value is used as the id value of the rendered HTML element. This is not necessarily the case because in certain circumstances a single control with a single ID value may appear multiple times in the rendered markup....

Short answer is ClientID to ensure you find your control.


Which version of ASP.NET are you using? In .NET 4 you can specify not to autogenerate the IDs.

I think this is a coincidence in this case because youn are not using any user controls or other containers. Once you do you will no longer be able to guarentee that the controls ID will remain the same and thus you should use the second approach as a best practice because if your page is modified in the ways I have stated your javascript will no longer work and it may be difficult to see why at a later date.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜