DataGrid 控件已禁用

发布于 2024-07-15 02:13:00 字数 1158 浏览 5 评论 0原文

我在 ASP.NET 2.0 中有一个 DataGrid,其中包含以下列:

<ASP:TEMPLATECOLUMN>
    <ItemStyle HorizontalAlign="Right"></ItemStyle>         
    <ItemTemplate>
        <asp:HyperLink id="HyperLink1" runat="server" 
            CssClass="DataGridCommand" Target="_blank"
            NavigateUrl='<%# GetPreviewURL(DataBinder.Eval(Container, "DataItem.NodeID")) %>'>
        Preview Graphic
        </asp:HyperLink>
    </ItemTemplate>
</ASP:TEMPLATECOLUMN>   

因此,基本思想是在 DataGrid 的每一行中都有一个链接,用于弹出图像预览(指的是数据绑定列)节点ID)。 这很好用。

我试图禁用没有关联图像的行的链接。 为此,我将其放在后面的代码中,在 Page_Load 下:

foreach (DataGridItem dgi in this.dgNode.Items)
{
    HyperLink myLink1 = (HyperLink)dgi.Cells[0].FindControl("HyperLink1");

    //myLink1.Visible = false;
    //if (condition for hiding links goes here...)
    myLink1.Enabled = false;
    this.dgNode.DataBind();
}

如您所见,我尝试将visible 属性设置为 false,但这不起作用。 我尝试将enable设置为false,但也不起作用。 然后我再次尝试对 DataGrid 进行数据绑定 - 不起作用。 这些链接继续正常运行。

有谁知道为什么这不起作用,可以采取什么措施使其起作用,如果它存在根本缺陷,是否有替代解决方案? 我希望将某些行的链接灰显,或隐藏它以使其无法单击。

I have a DataGrid in ASP.NET 2.0 with the following column in it:

<ASP:TEMPLATECOLUMN>
    <ItemStyle HorizontalAlign="Right"></ItemStyle>         
    <ItemTemplate>
        <asp:HyperLink id="HyperLink1" runat="server" 
            CssClass="DataGridCommand" Target="_blank"
            NavigateUrl='<%# GetPreviewURL(DataBinder.Eval(Container, "DataItem.NodeID")) %>'>
        Preview Graphic
        </asp:HyperLink>
    </ItemTemplate>
</ASP:TEMPLATECOLUMN>   

So the basic idea is to have a link in each row of the DataGrid that kicks back a pop-up image preview (referring to a data-bound column for the NodeID). This works just fine.

I am attempting to disable the links for the rows for which there is no associated image. To do this I put this in the code behind, under Page_Load:

foreach (DataGridItem dgi in this.dgNode.Items)
{
    HyperLink myLink1 = (HyperLink)dgi.Cells[0].FindControl("HyperLink1");

    //myLink1.Visible = false;
    //if (condition for hiding links goes here...)
    myLink1.Enabled = false;
    this.dgNode.DataBind();
}

As you can see, I tried setting the visible property to false, but that didn't work. I tried setting enable to false, it didn't work either. And then I tried databinding the DataGrid again -- doesn't work. The links continue to function normally.

Does anyone know why this doesn't work, what can be done to make it work, and if it's fundamentally flawed, an alternate solution? I'm looking to either grey out the link for certain rows, or hide it so that it can't be clicked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧话新听 2024-07-22 02:13:00

您可以在 OnRowDataBound< 上处理此问题/a> 事件,如下所示:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    HyperLink hl = (HyperLink)e.Row.FindControl("HyperLink1");
    hl.Visible = false;
}

You can handle this on the OnRowDataBound event, like so:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    HyperLink hl = (HyperLink)e.Row.FindControl("HyperLink1");
    hl.Visible = false;
}
送舟行 2024-07-22 02:13:00

您应该添加一个事件处理程序来处理 OnItemDataBound 事件,而不是循环访问页面加载时的项目。 然后,您可以在绑定每个项目时找到并禁用那里的超链接。

我相信当前的解决方案不起作用,因为它在将链接设置为不可见或禁用后调用 DataBind() 方法。 这将导致 DataGrid 自行重建并删除您刚刚所做的工作。 更不用说您为每个项目重新绑定 DataGrid,这会影响性能。

Instead of looping thru the items on page load, you should add an event handler to handle the OnItemDataBound event. You can then find and disable the HyperLink there as each item is bound.

I believe the current solution is not working because it calls the DataBind() method after setting the link to invisible or disabled. This will cause the DataGrid to rebuild itself and is erasing the work that you just did. Not to mention you rebind your DataGrid for each item, which is a performance hit.

屋檐 2024-07-22 02:13:00

我敢打赌,由于您禁用了超链接,然后在事后绑定数据网格,因此重新绑定本质上是通过其数据绑定代码再次“重新激活”链接。

作为替代方案,您是否考虑过使用 DataGrid 的 ItemDatabound 事件并在当时启用/禁用超链接? 所有数据都触手可及,并且代码看起来与现在几乎相同。

I would wager that since you're disabling the hyperlinks and then binding the datagrid after the fact, that the rebinding is essentially "reactivating" the links again through its databind code.

As an alternative, have you thought about consuming the ItemDatabound event of the DataGrid and enabling/disabling the hyperlinks at that time? You'll have all the data right at your fingertips and the code will look pretty much the same as you have now.

一绘本一梦想 2024-07-22 02:13:00

我不确定,但我认为 asp.net 正在匆忙,因为您确实有多个对象 Hyperlink1 的实例。 ID 属性不是用作唯一标识符,如果有多个实例,它可能会拒绝操作它。

I am not sure, but I think asp.net is rushing because you do have more than one instance of the object Hyperlink1. Isn't The ID proprety is used as unique identifier, if there is more than one instance maybe it refuses to manipulate it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文