【问题标题】:Code is not working in content place holder代码在内容占位符中不起作用
【发布时间】:2014-10-08 10:45:33
【问题描述】:

您好,我正在通过客户端的 javascript 代码将文本框设为空白。

如果我在没有母版页的情况下使用此代码,它可以完美运行,但我使用母版页它不起作用。为什么?

<script type="text/javascript">

  function clrCtrl() {
      var control = "<%=txtpassword.ClientID %>";
      var control2 = "<%=txtConfirmpassword.ClientID %>";
      document.getElementById(control).value = "";
      document.getElementById(control2).value = "";

  }
</script>

下面是我的单选按钮和下拉列表

<asp:RadioButtonList ID="RadioButtonList1" runat="server" CssClass="nbdcontent"     AutoPostBack="True"
                            RepeatDirection="Horizontal" Font-Bold="True" Font-Size="Medium"     onchange="clrCtrl()" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
                            <asp:ListItem Value="0">CPMU</asp:ListItem>
                            <asp:ListItem Value="1">SPMU</asp:ListItem>
                            <asp:ListItem Value="2">SFTI</asp:ListItem>
                        </asp:RadioButtonList>


<asp:DropDownList ID="ddlStates" runat="server" AutoPostBack="True" 
                            CssClass="mytextbox" DataTextField="State" DataValueField="State_Id" Enabled="False"
                            OnSelectedIndexChanged="ddlStates_SelectedIndexChanged1" 
                            onchange="clrCtrl()">
                            <asp:ListItem Value="0">-Select-</asp:ListItem>
                        </asp:DropDownList>

【问题讨论】:

  • 你能调试一下java脚本,看看它是否被执行了吗?
  • 不,它没有受到中断。我该怎么办?

标签: javascript master-pages contentplaceholder


【解决方案1】:

我创建了这个 js 函数来查找页面中的所有元素。如果元素(控件)id 没有前缀ct100_ContentPlaceholder1_(或类似的东西),如果不是masterconten 页面的内容,函数getElement 将立即返回控件/元素,将避免循环槽完成页面,否则将搜索。

function getElement(id) {
    if (document.getElementById(id) == null) {
        var pp = document.getElementsByTagName('*');
        for (var i = 0; i < pp.length; i++) {
            if (pp[i].id != null) {
                var ele = pp[i].id;
                if (ele.indexOf('_') > -1) {
                    //spliter is _
                    var tt = ele.split('_');
                    if (tt[tt.length - 1] == id) { return pp[i]; }
                }
                if (ele.indexOf('$') > -1) {
                    //spliter is $ 
                    var tt = ele.split('$');
                    if (tt[tt.length - 1] == id) { return pp[i]; }
                }
            }
        }
        // element not found, return nothing
        return null;
    } else {
        // element is not in contentplaceholder
        return document.getElementById(id);
    }
}

它将遍历页面中的所有元素。 然后你可以在你的代码中使用:

function clrCtrl() {
      getElement('txtpassword').value='';
      getElement('txtConfirmpassword').value='';
}

但是,有一点非常重要:不要在您的控件 ID 中使用 _ char。这是一个条件,否则function 将返回null。 例如。如果您的textbox idtxt_passwordfunction 将返回null,将找不到您的控件。

附言抱歉英语不好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2013-10-19
    • 2017-04-23
    • 2013-07-15
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    相关资源
    最近更新 更多