Question

I am tring to add an ImageButton to a label using C# but I don't success.... Can someone help me?

Thanks

This is what I did- (Delete is the name of the label)

var img = new ImageButton();
img.ID = "delete" + i;
img.ImageUrl = "images/1395958363_meanicons_57.png";
img.OnClientClick = "Delete_Click";
img.Width = 48;
img.Height = 38;
Delete.Page.Form.Controls.Add(img);
Was it helpful?

Solution

If Delete is the name of the Label (I suggest renaming it DeleteLbl or something similar) then you're going go to the label, getting a reference to the page, then the form on the page, then adding the ImageButton to that. That's convoluted. Instead, I think you want to add it directly to the Label's controls.

Delete.Controls.Add(img);

OTHER TIPS

I don't think you should be adding an ImageButton control to a Label control. That doesn't seem like a semantic use of the Label control (which is supposed to be used to display text).

However, you generally want to add sub-controls to the .Controls collection of whatever control you're trying to add it into, not the Page's or Form's .Controls collection. Like this:

Delete.Controls.Add(img);

Instead of a Label, you should probably use a PlaceHolder control for this:

<asp:PlaceHolder ID="myControlHolster" runat="server" />

And then add your ImageButton to that:

myControlHolster.Controls.Add(img);

ou can use the following code, where 2.jpg is your button image:

<asp:button id="myButton" style="background-image:url('2.jpg'); background-color:red; cursor:hand;" Runat="server" Height="25px" BorderStyText="ButtonText" Width="200px" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top