2

i want to create my imagebutton in code behind. so i use this code:

 LiteralControl ltr = new LiteralControl();

 ltr.Text = "<asp:ImageButton class=\"stylImage\" AlternateText=\"Signature\" runat=\"server\" ImageUrl=\"~/images/Workflow/digital-signature-pic.jpg\" OnCommand=\"Image_OnCommand\" CommandName=\"imgclick\"/>";

but it doesn't work. nothing display.

Any Idea?!

1
  • 1
    Make a new instance of ImageButton like @chridam and @Bex do and add that object to a placeholder or panel. Do that in the Page_Initmethod. Commented Aug 15, 2012 at 9:58

3 Answers 3

3

Instead of using a Literal control, add a panel control and include the ImageButton control in your panel. You should add the controls in the Page Init event

protected void Page_Init(object sender, EventArgs e)
{
    ImageButton imgBtn = new ImageButton();
    imgBtn.ID = "img_id";
    imgBtn.ImageUrl = "~/images/Workflow/digital-signature-pic.jpg";
    imgBtn.AlternateText= "Signature";
    imgBtn.Click += (source, args) =>
    {
        // do something
    };
    Panel1.Controls.Add(imgBtn);
}
Sign up to request clarification or add additional context in comments.

5 Comments

i've tried this. but the image does not load?! but when i use my code in directly in asp.net (not codebehind) the image load perfectly!
I've edited my answer to include the Page Init event that you should add the control in.
it works when i add my imagebutton in a panel. but when i add it in a table cell id doesn't work?! this is my code : HtmlTableRow row = new HtmlTableRow(); HtmlTableCell cell1 = new HtmlTableCell(); -- Creating image button as u said --cell.Controls.Add(img); cell.InnerText = "StatusName"; row.Cells.Add(cell); the text "StatusName" is shown, but there is no image!!! tblStatus.Rows.Add(row);
You created a table cell called cell1 here HtmlTableCell cell1 = new HtmlTableCell(); but you are adding the ImageButton control to another table cell called cell, not cell1.
it was just a mistyping, in real code both have the same name (cell).
0

You need to add it to a container on the page, see this article:

http://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx

1 Comment

i'm working in a web user control. and i add the imagebutton in a table cell. should i use Placeholder?!?!
0

You can't write a server side control to a literal. You have two options,

  • either set the image button directly on the page and change the visibility attribute
  • Or Add a placeholder or panel control and dynamically add the image button to it, something like this

            ImageButton btn = new ImageButton(); 
            btn.ImageUrl = "my image url";
    
            btn.Click += btn_Click;
    
            btn.ID = "create an ID";
            etc....
            ph.Controls.Add(btn);
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.