【问题标题】:Why are my rows not visible after being set to visible?为什么设置为可见后我的行不可见?
【发布时间】:2015-10-02 09:47:50
【问题描述】:

我有一个有六行的表格,但一开始只有两行可见(一个标题行和一个“业务”行)。

用户可以选择一个按钮,一次添加一个附加行(实际上,它只是使现有行可见),总共最多六行/五个“业务”行。

第 2-6 行一开始被此代码隐藏:

foapalrow3.Style["display"] = "none";

(foapalrow4、foapalrow5 和 foapalrow6 的代码相同)。

然后当用户选择“+”按钮时,它们会通过 jQuery 变得可见:

/* This makes the next hidden row visible, as long as there is one */
$(document).on("click", '[id$=btnAddFoapalRow]', function (e) {
    $('[id$=foapalhtmltable]').find('tr:hidden:first').show();
});

这很好用。问题是,当提交表单时,先前可见的行恢复为隐藏(除了前两个之外)。我正在尝试添加将重新显示这些行的代码如果其中的任何文本输入都被赋予了一个值。 IOW,如果我在每一行中给“索引”列一个值,如下所示:

....我希望它们能够再次可见,因为它们确实具有价值:

...它们的可见属性确实是“真”:

...但这些行仍然顽固,躲在他们的洞穴里。这是其背后的代码:

private void btnSave_Click(object sender, EventArgs e)
{
    try
    {
        . . . // code elided for brevity
        ConditionallyCreateList();
        SaveInputToList();
        listOfListItems = ReadFromList();
        message.Text = "Saving the data has been successful";

        // Expose any rows with vals
        if (RowContainsVals(3))
        {
            foapalrow3.Visible = true;
        }
        if (RowContainsVals(4))
        {
            foapalrow4.Visible = true;
        }
        if (RowContainsVals(5))
        {
            foapalrow5.Visible = true;
        }
        if (RowContainsVals(6))
        {
            foapalrow6.Visible = true;
        }

        . . . // code elided for brevity
    }
    catch (Exception ex)
    {
        message.Text = String.Format("Exception occurred: {0}", ex.Message);
    }
}

private bool RowContainsVals(int rownum)
{
    bool rowdirty = false;
    switch (rownum)
    {
        case 3:
            rowdirty = ((!String.IsNullOrEmpty(boxFund2.Text)) ||
                (!String.IsNullOrEmpty(boxIndex2.Text)) ||
                (!String.IsNullOrEmpty(boxOrganization2.Text)) ||
                (!String.IsNullOrEmpty(boxAccount2.Text)) ||
                (!String.IsNullOrEmpty(boxActivity2.Text)) ||
                (!String.IsNullOrEmpty(boxAmount2.Text)));
            break;
        case 4:
            rowdirty = ((!String.IsNullOrEmpty(boxFund3.Text)) ||
                (!String.IsNullOrEmpty(boxIndex3.Text)) ||
                (!String.IsNullOrEmpty(boxOrganization3.Text)) ||
                (!String.IsNullOrEmpty(boxAccount3.Text)) ||
                (!String.IsNullOrEmpty(boxActivity3.Text)) ||
                (!String.IsNullOrEmpty(boxAmount3.Text)));
            break;
        case 5:
            rowdirty = ((!String.IsNullOrEmpty(boxFund4.Text)) ||
                (!String.IsNullOrEmpty(boxIndex4.Text)) ||
                (!String.IsNullOrEmpty(boxOrganization4.Text)) ||
                (!String.IsNullOrEmpty(boxAccount4.Text)) ||
                (!String.IsNullOrEmpty(boxActivity4.Text)) ||
                (!String.IsNullOrEmpty(boxAmount4.Text)));
            break;
        case 6:
            rowdirty = ((!String.IsNullOrEmpty(boxFund5.Text)) ||
                (!String.IsNullOrEmpty(boxIndex5.Text)) ||
                (!String.IsNullOrEmpty(boxOrganization5.Text)) ||
                (!String.IsNullOrEmpty(boxAccount5.Text)) ||
                (!String.IsNullOrEmpty(boxActivity5.Text)) ||
                (!String.IsNullOrEmpty(boxAmount5.Text)));
            break;
        default:
            rowdirty = false;
            break;
    }
    return rowdirty;
}

但是,这就是我在代码运行后看到的全部内容:

那么为什么将 visible 属性设置为 visible 并没有真正将它们设置为可见?

更新

注意:如果我通过“+”按钮重新展开行,它们确实包含我添加到它们的值。所以这些行活着并保留了他们的数据,他们只是不想展示自己......

【问题讨论】:

  • 看看你的page_load中是否有你所有的逻辑在if(!IsPostBack)中,还有你的标记如何?,Control.Visible = false与Control.Style[“display”]不一样= "none",当控件 visible = false 时,它​​不是客户端的渲染器,而 dispay none 将在客户端隐藏它
  • 您能否扩展第一部分 - 我没有 IsPostBack - 这是我可以调用的标准 bool 函数,还是自定义方法?
  • 这是标准布尔值。只需使用 if(!Page.IsPostBack){}
  • 而不是设置 Visible = true,我应该是:foapalrow3.Style["display"] = "table-row"; ?
  • @B.ClayShannon 我会的,很高兴它帮助了你

标签: javascript c# jquery html css


【解决方案1】:

从此更改我的代码:

// Expose any rows with vals
if (RowContainsVals(3))
{
    foapalrow3.Visible = true;
}
if (RowContainsVals(4))
{
    foapalrow4.Visible = true;
}
if (RowContainsVals(5))
{
    foapalrow5.Visible = true;
}
if (RowContainsVals(6))
{
    foapalrow6.Visible = true;
}

...到这个:

// Re-visiblize any rows that contain vals
if (RowContainsVals(3))
{
    foapalrow3.Style["display"] = "table-row";
}
if (RowContainsVals(4))
{
    foapalrow4.Style["display"] = "table-row";
}
if (RowContainsVals(5))
{
    foapalrow5.Style["display"] = "table-row";
}
if (RowContainsVals(6))
{
    foapalrow6.Style["display"] = "table-row";
}

...有效。

【讨论】:

    【解决方案2】:

    尝试更改您的代码以使用Style["display"] = "table-row"; 而不是Visible = true;

    您说通过设置样式display:none. 使行后面的代码“不可见”

    请注意,在 asp.net 中制作控件。可见 与使用样式使其不可见 不一样

    Control.Visible = false 不会在客户端呈现 HTML 代码,而设置 display:none 将使用在浏览器上隐藏该行的样式呈现它。我假设这是你的情况,因为你说你隐藏和显示客户端的行,为了做到这一点,它们必须存在于客户端,所以我假设你的代码中没有任何地方它们是Visible = false;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2011-07-08
      相关资源
      最近更新 更多