【问题标题】:Cycle through controls in UpdatePanel, and access javascript循环浏览 UpdatePanel 中的控件,并访问 javascript
【发布时间】:2010-10-17 19:48:55
【问题描述】:

我正在尝试更新页面上的所有文本框,以将它们转换为如下标签:

foreach (Control ctrl in masterform.Controls)
{
    if (ctrl.GetType() == typeof(TextBox))
    {
        TextBox t = ctrl as TextBox;
        t.ReadOnly = true;
        t.BackColor = transparent;
        t.BorderWidth = 0;
    }
}

不幸的是,我将所有文本框都包含在更新面板中,并且无法再访问它们。所以我尝试了这个:

foreach (Control ctrl in masterform.Controls)
{
    if (ctrl is UpdatePanel)
    {
        UpdatePanel s = ctrl as UpdatePanel;
        if (s == PartPanel)
        {
            foreach (Control ctrl2 in s.Controls)
            {
                if (ctrl2 is TextBox)
                {
                    TextBox t = ctrl2 as TextBox;
                    t.ReadOnly = true;
                    t.BackColor = transparent;
                    t.BorderWidth = 0;
                }
            }
        }
    }
}

这只是显示面板控件计数为 1,但里面有很多文本框控件。任何帮助将不胜感激。

另外,我有相互排斥的复选框,它们的工作方式如下:如果选中#1,则无法选中#2 或#3,同样,如果选中#2 或#3,则无法选中#1。这意味着,如果选中了#2 和/或#3,而用户选中了#1,则#2 和#3 变为未选中,如果选中了#1,而用户选中了#2 和/或#3,则#1 变为未选中.我编写了以下函数来处理它,只要更新面板不更新,它就可以工作:

var objChkd;
$(document).ready(function() 
{
    $('.mutuallyexclusive1').click(function () 
    {
        checkedState = $(this).attr('checked');
        $('.mutuallyexclusive2:checked').each(function () 
        {
            $(this).attr('checked', false);
        });
        $(this).attr('checked', checkedState);
    });
    $('.mutuallyexclusive2').click(function () 
    {
        checkedState = $(this).attr('checked');
        $('.mutuallyexclusive1:checked').each(function () 
        {
            $(this).attr('checked', false);
        });
        $(this).attr('checked', checkedState);
    });
});  

<input id="Chk1" type="checkbox" runat="server" class="mutuallyexclusive1" /><br />
<input id="Chk2" type="checkbox"  runat="server" class="mutuallyexclusive2" /><br />
<input id="Chk3" type="checkbox" runat="server" class="mutuallyexclusive2" /><br />

问题是,当页面加载时,它们可以正常工作,但如果调用更新面板的 update() 函数,它们似乎会失去与应该调用的 javascript 的连接。同样,任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: asp.net javascript updatepanel


    【解决方案1】:

    对于你的 UpdatePanel,试试这个递归地找到你的文本框并更新它们:

    void DisableTextBoxes(Control parent)
    {
        foreach (Control ctrl in parent.Controls)
        {
            TextBox t = ctrl as TextBox;
            if (t != null)
            {
                t.ReadOnly = true;
                t.BackColor = transparent;
                t.BorderWidth = 0;
            }
            else
            {
                DisableTextBoxes(ctrl);
            }
        }
    }
    
    DisableTextBoxes(masterform);
    

    对于复选框,您的脚本在页面加载时执行,并且事件处理程序此时绑定到控件。如果复选框是在 UpdatePanel 刷新期间更新的 DOM 的一部分而没有重新加载完整的页面,则事件处理程序不再绑定到它们(它们是 new 元素)。部分刷新页面后,您需要再次绑定事件。

    【讨论】:

      猜你喜欢
      • 2020-12-13
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多