Passing ASP.NET client IDs in to a javascript function

后端 未结 3 668
北荒
北荒 2020-12-21 05:06

I need to pass a client ID to a Javascript function in the onblur event of an ASP.net control event like this:

OnBlur=\"javascript:setBackground(this, \'<         


        
相关标签:
3条回答
  • 2020-12-21 05:35
    <asp:TextBox ID="TextBox1" runat="server" onclick="dosomething(this)"></asp:TextBox>
    
    <script>
    function dosomething(obj)
    {
       var txtObj= document.getElementById(obj.id);
    alert (txtObj);
    }
    </script>
    

    pass this object from the function and can get this.id from javascript that will be ClientID

    0 讨论(0)
  • 2020-12-21 05:37

    You could either change the asp.net control to standard html element (i.e., without runat="server")

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <input type="text" id="ClientText1" onblur="javascript:alert('<%= TextBox1.ClientID %>')" />
    

    or see this Stack Overflow answer:

    problem assigning declarative values in asp:hyperlink. error: this is not scriptlet. will output as plain text

    or use jquery

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"> </script>
    
        <script type="text/javascript">
            $(document).ready(function () {
                $("#text2").blur(function(){alert('<%= TextBox1.ClientID %>')});
            });
        </script>
    
    0 讨论(0)
  • 2020-12-21 05:45

    Is this from the code-behind?

    How about OnBlur=String.Format("javascript:setBackground(this, '{0}')", txtClientName.ClientID);

    0 讨论(0)
提交回复
热议问题