2013-12-18 3 views
0

이 코드를 사용하여 ASP.net 웹 응용 프로그램 프로젝트의 img 폴더에서 이미지를 검색 할 때 imagebutton을 동적으로 생성합니다.동적으로 C#을 사용하여 ASP.net 웹 응용 프로그램에서 div에 imagebutton을 생성하는 방법?

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); 
    } 
} 

이제 이것을 동적으로 생성하는 div 태그 내에 표시하고 싶습니다. 어떤 제안 녀석?

+1

무엇이 문제입니까? 코드에서 imageButton을 추가했는지 여부 – RononDex

+0

코드가 이미지 버튼을 동적으로 생성하지 않습니다. – Nishantha

답변

3

코드 뒤에 HtmlGenericControl 클래스로 <div> (또는 일반 HTML 요소)을 생성하고이를 사용하여 원하는 방식으로 ImageButton을 감쌀 수 있습니다. 예 :

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); 
    } 
} 
+0

도움말을 보려면 Thanx @ p.swg를 참조하십시오. :) 그 div 클래스 이름을 제공하는 방법이 있습니까 ?? – Nishantha

+1

@Nishantha 업데이트 된 답변보기 –

+0

하하 잘 작동 ..! 이제는 해당 클래스를 사용하여 템플릿의 응답을 보존 할 수 있습니다. 고맙습니다. @ p.s.w.g – Nishantha