如何解决"'ddlAssignedTo'具有的SelectedValue这是无效的,因为它不会在项目列表中存在 [英] How to fix "'ddlAssignedTo' has a SelectedValue which is invalid because it does not exist in the list of items
问题描述
我加载GridView和GridView控件有一个编辑和删除按钮。
我点击编辑,我得到的, ddlAssignedTo'有SelectedValue的,因为它不会在项目列表中存在哪些无效。
参数名称:值
我知道,因为FO ddlAssignedTo
值为空,我得到这个错误 - 在DB的ddlAssignedTo存在什么。
所有我试图做的是更新的当前值。
所以,我的问题是这样的,如果当前值为空,我怎么分配的ddlAssignedTo一个默认值,这样,如果没有价值目前该数据库存在,则默认值将prevail?
下面是一些code:
标记:
< ASP:的TemplateField的HeaderText =入驻GT&;
<&EditItemTemplate的GT;
< ASP:DropDownList的ID =ddlAssignedTo=服务器
的DataSourceID =SubjectDataSource
DataTextField =全名DataValueField =empl_Id
的SelectedValue ='<%#绑定(AssignedTo)%>'>
< ASP:ListItem的值=> - 选择名称 - < / ASP:ListItem的>
< / ASP:DropDownList的>
< / EditItemTemplate中>
<&ItemTemplate中GT;
< ASP:标签ID =lblAssigned=服务器
文字='<%#Bind(全名)%>'>< / ASP:标签>
< / ItemTemplate中>
< / ASP:模板列< ASP:SqlDataSource的ID =SqlDataSource1=服务器
的ConnectionString =下;%$的ConnectionStrings:的ConnectionString%>中
的SelectCommand =SELECT DISTINCT [ROWNUM],[reqnum],AssignedTo,(empl_first +''+ empl_last)全名,[reqrecdate],[reqrecfrom],[技能],[应用],[hoursperweek],[寄件者],[ TODATE],[状态],[statusupdate],[statusupby] FROM [requestinfo]左连接上requestInfo.AssignedTo = employee.empl_id ORDER BY员工[reqnum]
更新命令=INSERT INTO [requestinfo]([reqnum],[reqrecdate],[reqrecfrom],[技能],[应用],[hoursperweek],[寄件者],[TODATE],[状态],[statusupdate],[ statusupby],[AssignedTo])VALUES(@reqnum,@reqrecdate,@reqrecfrom,@skillsets,@application,@hoursperweek,@fromdate,@todate,@status,@statusupdate,@ statusupby,@ empl_id)>
< DeleteParameters>
< ASP:参数名称=ROWNUMTYPE =的Int32/>
< / DeleteParameters>
< UpdateParameters>
< ASP:参数名称=reqnum类型=字符串/>
< ASP:参数的DbType =日期时间NAME =reqrecdate/>
< ASP:参数名称=reqrecfrom类型=字符串/>
< ASP:参数名称=技能类型=字符串/>
< ASP:参数名称=应用程序类型=字符串/>
< ASP:参数名称=hoursperweekTYPE =的Int32/>
< ASP:参数的DbType =日期时间NAME =寄件者/>
< ASP:参数的DbType =日期时间NAME =TODATE/>
< ASP:参数名称=状态类型=字符串/>
< ASP:参数的DbType =日期时间NAME =statusupdate/>
< ASP:参数名称=statusupby类型=字符串/>
< ASP:参数名称=empl_id类型=字符串/>
< ASP:参数名称=ROWNUMTYPE =的Int32/>
< / UpdateParameters>
< / ASP:SqlDataSource的>
< ASP:SqlDataSource的ID =SubjectDataSource=服务器
的ConnectionString =下;%$的ConnectionStrings:的ConnectionString%>中
的SelectCommand =SELECT empl_id,(empl_first +''+ empl_last)FROM dbo.Employee为了全名由empl_last>
< / ASP:SqlDataSource的>
codeBehind:
保护小组GridView1_RowUpdating(BYVAL发件人为对象,BYVAL E上GridViewUpdateEventArgs)处理GridView1.RowUpdating 昏暗DD作为DropDownList的= DirectCast(GridView1.Rows(e.RowIndex).FindControl(ddlstatus),DropDownList的)
e.NewValues(身份)= dd.SelectedItem.Text
昏暗ddAssigned作为DropDownList的= DirectCast(GridView1.Rows(e.RowIndex).FindControl(ddlAssignedTo),DropDownList的) 如果String.IsNullOrEmpty(ddAssigned.SelectedValue)然后
ddAssigned.SelectedValue =嘘
其他
e.NewValues(empl_id)= ddAssigned.SelectedValue
万一 SqlDataSource1.DataBind()
结束小组
看看在这个问题由@ cosmin.onea提供的解决方案的'DropDownList1'具有的SelectedValue这是无效的,因为它不会在项目列表中存在
此解决方案上设置DropDownList的AppendDataBoundItems =true,并创建一个为空的列表项,这样,在该表中的字段为空你的DropDownList甚至会进行绑定。
应用到OP的问题,下面的片段时,AssignedTo字段为空将提供将被选中的选项。
< ASP:DropDownList的ID =ddlAssignedTo=服务器
的DataSourceID =SubjectDataSource
DataTextField =全名DataValueField =empl_Id
的SelectedValue ='<%#绑定(AssignedTo)%>'
AppendDataBoundItems =真正的>
< ASP:ListItem的文本=选择VALUE =/><
< / ASP:DropDownList的>
I load the gridview and the gridview has an edit and delete buttons.
I click on Edit and I get, "ddlAssignedTo' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
I know that I am getting this error because the value fo ddlAssignedTo
is null - nothing exists on the db for ddlAssignedTo.
All I was trying to do is update the current value.
So, my issue is this, if the current value is null, how do I assign a default value for ddlAssignedTo so that if no value currently exists on the db, the default value will prevail?
Here are some code:
Markup:
<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>
CodeBehind:
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
Take a look at the solution provided by @cosmin.onea in the question 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items
This solution sets AppendDataBoundItems="true" on the DropDownList and creates one nullable ListItem so that your DropDownList will bind even when the field in the table is null.
Applied to the OP's problem, the following snippet would provide an option that would be selected when the AssignedTo field is null.
<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>
这篇关于如何解决&QUOT;'ddlAssignedTo'具有的SelectedValue这是无效的,因为它不会在项目列表中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!