我正在使用的應用程序是Service Desk應用程序。我有一個使用DropDownList的表單,其中包含來自Active Directory的員工姓名。任何員工都可以提出請求並保存。當ListItem不存在時DropDownList拋出錯誤
當員工離開公司並因此將他的帳戶從Active Directory中刪除時出現問題。當其他員工搜索數據庫以查找可能使用的相關服務故障單時,當嘗試打開它時,會引發一個錯誤消息,指示名稱在DropDownList項中不存在。
我需要的是一個解決方案,以便功能保持不變(能夠刪除Active Directory條目),但不會引發錯誤。
我正在使用如標籤所示,ASP.NET與VB。使用C#解決方案也是受歡迎的。
非常感謝您對我的問題提出的建議。
UPDATE:
我加入一些代碼,以便它可以更清楚。
ASPX:(這是巨大的,我只把DropDownList的問題)
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="ITRequestId">
<EditItemTemplate>
<br />
<asp:LinkButton ID="LinkButton5" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" CssClass="InsertLink" />
<asp:LinkButton ID="LinkButton6" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" CssClass="CancelLink" />
........
<div id="user" style="float: left;">
<label>User:<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="User" Display="Dynamic" ControlToValidate="DropDownList5" Text="*" ForeColor="#FF0000"></asp:RequiredFieldValidator></label><br />
<asp:DropDownList ID="DropDownList5" runat="server" SelectedValue='<%# Bind("ITRequestUserName") %>'>
<asp:ListItem Value=""></asp:ListItem>
<asp:ListItem Value="All">All</asp:ListItem>
<asp:ListItem Value="NA">N/A</asp:ListItem>
</asp:DropDownList>
</div>
.........
</EditItemTemplate>
後面的代碼VB:(這是巨大的,但我把其中拋出錯誤的頁面加載事件)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not IsPostBack) Then
If Request.UrlReferrer IsNot Nothing Then
ViewState("RefUrl") = Request.UrlReferrer.ToString()
End If
End If
'Handles the mode of the FormView according to the request
If Request.QueryString.Get("ITRequestId") IsNot Nothing Then
FormView1.DefaultMode = FormViewMode.ReadOnly
Dim tName As String = DirectCast(FormView1.Row.FindControl("DropDownList5"), DropDownList).SelectedValue
Dim temptype As String = DirectCast(FormView1.Row.FindControl("DropDownList1"), DropDownList).SelectedItem.Text
Dim myAD As New tActiveDirectory(LDAPpath)
Dim lName As String = HttpContext.Current.User.Identity.Name.ToString()
Dim sDisplayName As String = myAD.GetUserInfo(lName, "displayName")
Dim cName As String = DirectCast(FormView1.Row.FindControl("Label5"), Label).Text
If Not (User.IsInRole("Developers") Or User.IsInRole("Administrators") Or tName = sDisplayName Or cName = sDisplayName) Then
If (temptype = "Access rights") Then
Response.Redirect("../IT/ITAccessDenied.aspx")
End If
End If
If Not (User.IsInRole("Developers") Or User.IsInRole("Administrators") Or cName = sDisplayName) Then
If (temptype = "Account") Then
Response.Redirect("../IT/ITAccessDenied.aspx")
End If
End If
If Not (User.IsInRole("LocalIT")) Then
If (temptype = "Internal IT Task") Then
Response.Redirect("../IT/ITAccessDenied.aspx")
End If
End If
Else
FormView1.DefaultMode = FormViewMode.Insert
Dim tempstatus As DropDownList = DirectCast(FormView1.Row.FindControl("DropDownList2"), DropDownList)
tempstatus.SelectedIndex = 3
End If
End Sub
聲明tName變量時,錯誤在代碼的第11行拋出。發生這種情況的原因是,數據庫中的用戶名以及將由DropDownList限制的UserName已從ActiveDirectory中刪除,因此它不存在於List的值中。
這裏是填充的DropDownList代碼:
Protected Sub FormView1_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.ItemCreated
Dim d1 As DropDownList
Dim d2 As DropDownList
Dim myAD As New tActiveDirectory(LDAPpath)
Dim users As New ArrayList()
users = myAD.GetAllUsersInfo()
d1 = DirectCast(FormView1.Row.FindControl("DropDownList5"), DropDownList)
d2 = DirectCast(FormView1.Row.FindControl("DropDownList7"), DropDownList)
d1.DataSource = users
d2.DataSource = users
End Sub
Public Function GetAllUsersInfo() As ArrayList
Dim Users As New ArrayList()
Dim myDirectory As New DirectoryEntry(sPath)
Dim mySearcher As New DirectorySearcher(myDirectory)
Dim fullName As String
mySearcher.Filter = "(&(objectCategory=person)(objectClass=user))"
mySearcher.PropertiesToLoad.Add("sn")
mySearcher.PropertiesToLoad.Add("displayName")
mySearcher.Sort.PropertyName = "sn"
mySearcher.Sort.Direction = SortDirection.Ascending
Users.Add("")
Users.Add("N/A")
Users.Add("All")
For Each result As DirectoryServices.SearchResult In mySearcher.FindAll
fullName = result.Properties("displayName")(0).ToString
Users.Add(fullName)
Next
Return Users
End Function
任何幫助,這將不勝感激。謝謝。
我加入一些代碼。您是否認爲您可以提供一些洞察力,因爲我實際上無法符合您在上面給出的代碼。先謝謝你。 – 2012-03-09 06:23:45
我建議將<%#Bind(「ITRequestUserName」)%>移動到文件後面的代碼中(例如Page_Load或DropDownList5_Load),您可以在選擇它之前檢查該值是否在列表中。 – Elementenfresser 2012-03-09 08:12:35
按照你的建議,我設法解決了這個問題。謝謝! – 2012-03-13 14:37:06