简体   繁体   中英

how to add imagebutton in asp.net linkButton?

How to do that dynamically C# ?


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

Forexample:


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

Try the following

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

if its a LinkButton, Then you dont need to add an ImageButton into it

instead it'd be much better if you do it this way

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

You can add an ImageButton to the Controls collection:

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

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM