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

时间:2021-11-24 07:03:56

I'd like to be able to get a ASP.NET control's ClientID inside of a JavaScript function. 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.

我希望能够在JavaScript函数中获得ASP.NET控件的ClientID。我还希望能够传入包含控件名称的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)).

我在创建字符串时尝试使用双引号或单引号,但错误总是“字符串文字中的字符太多”或“字符文字中的字符太多”。控件存在且名称正确传递(由警报(名称)测试)。

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

但是,这些控件的ID经常在渲染时由ASP.NET更改,因此它们最终会查找,例如,像MainContent_Button2而不是Button2。为了解决这个问题,我传统上只使用<%= Button1.ClientID%>,但由于我希望任意数量的控件能够使用这一个函数,所以我遇到了一些麻烦。

Update 2: I was able to use this along with a getAttribute call to get what I needed. (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:

更新2:我能够使用它和getAttribute调用来获得我需要的东西。 (显然这只会在你所指的控件是你点击的控件时才有效,但这就是我所需要的)像这样:

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

3 个解决方案

#1


1  

'<%= 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>

#2


1  

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
}

#3


0  

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;
        }
    }

#1


1  

'<%= 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>

#2


1  

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
}

#3


0  

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;
        }
    }