I have a dropdown list within a template field in details view
I am getting the following error whenever the page loads:
I read on a website to add the following code to my codebehind which would eliminate the error
But now I get the following error:
on the line
Code:
<asp:TemplateField HeaderText="Allocate Babysitter">
<EditItemTemplate>
<asp:DropDownList ID="ddlBabysitter" runat="server" SelectedValue='<%# Bind("BabysitterID") %>'
DataSourceID="GetBabysittersList" DataTextField="BabysitterName"
DataValueField="BabysitterID" ondatabinding="PreventErrorOnbinding" >
<asp:ListItem Text="none" Value=""></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblBabysitter" runat="server" Text='<%# Bind("BabysitterName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I am getting the following error whenever the page loads:
Code:
ddlBabysitter' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
I read on a website to add the following code to my codebehind which would eliminate the error
Code:
Protected Sub PreventErrorOnbinding(sender As Object, e As EventArgs)
Dim ddlBabysitter As DropDownList = DirectCast(sender, DropDownList)
ddlBabysitter.DataBinding -= New EventHandler(AddressOf PreventErrorOnbinding)
ddlBabysitter.AppendDataBoundItems = True
Dim li As New ListItem("Make a selection >>", "")
ddlBabysitter.Items.Insert(0, li)
Try
ddlBabysitter.DataBind()
Catch generatedExceptionName As ArgumentOutOfRangeException
ddlBabysitter.SelectedValue = ""
End Try
End Sub
But now I get the following error:
Code:
'Public Event DataBinding(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
Code:
ddlBabysitter.DataBinding -= New EventHandler(AddressOf PreventErrorOnbinding)