ASP.NET传递客户ID到一个javascript函数 [英] Passing ASP.NET client IDs in to a javascript function

查看:95
本文介绍了ASP.NET传递客户ID到一个javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个客户端ID传递给JavaScript函数在这样一个ASP.net控制事件的onblur事件:

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, '<%= txtClientName.ClientID %>')"

下面是我的javascript功能:

Here is my Javascript function:

function setBackground(sender, controlID) {
        sender.style.backgroundColor = "#ffffff";
        var nextElement = document.getElementById(controlID);
        if ((nextElement.value == '' || nextElement.value == 'Select') && tab == true) {
            nextElement.style.backgroundColor = "#f7C059"
            tab = false;
        }
    }

的问题是,该客户端ID获取在字面上通过如'&下;%= txtClientName.ClientID%>而不是实际的数值。因此,调用的document.getElementById(控件);不工作。

The problem is that the client ID gets passed in literally as '<%= txtClientName.ClientID %>' instead of the actual value. So, calling document.getElementById(controlID); doesn't work.

我怎样才能得到实际的客户端ID,并将它传递给我的javascript功能?

How can I get the actual client ID and pass it to my Javascript function?

推荐答案

您既可以改变asp.net控制标准的HTML元素(即不=服务器)

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:

<一个href=\"http://stackoverflow.com/questions/3441841/problem-assigning-declarative-values-in-asphyperlink-error-this-is-not-scriptl\">problem在ASP分配的声明价值:超链接。错误:这不是scriptlet而。将输出为纯文本。

或使用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>

这篇关于ASP.NET传递客户ID到一个javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