简体   繁体   English

如何设置HtmlTableRow的高度,以使其生成但不显示?

[英]How can I set the height of an HtmlTableRow so that it will be generated but not display?

I need to dynamically display rows in an HtmlTable. 我需要在HtmlTable中动态显示行。 I thought setting the height of the HtmlTableRow to 0 would do the trick, but it doesn't. 我以为将HtmlTableRow的高度设置为0可以解决问题,但事实并非如此。 Here's how I create the row and its "pieces parts" in the code-behind: 这是我在后面的代码中创建行及其“部分”的方式:

// Row 3
foapalrow3 = new HtmlTableRow();
foapalrow3.ID = "foapalrow3";

var cellColIndex2 = new HtmlTableCell();
cellColIndex2.Width = CELL_WIDTH;
foapalrow3.Cells.Add(cellColIndex2);
var cellColFund2 = new HtmlTableCell();
cellColFund2.Width = CELL_WIDTH;
foapalrow3.Cells.Add(cellColFund2);
var cellColOrg2 = new HtmlTableCell();
cellColOrg2.Width = CELL_WIDTH;
foapalrow3.Cells.Add(cellColOrg2);
var cellColAccount2 = new HtmlTableCell();
cellColAccount2.Width = CELL_WIDTH;
foapalrow3.Cells.Add(cellColAccount2);
var cellColActivity2 = new HtmlTableCell();
cellColActivity2.Width = CELL_WIDTH;
foapalrow3.Cells.Add(cellColActivity2);
var cellColAmount2 = new HtmlTableCell();
cellColAmount2.Width = CELL_WIDTH;
foapalrow3.Cells.Add(cellColAmount2);

boxIndex2 = new TextBox()
{
    CssClass = "dplatypus-webform-field-input",
    Width = TEXTBOX_WIDTH,
    ID = "boxIndex2foapalrow3"
};
cellColIndex2.Controls.Add(boxIndex2);

boxFund2 = new TextBox()
{
    CssClass = "dplatypus-webform-field-input",
    Width = TEXTBOX_WIDTH,
    ID = "boxFund2foapalrow3"
};
cellColFund2.Controls.Add(boxFund2);

boxOrganization2 = new TextBox()
{
    CssClass = "dplatypus-webform-field-input",
    Width = TEXTBOX_WIDTH,
    ID = "boxOrg2foapalrow3"
};
cellColOrg2.Controls.Add(boxOrganization2);

boxAccount2 = new TextBox()
{
    CssClass = "dplatypus-webform-field-input",
    Width = TEXTBOX_WIDTH,
    ID = "boxAccount2foapalrow3"
};
cellColAccount2.Controls.Add(boxAccount2);

boxActivity2 = new TextBox()
{
    CssClass = "dplatypus-webform-field-input",
    Width = TEXTBOX_WIDTH,
    ID = "boxActivity2foapalrow3"
};
cellColActivity2.Controls.Add(boxActivity2);

boxAmount2 = new TextBox()
{
    CssClass = "dplatypus-webform-field-input",
    Width = TEXTBOX_WIDTH,
    ID = "boxAmount2foapalrow3"
};
cellColAmount2.Controls.Add(boxAmount2);
//cellColAmount2.ID = ""; <= Maybe the cells need IDs too, like: ?

foapalHTMLTable.Rows.Add(foapalrow3);
foapalrow3.Height = "0"; // <= does this need something appended, such as "em" or such? Normal height may be 15

...and here is the jQuery to try to conditionally increase the height to make the row visible: ...这是jQuery,尝试有条件地增加高度以使行可见:

$(document).on("click", '[id$=btnAddFoapalRow]', function (e) {
    if ($('[id$=foapalrow3]').height.not("15")) {
        console.log('reached the foapalrow3 not visible branch');
        $('[id$=foapalrow3]').height(15);
    }
});

Unfortunately, the row is visible from the git-go; 不幸的是,该行在git-go中可见; setting the height to 0 doesn't prevent it from displaying "in all its glory" right away. 将高度设置为0并不阻止它立即显示“全部荣耀”。 Do I also need to set the heights of the HtmlTableCells and/or TextBoxes to 0? 我还需要将HtmlTableCells和/或TextBoxes的高度设置为0吗? Or what else is needed? 或者还需要什么?

Note: I realize that the jQuery probably will not work (the "height.not" is just a guess for now), but I need to solve the first problem first (that the row height is not being set to 0 and thus effectively making it invisible). 注意:我意识到jQuery可能不起作用(“ height.not”只是现在的猜测),但是我需要首先解决第一个问题(行高未设置为0,从而有效地使它不可见)。

UPDATE 更新

