【问题标题】:asp.net hidden field not setting new value, need disabled alternativeasp.net 隐藏字段未设置新值,需要禁用替代
【发布时间】:2009-03-31 12:54:10
【问题描述】:

我的页面上有一个隐藏字段

<input runat="server" type="hidden" id="selectedIndex" />

它是由这组代码设置的,一个网格视图行的onclick事件:

var gridViewCtlId = '<%=GridView.ClientID%>';
var selectedIndex = '#<%=selectedIndex.ClientID%>';
var itemVisible = '<%=ItemVisible.ClientID%>';
var gridViewCtl = null;
var curSelRow = null;
var previousRowIndx = null;

window.onload = function showQuery()
{
    if ($(selectedIndex).val() != undefined)
    {
        if ($(selectedIndex).val() != '')
        {
            var prevRowID = $(selectedIndex).val();
            var prevRow = getSelectedRow(prevRowID);
            prevRow.style.backgroundColor = '#1A8CD4';
        }
    }       
}

 function getGridViewControl(rowIdx)
{
    if (gridViewCtl == null)
    {
        gridViewCtl = document.getElementById(gridViewCtlId);
    }
}

function onGridViewRowSelected(rowIdx)
{   
    if (document.getElementById(gridViewCtlId).disabled == false)
    {
        var selRowCCA = getSelectedRow(rowIdx);
        if (curSelRow != null)
        {   
            var previousRow = getSelectedRow(previousRowIndx);

            var CountIdx = previousRowIndx % 2;
            if (document.getElementById(itemVisible) == null)
            {
                if (CountIdx == 0)
                {
                    previousRow.style.backgroundColor = 'Silver';
                }
                else
                {
                    previousRow.style.backgroundColor = 'White';
                }
            }
        }

        if (null != selRow)
        {
            previousRowIndx = rowIdx;
            curSelRow = selRow;
            selRow.style.backgroundColor = '#1A8CD4';
        }
    }
}
function getSelectedRow(rowIdx)
{
    getGridViewControl(rowIdx);
    if (gridViewCtl != null)
    {
        $(selectedIndex).val(rowIdx);
        return gridViewCtl.rows[rowIdx];
    }
    return null;
}

会发生这种情况:当页面第一次加载时,隐藏字段是未定义的,它应该是。当我点击一行,然后点击“选择”按钮,然后调用它:

GridView.Attributes.Add("disabled", "true");

gridview 被禁用(与选择按钮一起),并且出现另一个 gridview(这应该取决于在第一个 gridview 中选择的内容)。所以现在,问题来了。当我单击 gridview 中的一行时(我只是在谈论初始的 gridview,而不是出现的第二个,这在这里不是问题),然后单击选择,所有内容都变灰并且大多数时候,页面加载时选定的行将突出显示(其他时候由于某种原因默认为第 2 行)。然后,假设您单击第 4 行,然后单击第 1 行,然后单击选择,由于某种原因,第 4 行将保持突出显示,然后第 4 行的数据将填充第二个网格视图,就像您从未单击过第 1 行一样。但是如果我单击第 4 行然后单击第 1 行,然后再次单击第 1 行,是否保存。有谁知道为什么会这样?

Also, I'm pretty much trying to disable the first gridview when select is hit so I do

GridView.Attributes.Add("disabled", "true");

而不是 GridView.Enabled = false;

如果用户重新单击搜索按钮(之前位于页面上的另一个按钮,使此 gridview 变得可见),我希望辅助 gridview 变为隐藏,而主 gridview(这个有问题)变为重新启用。但是在做

GridView.Attributes.Add("disabled", "false");

当触发搜索按钮时,只会禁用网格视图,这很奇怪。现在我知道除了 IE 之外的任何其他浏览器都不支持禁用字段,我只使用它是因为我需要检查 gridview 是否被禁用,因此用户在做出选择后无法单击另一行(发生这种情况如果我不执行以下操作:

if (document.getElementById(gridViewCtlId).disabled == false)

那么谁能告诉我完成这项任务的另一种方法?再次提前感谢。

【问题讨论】:

    标签: asp.net javascript gridview hidden


    【解决方案1】:

    关于禁用的一些信息:

    • 浏览器不会将任何禁用控件的值发送到服务器。这是根据定义。
    • 其他浏览器支持禁用字段,但它使用不同的模型。注意支持的浏览器列表:http://www.w3schools.com/tags/att_input_disabled.asp(以及它是如何定义的 disabled='disabled')。

    还可以查看它与只读的比较:http://www.w3.org/TR/html401/interact/forms.html#h-17.12.2

    还请注意,根据标准,它的支持仅限于某些元素。这很重要,因为您将它应用于不受支持的 html 元素,这也是它在您的场景中的其他浏览器中无法正常工作的可能原因。您可以使用脚本禁用支持的控件,让控件像 $get("someClientID").getElementsByTagName("input"); 一样应用它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      相关资源
      最近更新 更多