gpt4 book ai didi

asp.net - 如何修复 "' ddlAssignedTo' 的 SelectedValue 无效,因为它不存在于项目列表中

转载 作者:行者123 更新时间:2023-12-04 09:41:02 25 4
gpt4 key购买 nike

我加载了gridview,gridview 有一个编辑和删除按钮。

我点击编辑,我得到,“ddlAssignedTo' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

我知道我收到此错误是因为 ddlAssignedTo 的值为 null - ddlAssignedTo 的数据库中不存在任何内容。

我想做的就是更新当前值。

所以,我的问题是,如果当前值为空,如何为 ddlAssignedTo 分配默认值,以便如果数据库上当前不存在值,则以默认值为准?

下面是一些代码:

标记:

<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], [reqrecdate], [reqrecfrom], [skillsets], [application], [hoursperweek], [fromdate], [todate], [status], [statusupdate], [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

最佳答案

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

此解决方案设置AppendDataBoundItems="true"DropDownList并创建一个 nullable ListItem 以便您的 DropDownList即使表中的字段为空,也会绑定(bind)。

应用于 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>

关于asp.net - 如何修复 "' ddlAssignedTo' 的 SelectedValue 无效,因为它不存在于项目列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16502905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com