0

I'm using this code to dynamically generate imagebutton when retrieve images in img folder in ASP.net Web Application Project.

private void OpenImage()
{
    foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/img/")))
    {
          ImageButton imageButton = new ImageButton();
          FileInfo fileInfo = new FileInfo(strFileName);
          imageButton.ImageUrl = "~/img/" + fileInfo.Name;
          imageButton.Width = Unit.Pixel(100);
          imageButton.Height = Unit.Pixel(100);
          imageButton.Style.Add("padding", "10px");
          imageButton.Click += new ImageClickEventHandler(imageButton_Click);
          Panel1.Controls.Add(imageButton);
     }
 }

Now I want to display this within div tag that also generate dynamically. Any Suggestions Guys?

2
  • 1
    What is your question or problem? Does your code add a imageButton or not? Commented Dec 18, 2013 at 15:24
  • No the code generate imagebutton dynamically. Commented Dec 18, 2013 at 15:26

1 Answer 1

3

You can create a <div> (or any plain HTML element) in code behind with the HtmlGenericControl class, and use that to wrap your ImageButton any way you want. For example:

private void OpenImage()
{
    foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/img/")))
    {
        ImageButton imageButton = new ImageButton();
        ...
        HtmlGenericControl div = new HtmlGenericControl("div");
        div.Attributes["class"] = "my-class"; // or div.Attributes.Add("class", "my-class"); 
        div.Controls.Add(imageButton);
        Panel1.Controls.Add(div);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanx @p.s.w.g for the help. :) Is there any way to give that div a class name??
ha ha work fine..! now I can preserve responsiveness of my template using that class. thanx again @p.s.w.g

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.