Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在进入添加模式时使用默认值填充ASP.Net DetailsView DropDownList_Asp.net_Vb.net_Default Value_Populate_Code Behind - Fatal编程技术网

在进入添加模式时使用默认值填充ASP.Net DetailsView DropDownList

在进入添加模式时使用默认值填充ASP.Net DetailsView DropDownList,asp.net,vb.net,default-value,populate,code-behind,Asp.net,Vb.net,Default Value,Populate,Code Behind,我们希望使用默认值填充DropDownList,该值是在用户从VB.Net代码隐藏文件进入ASP.Net详细信息视图的“添加”模式时从变量中获得的。你能告诉我怎么填充它吗 以下是我们希望填充的DropDownList的标记: <asp:TemplateField HeaderText="Class:" SortExpression="ClassID"> <EditItemTemplate> <asp:DropDownList

我们希望使用默认值填充DropDownList,该值是在用户从VB.Net代码隐藏文件进入ASP.Net详细信息视图的“添加”模式时从变量中获得的。你能告诉我怎么填充它吗

以下是我们希望填充的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>
我们只想使用此值,并使用此变量中的值预先选择DropDownList。

编辑:

你是说像这样

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 true和listItem

感谢您的回复,但我们不希望添加新值,而是希望在DropDownList中选择一个现有值,该值是从包含要选择的值的变量获得的。类似于我在编辑中发布的内容?
<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>