The dropdownlist control often has the following error message because there are no closed fields in the project list. Such an exception is caused by incomplete or abnormal data binding, which is quite troublesome in design, and the most troublesome thing is that the program code of this error on the page cannot use try... catch Method to skip the error. In fact, the simplest way is to directly modify the dropdownlist control, so that when the dropdownlist control binds data, even if the field value does not exist in the list item, do not release the error. This article describes how to inherit the dorpdownlist and modify it, to effectively solve this problem.
Download program code: ASP. NET Server Control-day22.rar
Download the northwnd Database: northwnd.rar
I. Overwriting SelectedvalueAttribute solves the problem of data binding
The reason for the dropdownlist control to fasten the error is that the error message probably indicates the error occurred when the selectedvalue attribute is written. Therefore, we inherit the dorpdownlist and name it tbdropdownlist, and overwrite the selectedvalue attribute to solve this problem. The solution is to determine whether the value to be written exists in the project list before writing the selectedvalue attribute when writing the selectedvalue attribute. If the value does not exist, set the selectedindex attribute to-1.
Public Class TBDropDownList
Inherits DropDownList
''' <summary>
''' Override selectedvalue adequacy.
''' </summary>
Public Overrides Property SelectedValue() As String
Get
Return MyBase.SelectedValue
End Get
Set(ByVal value As String)
Dim oItem As ListItem = Me.Items.FindByValue(value)
If (oItem Is Nothing) Then
Me. selectedindex =-1 'When items does not exist
Exit Property
Else
MyBase.SelectedValue = value
End If
End Set
End Property
End Class
We use the products data table of the northwnd database as the test data, and define the items content of dropdownlist in advance. The first project that is added to "not matched" binds the selectedvalue attribute to the categoryid field.
<bee:TBDropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("CategoryID") %>'>
<Asp: listitem value = ""> not matched </ASP: listitem>
<asp:ListItem Value="2">Condiments</asp:ListItem>
<asp:ListItem Value="3">Confections</asp:ListItem>
</bee:TBDropDownList>
When the categoryid field value of a data item does not exist in the items set attribute of dropdownlist, the first "unmapped" project is displayed.
II,TbdropdownlistSet DatasoruceidProblems
The above solution has been mentioned in the author's article "Removing dropdownlist databind from errors". However, some readers have found another problem, when the datasourceid is set in dropdownlist, data cannot be properly tied. Here we will solve this problem.
We set the datasoruceid of tbdropdownlist to get the content of the project list, and set the datasourceid to another sqldatasource control that gets the content of the categories data table.
<bee:TBDropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("CategoryID") %>' DataSourceID="SqlDataSource2"
DataTextField="CategoryName" DataValueField="CategoryID">
</bee:TBDropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwnd %>"
SelectCommand="SELECT CategoryID, CategoryName, Description, Picture FROM Categories"
ProviderName="<%$ ConnectionStrings:Northwnd.ProviderName %>" >
</asp:SqlDataSource>
When the program is executed, the value of the categoryid field in formview is 7 (the categoryname should be product) in browsing mode ).
When you press Edit to switch to edititemtemplate, use tbdropdownlist to fasten the value of the categoryid field. You can disable the correct value.
Iii. Solution TbdropdownlistSet PerformanceidData cannot be closed
To solve the problem of setting the performanceid for tbdropdownlist, you need to set the selectedvalue attribute if items. count = 0 first use a fcachedselectedvalue variable to save the correct value first, and then overwrite the mongomdatabinding method. When dorpdownlist gets the project list content corresponding to datasoruceid, in this case, the items content will be completely retrieved, and then the selectedvalue attribute can be set again to properly fasten the data.
Public Class TBDropDownList
Inherits DropDownList
Private FCachedSelectedValue As String
''' <summary>
''' Override selectedvalue adequacy.
''' </summary>
Public Overrides Property SelectedValue() As String
Get
Return MyBase.SelectedValue
End Get
Set(ByVal value As String)
If Me.Items.Count <> 0 Then
Dim oItem As ListItem = Me.Items.FindByValue(value)
If (oItem Is Nothing) Then
Me. selectedindex =-1 'When items does not exist
Else
MyBase.SelectedValue = value
End If
Else
FCachedSelectedValue = value
End If
End Set
End Property
Protected Overrides Sub PerformDataBinding(ByVal data As System.Collections.IEnumerable)
MyBase.PerformDataBinding(data)
'Datasoruceid: Specify the selectedvalue adequacy value after the data is saved.
If (Not FCachedSelectedValue Is Nothing) Then
Me.SelectedValue = FCachedSelectedValue
End If
End Sub
End Class
Re-execute the program and switch to the editing mode. Then, tbdropdownlist can properly fasten the field value.
Note: This article is also published in "the first it state help tie Ren Competition". If you think this article is helpful to you, remember to repeat it to improve your popularity ^
Http://ithelp.ithome.com.tw/question/10012909
Http://ithelp.ithome.com.tw/question/10012915