2009-11-29 5 views
7

데이터베이스에서 항목 목록을 가져 오는 드롭 다운 목록 컨트롤이 있습니다.데이터 바인딩 된 DropDownList에 항목 추가

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
     DataSourceID="SqlDataSource2" DataTextField="semester" 
     DataValueField="semester"> 
    </asp:DropDownList> 

그러나 처음에 목록 항목 하나를 "ALL"로 추가하고 싶습니다. 어떻게 추가 할 수 있습니까?

고마워!

+1

이 질문과 중복되는 것으로 보입니까? -> http://stackoverflow.com/questions/679128/add-empty-item-to-dropdownlist-of-custom-objects-in-c – JohnIdol

답변

18

DropDownList에 새 목록 항목을 추가하려면 속성 창에서 Items 속성의 줄임표를 클릭하십시오. 텍스트 "ALL"로 새 목록 항목 추가 & 값 -1.

아니면 DropDownList로이 마크 업을 추가하여 목록 항목을 추가 할 수 있습니다

<asp:DropDownList ID="categories" runat="server" ...> 
    <asp:ListItem Value="-1"> 
     ALL 
    </asp:ListItem>   
</asp:DropDownList> 

설정 DropDownList로의 특정 인덱스에 항목을 추가 할 수 있습니다 Items.Insert 방법으로 AppendDataBoundItems=True

+5

+1 AppendDataBoundItems, 그 속성을 몰랐어요. – Canavar

+2

그래, 그걸 생략하면 너를 신비화 할 수 있겠 니? – DOK

4

:

DropDownList1.Items.Insert(0, new ListItem("ALL", "ALL")); 
2

것은 각을 유지하지 않도록 DropDownList1viewstate을 해제해야합니다 db에서3210!

관련 문제