当用户从 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。