javascript - Difference between ID and control.ClientID OR why use control.ClientID if I can access control through ID - Stack O

This is the code from .aspx file<html xmlns=""><head runat="server">&l

This is the code from .aspx file

<html xmlns="">
<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.ClientID and why?

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

This is the code from .aspx file

<html xmlns="http://www.w3/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.ClientID and why?

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

Share Improve this question asked Sep 18, 2010 at 21:29 KashifKashif 14.5k18 gold badges70 silver badges100 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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 remended 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248678a4618542.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信