【问题标题】:how to add imagebutton in asp.net linkButton?如何在asp.net linkBut​​ton中添加imagebutton?
【发布时间】:2011-10-24 15:13:29
【问题描述】:

如何动态地做到这一点 C# ?


    <asp:LinkButton ID="LinkButton1" runat="Server" >Text
<asp:ImageButton ID="ImageButton1" runat="Server" ImageUrl="~/images/Detail.png"></asp:ImageButton>
</asp:LinkButton>

例如:


LinkButton lnk = new LinkButton();
lnk.Add(new ImageButton());

【问题讨论】:

  • 你的例子有什么问题?只需这样做,还要设置 ID、Text 和其他需要的属性...
  • 他的例子不能编译,这就是问题所在:p

标签: c# asp.net dll


【解决方案1】:

试试下面的

var linkButton = new LinkButton() {
  ID = "LinkButton1"
};
linkButton.Controls.Add(new ImageButton() {
  ID = "ImageButton1",
  ImageUrl = "~/images/Detail.png"
});

【讨论】:

  • @Penguen 您是否将LinkButton 添加回原始页面?
【解决方案2】:

如果是 LinkBut​​ton,则不需要添加 ImageButton

如果你这样做会更好

<asp:LinkButton ID="MyLinkButton" runat="server" PostBackUrl="MyLink"><img src="MyImage" alt="MyLink" /></asp:LinkButton>

【讨论】:

    【解决方案3】:

    您可以将ImageButton 添加到Controls 集合中:

    LinkButton lnk = new LinkButton();
    lnk.Controls.Add(new ImageButton()); //set your image button's properties first
    

    【讨论】:

      【解决方案4】:

      试试:

      LinkButton lnk = new LinkButton();
      lnk.Controls.Add(new ImageButton());
      

      【讨论】:

      • 坦率地说,这是一件很奇怪的事情,你想达到什么目的?
      猜你喜欢
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多