2011-07-02 2 views
1

C# 코드에서 dropdownlist를 만들었습니다. 이 ddlist를 aspx 페이지에 표시하는 방법은 무엇입니까? 내 코드 :aspx-page의 DropDownList 표시

당신이 코드에서 컨트롤을 만든 이후
<% 
    DropDownList list; 
    for (int i = 0; i < 10; i++) 
    { 
     list = new DropDownList(); 
     list.ID = i + "_ID"; 
     %> 
     <!-- how to display drop down list here??? --> 
     <% 
    } 
%> 
+2

와 컨트롤에 DropDownList을 추가 누락'Visible' 속성 false로 설정되지 않았습니다 – Bastardo

답변

1

다음은 aspx 코드에있는 것입니다.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DenemeWebForm1.aspx.cs" Inherits="DenemeWebApplication.DenemeWebForm1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <div> 
      <table> 
       <tr> 
        <td> 
         <asp:Panel ID="PanelAmk" runat="server"> 
          <table> 
           <tr> 
            <td> 
            </td> 
           </tr> 
          </table> 
         </asp:Panel> 
        </td> 
       </tr> 
      </table> 
     </div> 
    </form> 
</body> 
</html> 

이 코드는 PanelAmk 이름 패널 등 아무것도 웹 페이지를 작성합니다.

다음은 기본적으로 위의 페이지에 DropDownList을 추가하는 aspx.cs 코드 (C# 코드)입니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace DenemeWebApplication 
{ 
    public partial class DenemeWebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      DropDownList dropdownlistAmk = new DropDownList();//creates a new dropdownlist 
      dropdownlistAmk.Items.Add("amk");//adds an item to dropdownlistAmk created above 
      PanelAmk.Controls.Add(dropdownlistAmk);//and adds dropwdownlistAmk to PanelAmk 

     } 
    } 
} 

당신이 당신이 브라우저에서 페이지를 열 때 그것은 그것 인 경우에 표시됩니다 추가 한 경우? 당신은 당신의 코드를 추가하십시오 수 ...Controls.Add(...)

+1

좋은 ID, 영원히 amk – zer0w1dthspace

0

, 컨트롤은 아직 페이지에 존재하지 않습니다. 페이지의 어딘가에 추가해야합니다. "myPanel"의 ID로 패널을 다음 코드를 통해 ddlist를 추가 다음과 같이 예를 들어, 당신은 장식 적 ASP를 만들 수 있습니다

myPanel.Controls.Add(ddlist); 

을이 과정에 대한 자세한 설명은 다음 MSDN 문서를 살펴 : How to: Add Controls to an ASP.NET Web Page Programmatically