我可以以编程方式向 gridview 添加链接按钮吗?

发布于 2024-07-25 13:40:18 字数 1165 浏览 4 评论 0原文

我一直在研究一些类似的问题,但没有任何运气。 我想做的是有一个 gridview,对于某些项目显示链接按钮,对于其他项目显示超链接。 这是我当前拥有的代码:

public void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var data = (FileDirectoryInfo)e.Row.DataItem;
        var img = new System.Web.UI.HtmlControls.HtmlImage();
        if (data.Length == null)
        {
            img.Src = "/images/folder.jpg";
            var lnk = new LinkButton();
            lnk.ID = "lnkFolder";
            lnk.Text = data.Name;
            lnk.Command += new CommandEventHandler(changeFolder_OnCommand);
            lnk.CommandArgument = data.Name;
            e.Row.Cells[0].Controls.Add(lnk);
        }
        else
        {
            var lnk = new HyperLink();
            lnk.Text = data.Name;
            lnk.Target = "_blank";
            lnk.NavigateUrl = getLink(data.Name);
            e.Row.Cells[0].Controls.Add(lnk);
            img.Src = "/images/file.jpg";
        }
        e.Row.Cells[0].Controls.AddAt(0, img);
    }
}

其中第一个单元格是 TemplateField。 目前,所有内容都显示正确,但链接按钮不会引发命令事件处理程序,并且所有控件都会在回发时消失。

有任何想法吗?

I've been looking through some similar questions without any luck. What I'd like to do is have a gridview which for certain items shows a linkbutton and for other items shows a hyperlink. This is the code I currently have:

public void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var data = (FileDirectoryInfo)e.Row.DataItem;
        var img = new System.Web.UI.HtmlControls.HtmlImage();
        if (data.Length == null)
        {
            img.Src = "/images/folder.jpg";
            var lnk = new LinkButton();
            lnk.ID = "lnkFolder";
            lnk.Text = data.Name;
            lnk.Command += new CommandEventHandler(changeFolder_OnCommand);
            lnk.CommandArgument = data.Name;
            e.Row.Cells[0].Controls.Add(lnk);
        }
        else
        {
            var lnk = new HyperLink();
            lnk.Text = data.Name;
            lnk.Target = "_blank";
            lnk.NavigateUrl = getLink(data.Name);
            e.Row.Cells[0].Controls.Add(lnk);
            img.Src = "/images/file.jpg";
        }
        e.Row.Cells[0].Controls.AddAt(0, img);
    }
}

where the first cell is a TemplateField. Currently, everything displays correctly, but the linkbuttons don't raise the Command event handler, and all of the controls disappear on postback.

Any ideas?

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

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

发布评论

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

评论(3

江湖彼岸 2024-08-01 13:40:18

我认为您应该尝试在回发时强制重新绑定 GridView。 这将确保重新创建所有动态控件并重新附加其事件处理程序。 这也可以防止它们在回发后消失。

IOW,在回发时在 GridView 上调用 DataBind()

I think you should try forcing a rebind of the GridView upon postback. This will ensure that any dynamic controls are recreated and their event handlers reattached. This should also prevent their disappearance after postback.

IOW, call DataBind() on the GridView upon postback.

野侃 2024-08-01 13:40:18

您还可以在 Row_Created 事件中添加这些,然后就不必撤消 !PostBack 检查

You can also add these in the Row_Created event and then you don't have to undo !PostBack check

最笨的告白 2024-08-01 13:40:18

为什么不以声明方式创建按钮,并以声明方式创建连字符(使用文字控件),然后使用数据绑定语法并根据需要将控件的 Visible 属性的可见性设置为 true 或 false:

Visible='<%#((FileDirectoryInfo)Container.DataItem).Length == null) %>' 

类似的东西。

Why don't you create the button declaratively, and create the hypen declaratively (using a literal control) and then using data binding syntax and set the visibility that is Visible property of the controls to true or false as needed:

Visible='<%#((FileDirectoryInfo)Container.DataItem).Length == null) %>' 

Something like that.

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