简体   繁体   English

如何使用JavaScript变量在JavaScript函数中获取ASP.NET ClientID?

[英]How can I use a JavaScript variable to get an ASP.NET ClientID inside a JavaScript function?

I'd like to be able to get a ASP.NET control's ClientID inside of a JavaScript function. 我希望能够在JavaScript函数中获得ASP.NET控件的ClientID。 I also would like to be able to pass in a var containing a control name so that different controls can use the same function. 我还希望能够传入包含控件名称的var,以便不同的控件可以使用相同的函数。

For example, if I wanted to use a control named Button1, this works fine: 例如,如果我想使用名为Button1的控件,这可以正常工作:

function doStuffToButton1Control()
{
    var buttonControl = document.getElementById('<%= Button1.ClientID %>');
    //do stuff with buttonControl
}

But I get an error when I try to make the control name dynamic. 但是当我尝试使控件名称动态时,我收到错误。 I'm trying to string together the ClientID call, like so: 我正在尝试将ClientID调用串起来,如下所示:

function doStuffToGenericControl(controlName)
{
    var dynamicControl = document.getElementById('<%= ' + controlName + '.ClientID %>');
    //do stuff with dynamicContorl
}

I've tried using double quotes or single quotes when creating the string, but the error is always "Too many characters in string literal" or "Too many characters in character literal". 我在创建字符串时尝试使用双引号或单引号,但错误总是“字符串文字中的字符太多”或“字符文字中的字符太多”。 The control exists and the name passes correctly (as tested by an alert(name) ). 控件存在且名称正确传递(由alert(name)测试)。

Is there a better way to go about this, or am I missing something obvious? 有没有更好的方法来解决这个问题,还是我错过了一些明显的东西?

Thanks! 谢谢!

Update: This function will be on a master page. 更新:此功能将在母版页上。 Any number of child pages will be calling it. 任意数量的子页面都会调用它。 For example, one child page might have an ASP control that calls it like so: 例如,一个子页面可能有一个ASP控件,可以像这样调用它:

<asp:LinkButton ID="Button2" runat="server" OnClientClick="doStuffToGenericControl('Button2')">...</asp:LinkButton>

But another page might have this ASP control that also calls it: 但另一个页面可能有这个ASP控件也调用它:

    <asp:LinkButton ID="Button4" runat="server" OnClientClick="doStuffToGenericControl('Button4')">...</asp:LinkButton>

However, these controls' IDs often change by ASP.NET on render, so they end up looking, for example, like MainContent_Button2 instead of just Button2 . 但是,这些控件的ID经常在渲染时由ASP.NET更改,因此它们最终会看起来像MainContent_Button2而不仅仅是Button2 To get around this, I traditionally just used <%= Button1.ClientID %> , but since I want any number of controls to be able to use this one function, I'm having a bit of trouble. 为了解决这个问题,我传统上只使用<%= Button1.ClientID %> ,但由于我希望任何数量的控件能够使用这一个函数,所以我遇到了一些麻烦。

Update 2: I was able to use this along with a getAttribute call to get what I needed. 更新2:我能够使用thisgetAttribute调用来获得我需要的东西。 (Obviously this will only work when the control you are referring to is the one you are clicking, but that is what I needed) Like so: (显然这只会在你所指的控件是你点击的控件时起作用,但这就是我所需要的)像这样:

function doStuffToGenericControl(controlName)
{
     var controlID = controlName.getAttribute('id');
     var control = document.getElementById(controlID);
     //do stuff with control
}

' <%= Button1.ClientID %>' is something that replaced in with control id on asp.net page render, ' <%= Button1.ClientID %>'取代了asp.net页面渲染中的控件ID,

but when you pass variable in client side, you should pass id and use it without any % scops 但是当你在客户端传递变量时,你应该传递id并在没有任何%scops的情况下使用它

so try this 试试吧

function getGenericControl(control)
{
    var dynamicControl = control;
    alert(dynamicControl);
    //do stuff with dynamicContorl
}

UPDATE UPDATE

You can use <%= ClientID %> to generate parameters for the method above, so just change OnClientClicks like this and use method above 您可以使用<%= ClientID%>为上述方法生成参数,因此只需更改OnClientClicks并使用上面的方法

<asp:LinkButton ID="Button2" runat="server" OnClientClick="doStuffToGenericControl(this)">...</asp:LinkButton>

Rather than passing control name why don't you pass the control itself by passing this in the function like this: 而不是传递控件名称为什么不通过在函数中传递这个来传递控件本身:

  <asp:LinkButton ID="Button2" runat="server" OnClientClick="doStuffToGenericControl(this)">...</asp:LinkButton>

and your function will be : 你的功能将是:

function getGenericControl(control)
{
    var dynamicControl = control;
    alert(dynamicControl.id);
    //do stuff whatever you want with dynamicContorl
}

I would strongly suggest using a class if that is possible, 如果可能,我强烈建议使用课程,

var elems = document.getElementsByTagName('*'), i;
    for (i in elems) {
        if((' ' + elems[i].className + ' ').indexOf(' ' + matchClass + ' ')
                > -1) {
            elems[i].innerHTML = content;
        }
    }

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

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