&所述;%= x.ClientID%GT;在反复的用户控件返回错误值 [英] <%= x.ClientID %> in repeated user control returning wrong value

查看:97
本文介绍了&所述;%= x.ClientID%GT;在反复的用户控件返回错误值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ASP.NET的用户控件,具有<%= myDiv.ClientID%>在HTML模板主办的一段JavaScript代码

I have asp .net user control that has <%= myDiv.ClientID %> in a piece of javascript hosted in the html template.

此控制被放置在页面上的次数。问题是,上面的语句总是返回的div的客户端id在页面上的最后一个控件。

This control is placed on a page a number of times. The problem is that the above statement is always returning the client id of the div in the last control on the page.

我已经检查了HTML和每个div被赋予了自己独特的ID。

I have checked the HTML and each div is given its own unique ID.

控制的完整的HTML是...

The full HTML of the control is...

<script type="text/javascript">
    function ToggleHelpText() {
        try {

            var e = document.getElementById("<%= divHelpText.ClientID %>");

            if (e.style.display == 'block')
                e.style.display = 'none';
            else
                e.style.display = 'block';
        }
        catch (err) {
            alert(err);
        }
    }
</script>
<asp:LinkButton ID="Help" OnClientClick="ToggleHelpText(); return false;" CausesValidation="false"
    OnClick="btnHelp_Click" runat="server">Help</asp:LinkButton>
<div id="divHelpText" runat="server" visible="true" style="display: none">
</div>

什么可能会导致这,什么是身边的工作?

What could be causing this and what is the work around?

感谢您

推荐答案

您的问题是,你定义函数 ToggleHelpText 多次。每个定义有一个控制硬codeD的ID到它,你只用最后的定义和最后一个ID留给他们互相覆盖。

Your problem is that you're defining the function ToggleHelpText multiple times. Each definition has the ID of one control hardcoded into it, and you're only left with the last definition and the last ID as they overwrite each other.

作为一个简单的步骤,改变你的code到

As a simple step, change your code to

<script type="text/javascript">
 function ToggleHelpText(id) {
     try {
          var e = document.getElementById(id);
          if (e.style.display == 'block')
             e.style.display = 'none';
         else
             e.style.display = 'block';
     }
     catch (err) {
         alert(err);
     }
 }
</script>
<asp:LinkButton
  ID="Help"
  OnClientClick='ToggleHelpText(\'<%= divHelpText.ClientID %>\'); return false;'
  CausesValidation="false"
  OnClick="btnHelp_Click" runat="server">Help</asp:LinkButton>
<div id="divHelpText" runat="server" visible="true" style="display: none">
  </div> 

这将继续多次重写的功能,但现在的ID本身不会被改写,因为它并不难codeD插入功能。

This will continue to overwrite the function multiple times, but now the ID itself will not be overwritten as it's not hardcoded into the function.

在未来你能弄清楚如何不多次定义函数。

In future you can figure out how not to define the function multiple times.

NB我在的OnClientClick 引用的一个有点不确定。如果它不工作,试试这个

NB I'm a little unsure of the quoting in the OnClientClick. If it doesn't work, try this

OnClientClick='ToggleHelpText(<%= "\"" + divHelpText.ClientID + "\""%>); return false;'

或尝试在code设置它:

or try setting it in code:

Help.OnClientClick =
    "ToggleHelpText('" +
    divHelpText.ClientID +
    "'); return false;";

这篇关于&所述;%= x.ClientID%GT;在反复的用户控件返回错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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