如何在.ascx页面中显示/隐藏表行

时间:2009-11-09 15:57:52

标签: c# asp.net javascript html

我尝试了这个,但无法通过: -

背后的代码

protected HtmlTableRow trComment;

protected void Page_Load(object sender, EventArgs e)
{
    //Show/Hide table rows (TR)
    trComment.Visible = ConfigUtil.DisplaySummaryComment;
}

.ascx page

<tr id="trComment" runat="server">
    <td style="vertical-align:top; text-align:left;">
        <%#ConfigUtil.FieldLabels["PIComments"]%>
        :
    </td>
    <td>
        <%= Test.Comment %>
    </td>
</tr>

4 个答案:

答案 0 :(得分:13)

您的原始代码不起作用,不是因为它不正确,而是因为您可能有更多地方trComment(在这种情况下它应该出错)或者因为您当前的代码位于某种模板内(在GridViewRepeater)。后者很有可能,因为您使用的是数据语句(<%#),它通常放在数据绑定控件模板中(但不一定)。

一种简单易用地解决这个问题的方法(存在多种方式,最好不要使用文字表)是使用asp:PlaceHolder,它不会留下HTML“痕迹”,但可以用来切换任何HTML / ASP.NET代码块:

<!-- toggle through OnLoad (can use ID as well) -->
<asp:PlaceHolder runat="server" OnLoad="MakeVisibleOrNot">
    <tr>
       ...
    </
</asp:PlaceHolder>

中的代码

protected void MakeVisibleOrNot(object sender, EventArgs e)
{
    ((Control) sender).Visible = ConfigUtil.DisplaySummaryComment;
}

答案 1 :(得分:4)

SELECT *, 1 AS RN FROM TABLE_A
UNION ALL 
SELECT *, 2 AS RN FROM TABLE_B
ORDER BY RN, COLUMN_1

然后在你的Page_Load()方法中找到你的元素并将可见性设置为true或false,如下所示

<tr id="trComment" runat="server">
   <td>

   </td>
</tr>

希望这有助于你

答案 2 :(得分:2)

尝试

trComment.Style.Add("display", "none");

答案 3 :(得分:0)

这也适用于

背后没有代码
namespace ExampleBundle\Cms\Loader;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Sonata\BlockBundle\Block\Loader\ServiceLoader;
use Sonata\BlockBundle\Model\BlockInterface;

/**
 * ORM block loader.
 */
class BlockLoader extends ServiceLoader
{
    /**
     * @var string
     */
    const TYPE_ACTION = 'cmf.block.action';

    /**
     * @var string
     */
    const TYPE_CONTAINER = 'cmf.block.container';

    /**
     * @var string
     */
    const TYPE_SIMPLE = 'cmf.block.simple';

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    protected $entityManager;

    /**
     * {@inheritdoc}
     */
    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;

        parent::__construct(array(
            self::TYPE_ACTION,
            self::TYPE_CONTAINER,
            self::TYPE_SIMPLE,
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function load($configuration): BlockInterface
    {
        $blockDefault = parent::load($configuration);

        $block = $this->getRepository($configuration['type'])->findOneByName($configuration['name']);

        return $block instanceof BlockInterface ? $block : $blockDefault;
    }

    /**
     * Get the repository for a given block type.
     *
     * @param string $type The block type.
     *
     * @return \Doctrine\ORM\Repository
     */
    protected function getRepository(string $type): EntityRepository
    {
        foreach ($this->entityManager->getMetadataFactory()->getAllMetadata() as $metadata) {
            if ($metadata->discriminatorValue === $type) {
                $baseName = str_replace($metadata->namespace . '\\', '', $metadata->name);
                return $this->entityManager->getRepository(sprintf('CmsBlock:%s', $baseName));
            }
        }

        throw new \RuntimeException(sprintf('Could not find repository for block type "%s%.', $type));
    }

    /**
     * {@inheritdoc}
     */
    public function support($configuration)
    {
        if (parent::support($configuration) && $this->exists($configuration['type'])) {
            return true;
        }

        return false;
    }
}