I hide the rows after creating them like so (in the C# code-behind/server-side code): 我像这样创建行后将其隐藏(在C#代码隐藏/服务器端代码中):

        foapalHTMLTable.Rows.Add(foapalrow3);
        foapalrow3.Visible = false;

...and have this in my client-side jQuery: ...并将其放在我的客户端jQuery中:

$(document).on("click", '[id$=btnAddFoapalRow]', function (e) {
    if ($('[id$=foapalrow3]').not(":visible")) {
        alert('reached the foapalrow3 not visible branch');
        $('[id$=foapalrow3]').toggle();
    }
    else if ($('[id$=foapalrow4]').not(":visible")) {
        alert('reached the foapalrow4 not visible branch');
        $('[id$=foapalrow4]').slideDown();
    }
});

...but it does not work - the invisiblized rows are never made visible (I do see the first alert - twice, in fact (I have to dismiss it twice in succession)). ...但是它不起作用-永远不会使不可见的行可见(我确实看到了第一个警报-实际上是两次(我必须连续两次将其关闭))。

UPDATE 2 更新2

I commented out the setting of the rows to invisible (visible = false). 我注释掉了行的设置为不可见(visible = false)。 I added this to the ready function: 我将其添加到ready函数中:

$(document).ready(function () {
    alert('The ready function has been reached'); /* This is a "sanity check" so it can be verified that this jQuery script is running/ran */
    $('[id$=foapalrow3]').toggle();
    $('[id$=foapalrow4]').toggle();
});

I do see the alert, but toggle does not toggle them from visible to -in. 我确实看到了警报,但是切换并没有将它们从可见切换为-in。 I also tried .slideUp(), and that also did nothing. 我也尝试了.slideUp(),但也没有执行任何操作。 Ravi suggests to "set display:none"; 拉维建议“设置display:none”; I'm willing to try that, but just how, and where? 我愿意尝试,但是如何,在哪里?

You could use JQuery toggle() to hide/show dynamically. 您可以使用JQuery toggle()动态隐藏/显示。 Or you could use display:none css property (as a class if you wish to). 或者,您可以使用display:none css属性(如果愿意,可以用作类)。 Both will generate html at the background but not show on the visible page. 两者都会在后台生成html,但不会在可见页面上显示。

This (sort of) works, but is kludgy: 这(某种)有效,但是很笨拙:

If I set not the height of the HtmlTableRow, but the height of the elements in the HtmlTableRow (TextBoxes/input = text) to 0, clicking the button does "add" that row (it increases the height of the elements, thus increasing the height of the parent, the HtmlTableRow, and its parent, the HtmlTable. 如果我未将HtmlTableRow的高度设置为0,而是将HtmlTableRow中的元素的高度设置为0(文本框/输入=文本),则单击按钮 “添加”该行(它会增加元素的高度,从而增加父级HtmlTableRow及其父级HtmlTable的高度。

But, due to just being "short" instead of hidden, that dormant row looks funkier than a dumpster prior to hitting the "+" button: 但是,由于只是“短”而不是隐藏,因此在单击“ +”按钮之前,该休眠行比垃圾箱更时髦:

在此处输入图片说明

I might be able to "sell" this as a feature ("the miniscule height of the sort-of-hidden row indicates that it is available, but not yet usable") but I doubt it, really. 我可能可以将此功能“出售”(“隐藏的行的极小高度表明它可用,但尚未使用”),但我确实对此表示怀疑。

So I'm still open to suggestions on how to polish this kludgification. 因此,我仍然愿意就如何完善这种集群化提出建议。

I'm accomplishing this like so: 我是这样实现的:

C# code-behind (server side): C#代码隐藏(服务器端):

boxIndex2 = new TextBox()
{
    CssClass = "dplatypus-webform-field-input",
    Width = TEXTBOX_WIDTH,
    Height = 0,
    ID = "boxIndex2foapalrow3"
};
. . . // similar code for the other elements (where their height is also set to 0) elided for brevity

NOTE: the HtmlTableRow is not being set to invisible - if I do that, it doesn't work at all (the row is invisible, but it stays that way even after selecting the "+" button). 注意: HtmlTableRow 不会设置为不可见-如果我这样做,它将根本无法工作(该行是不可见的,但即使选择了“ +”按钮,它仍保持这种方式)。

jQuery client-side: jQuery客户端:

$(document).on("click", '[id$=btnAddFoapalRow]', function (e) {
    var textboxHeight = 15;
    if ($('[id$=foapalrow3]').not(":visible")) {
    //if (($('[id$=boxIndex2foapalrow3]').height() == 0) {
        alert('reached the foapalrow3 height zilch branch');
        $('[id$=boxIndex2foapalrow3').height(textboxHeight);
        $('[id$=boxFund2foapalrow3').height(textboxHeight);
        $('[id$=boxOrg2foapalrow3').height(textboxHeight);
        $('[id$=boxAccount2foapalrow3').height(textboxHeight);
        $('[id$=boxActivity2foapalrow3').height(textboxHeight);
        $('[id$=boxAmount2foapalrow3').height(textboxHeight);
    }
    . . .
});

NOTE: Another conundrum is why the "if ($('[id$=foapalrow3]').not(":visible"))" test returns true, as the row should not really be invisible...??? 注意:另一个难题是为什么“ if($('[id $ = foapalrow3]')。not(”:visible“))”测试返回true,因为该行实际上应该是不可见的... ??? The commented-out test, which should return true: 注释掉的测试, 返回true:

if (($('[id$=boxIndex2foapalrow3]').height() == 0) {

...does not (it returns false). ...不(返回false)。

??? ???

UPDATE 更新

I was able to improve the jQuery to this: 我能够将jQuery改进为:

$(document).on("click", '[id$=btnAddFoapalRow]', function (e) {
    var textboxHeight = 15;
    if ($('[id$=boxIndex2foapalrow3]').height() == 0) {
        $('[id$=boxIndex2foapalrow3').height(textboxHeight);
        $('[id$=boxFund2foapalrow3').height(textboxHeight);
        $('[id$=boxOrg2foapalrow3').height(textboxHeight);
        $('[id$=boxAccount2foapalrow3').height(textboxHeight);
        $('[id$=boxActivity2foapalrow3').height(textboxHeight);
        $('[id$=boxAmount2foapalrow3').height(textboxHeight);
    }
    else if ($('[id$=boxIndex3foapalrow4]').height() == 0) {
        $('[id$=boxIndex3foapalrow4').height(textboxHeight);
        $('[id$=boxFund3foapalrow4').height(textboxHeight);
        $('[id$=boxOrg3foapalrow4').height(textboxHeight);
        $('[id$=boxAccount3foapalrow4').height(textboxHeight);
        $('[id$=boxActivity3foapalrow4').height(textboxHeight);
        $('[id$=boxAmount3foapalrow4').height(textboxHeight);
    }
});

...but it still isn't working quite right - when I click the button, both rows are added at the same time. ...但是它仍然无法正常工作-当我单击按钮时, 行同时添加。 I think it's because the jQuery seems to be running twice, but why is it doing that? 我认为这是因为jQuery似乎运行了两次,但是为什么这样做呢?

The pre-button-clicking state of the HtmlTable is now this: 现在,HtmlTable的单击前状态为:

在此处输入图片说明

UPDATE 3 更新3

I finally figured it out - I had two instances of the same Web Part on the page and that means that the jQuery from both instances ends up in the "View Source" and thus it runs twice. 我终于弄清楚了-我在页面上有两个相同Web部件的实例,这意味着来自这两个实例的jQuery最终出现在“查看源代码”中,因此它运行了两次。 By removing one of the instances of the Web Part, the problem was solved, and code directly above (in Update 2) works fine. 通过删除Web部件的一个实例,此问题得以解决,并且上面(在Update 2中)的代码可以正常工作。

The only "problem" now is that the rows aren't completely hidden prior to mashing the "+" button - their height is not great, but it is greater than 0. 现在唯一的“问题”是在捣碎“ +”按钮之前,这些行并未完全隐藏-它们的高度虽然不大,但大于0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jQuery来显示HtmlTableRow(开始隐藏/隐藏生命)? - How can I use jQuery to get an HtmlTableRow (which starts life invisible/hidden) to display? 如何通过jQuery显示以前隐藏的HtmlTableRow? - How can I show a formerly hidden HtmlTableRow via jQuery? 如何减小HtmlTableCell的宽度或以其他方式将HtmlTableRow中的两个控件结合在一起? - How can I reduce the width of an HtmlTableCell or otherwise join together two controls in an HtmlTableRow? 如何使我的 MAUI 应用程序的 window 固定,因此无法调整大小或设置最小高度/宽度? - How can I make the window of my MAUI app fixed, so not resizable or set a minimum height/width? 如何将HtmlTable添加到HtmlTableRow? - How to add HtmlTable to HtmlTableRow? 如何在浏览器中显示动态生成的bmp - how can I display dynamically generated bmp in browser 如何设置使用 Database First 生成的 DbContext 的执行策略? - How can I set the execution strategy of a DbContext generated with Database First? 如何提取order by子句,以便我可以使用逻辑来设置它? - How to extract an order by clause so I can use logic to set it? 如何在system.drawings.font中设置字体高度? - How can I set font height in system.drawings.font? 如何在维护到目前为止生成的堆栈跟踪的同时重新抛出内部异常? - How can I rethrow an Inner Exception while maintaining the stack trace generated so far?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM