2013-01-04 32 views
0

我們想用一個默認值填充一個DropDownList,當用戶從一個ASP.Net DetailsView進入「添加」模式時從一個變量中獲得一個默認值VB.Net代碼隱藏文件。你能告訴我如何讓它填充?在進入添加模式時填充ASP.Net DetailsView DropDownList和默認值

這是我們要填充爲DropDownList標記:

<asp:TemplateField HeaderText="Class:" SortExpression="ClassID"> 
    <EditItemTemplate> 
     <asp:DropDownList 
      ID="DropDownListClass" 
      Runat="server" 
      DataSourceID="SqlDataSourceClasses" 
      DataTextField = "ClassName" 
      DataValueField="ID" 
      SelectedValue='<%# Bind("ClassID") %>' 
      ForeColor="Blue"> 
     </asp:DropDownList> 

     <asp:RequiredFieldValidator ID="RequiredFieldValidatorEditClass" runat="server" ControlToValidate="DropDownListClass" 
      ErrorMessage="Please select a Class here." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
      SetFocusOnError="True" Display="Dynamic"> 

     </asp:RequiredFieldValidator> 

    </EditItemTemplate> 

    <InsertItemTemplate> 

     <asp:DropDownList 
      ID="DropDownListClass" 
      Runat="server" 
      DataSourceID="SqlDataSourceClasses" 
      DataTextField = "ClassName" 
      DataValueField="ID" 
      SelectedValue='<%# Bind("ClassID") %>' 
      ForeColor="Blue"> 
     </asp:DropDownList> 

     <asp:RequiredFieldValidator ID="RequiredFieldValidatorInsertClass" runat="server" ControlToValidate="DropDownListClass" 
      ErrorMessage="Please select a Class here." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
      SetFocusOnError="True" Display="Dynamic"> 

     </asp:RequiredFieldValidator> 
    </InsertItemTemplate> 

    <ItemTemplate> 
     <asp:DropDownList 
      ID="DropDownListClass" 
      Runat="server" 
      DataSourceID="SqlDataSourceClasses" 
      DataTextField = "ClassName" 
      DataValueField="ID" 
      SelectedValue='<%# Bind("ClassID") %>' 
      Enabled="false" 
      ForeColor="Blue" 
      Font-Bold="true"> 
     </asp:DropDownList> 
    </ItemTemplate> 
</asp:TemplateField> 

我們在代碼隱藏文件中使用它來填充將包含默認值的變量:

Function GetValueFromDropDownListClassItem() As String 

    Dim strValueToReturn As String 
    Dim drpValue As DropDownList 

    drpValue = DetailsView.FindControl("DropDownListClass") 

    If String.IsNullOrEmpty(drpValue.Text) Then 
     strValueToReturn = "" 
    Else 
     strValueToReturn = drpValue.SelectedItem.Text 
    End If 

    Return strValueToReturn 
End Function 

我們只是想使用這個值,並使用此變量的值預先選擇DropDownList。

回答

2

編輯:

您的意思是這樣的?

Protected Sub dropdown_DataBound(sender As Object, e As EventArgs) 
    Dim list As DropDownList = TryCast(sender, DropDownList) 
    Dim value as String = GetValueFromDropDownListClassItem() 
    If list IsNot Nothing And value IsNot "" Then 
     list.SelectedValue = value 
    End If 
End Sub 

舊消息: 請嘗試以下方法:

<asp:DropDownList 
    ID="DropDownListClass" 
    Runat="server" 
    DataSourceID="SqlDataSourceClasses" 
    DataTextField = "ClassName" 
    DataValueField="ID" 
    SelectedValue='<%# Bind("ClassID") %>' 
    AppendDataBoundItems="True" 
    ForeColor="Blue"> 
     <asp:ListItem Selected="True" Value="0">Please select</asp:ListItem> 
</asp:DropDownList> 

請注意AppendDataBoundItems真實的listItem

+0

備註:您可以添加此只是爲了InsertTemplate –

+0

感謝您的答覆,但我們正在尋找不添加新值,而是選擇從包含要選擇的值的變量中獲取的DropDownList中的現有值。 –

+0

就像我發佈在我的編輯? –