asp.net – 如何修复“’ddlAssignedTo’有一个无效的SelectedValue,因为它在项目列表中不存在

前端之家收集整理的这篇文章主要介绍了asp.net – 如何修复“’ddlAssignedTo’有一个无效的SelectedValue,因为它在项目列表中不存在前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我加载了gridview,gridview有一个编辑和删除按钮.

我点击编辑,我得到,“ddlAssignedTo”有一个无效的SelectedValue,因为它在项目列表中不存在.
参数名称:值

我知道我收到此错误,因为fo ddlAssignedTo的值为null – ddlAssignedTo的db上没有任何内容.

我所要做的就是更新当前值.

所以,我的问题是,如果当前值为null,我如何为ddlAssignedTo分配一个默认值,这样如果db上当前没有值,默认值将占优势?

这是一些代码

标记

<asp:TemplateField HeaderText="Assigned To">
    <EditItemTemplate>
      <asp:DropDownList ID="ddlAssignedTo" runat="server" 
                        DataSourceID="SubjectDataSource"
                        DataTextField="fullname" DataValueField="empl_Id"
                        SelectedValue='<%# Bind("AssignedTo") %>'>
        <asp:ListItem Value="">--Select Name--</asp:ListItem>
      </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblAssigned" runat="server" 
                   Text='<% #Bind("fullname") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField

<asp:sqlDataSource ID="sqlDataSource1" runat="server" 
                   ConnectionString="<%$ConnectionStrings:ConnectionString %>"
                   SelectCommand="SELECT Distinct [rownum],[reqnum],AssignedTo,(empl_first + ' ' + empl_last) fullname,[reqrecdate],[reqrecfrom],[skillsets],[application],[hoursperweek],[fromdate],[todate],[status],[statusupdate],[statusupby] FROM [requestinfo] left join employee on requestInfo.AssignedTo=employee.empl_id ORDER BY [reqnum]" 
                   UpdateCommand="INSERT INTO [requestinfo] ([reqnum],[statusupby],[AssignedTo]) VALUES (@reqnum,@reqrecdate,@reqrecfrom,@skillsets,@application,@hoursperweek,@fromdate,@todate,@status,@statusupdate,@statusupby,@empl_id)">
    <DeleteParameters>
        <asp:Parameter Name="rownum" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="reqnum" Type="String" />
        <asp:Parameter DbType="DateTime" Name="reqrecdate" />
        <asp:Parameter Name="reqrecfrom" Type="String" />
        <asp:Parameter Name="skillsets" Type="String" />
        <asp:Parameter Name="application" Type="String" />
        <asp:Parameter Name="hoursperweek" Type="Int32" />
        <asp:Parameter DbType="DateTime" Name="fromdate" />
        <asp:Parameter DbType="DateTime" Name="todate" />
        <asp:Parameter Name="status" Type="String" />
        <asp:Parameter DbType="DateTime" Name="statusupdate" />
        <asp:Parameter Name="statusupby" Type="String" />
        <asp:Parameter Name="empl_id" Type="String" />
        <asp:Parameter Name="rownum" Type="Int32" />
    </UpdateParameters>
</asp:sqlDataSource>
<asp:sqlDataSource ID="SubjectDataSource" runat="server" 
                   ConnectionString="<%$ConnectionStrings:ConnectionString %>"
                   SelectCommand="SELECT empl_id,(empl_first + ' ' + empl_last) fullname FROM dbo.Employee order by empl_last">
</asp:sqlDataSource>

代码隐藏:

Protected Sub GridView1_RowUpdating(ByVal sender As Object,ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating

    Dim dd As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlstatus"),DropDownList)
    e.NewValues("status") = dd.SelectedItem.Text
    Dim ddAssigned As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlAssignedTo"),DropDownList)

    If String.IsNullOrEmpty(ddAssigned.SelectedValue) Then
        ddAssigned.SelectedValue = "shhhh"
    Else
        e.NewValues("empl_id") = ddAssigned.SelectedValue
    End If

    sqlDataSource1.DataBind()
End Sub

解决方法

看一下@ cosmin.onea在问题 ‘DropDownList1’ has a SelectedValue which is invalid because it does not exist in the list of items中提供的解决方

解决方案在DropDownList上设置AppendDataBoundItems =“true”并创建一个可空的ListItem,以便即使表中的字段为空,DropDownList也将绑定.

应用于OP的问题,以下代码段将提供在AssignedTo字段为空时选择的选项.

<asp:DropDownList ID="ddlAssignedTo" runat="server" 
    DataSourceID="SubjectDataSource" 
    DataTextField="fullname" DataValueField="empl_Id" 
    SelectedValue='<%# Bind("AssignedTo") %>' 
    AppendDataBoundItems="true">
        <asp:ListItem Text="Select" Value="" />
</asp:DropDownList>
原文链接:https://www.f2er.com/aspnet/247882.html

猜你在找的asp.Net相关文章