2011-05-04 4 views
1

아무도 내가 ASP에서 기본 값을 선택하면 알 수 있습니까? : DropDownList?어떻게 내가 ASP에서 기본값을 선택할 수 있습니다 : DropDownList

다음은 내 코드입니다 :

<tr> 
    <th align="right"><strong>Test: </strong></th> 
    <td> 
     <asp:DropDownList ID="Test" runat="server" DataSourceID="dsTest" 
     DataValueField="TestID" DataTextField="TestName" AppendDataBoundItems="true"> 
     <asp:ListItem></asp:ListItem> 
     </asp:DropDownList> 
     <asp:SqlDataSource ID="dsTest" runat="server" ConnectionString="<%$ ConnectionStrings:Test %>" 
     SelectCommand="test_select" SelectCommandType="StoredProcedure"> 
     </asp:SqlDataSource> 
    </td> 
</tr> 

어떻게이 시험 ID입니다 표시 목록이 1 인 선택을 할 수 있습니까?

답변

4

기본적으로이 작업을 수행하려면 아래에 몇 가지 옵션이 있습니다. 당신은 코드에서이

<asp:ListItem Selected="True"></asp:ListItem> 

또는 할 수

Test.selectedIndex = 0; 
0

코드 뒤에서 데이터를 바인딩 한 후에 추가 할 수 있습니까?

<tr> 
    <th align="right"><strong>Test: </strong></th> 
    <td> 
     <asp:DropDownList ID="Test" runat="server" DataSourceID="dsTest" 
     DataValueField="TestID" DataTextField="TestName" AppendDataBoundItems="true" ondatabound="AddDefaultItem"> 
     <asp:ListItem></asp:ListItem> 
     </asp:DropDownList> 
     <asp:SqlDataSource ID="dsTest" runat="server" ConnectionString="<%$ ConnectionStrings:Test %>" 
     SelectCommand="test_select" SelectCommandType="StoredProcedure"> 
     </asp:SqlDataSource> 
    </td> 
</tr> 

은 그런 뒤에 코드 :

protected void AddDefaultItem(object sender, EventArgs e) 
{ 
    Test.Items.Insert(0, new ListItem("", "")); 
} 

UPDATE :이 코드를 사용하지 않는 값 1이있는 목록 항목을 할 수 있습니다처럼

마지막 문장을 읽은 후 그것을 소리 아래 :

Test.Items.FindByValue("1").Selected = true; 

희망 사항 중 하나가 적합합니다.

관련 문제