【发布时间】:2012-03-03 07:45:32
【问题描述】:
我正在开发的应用程序是一个服务台应用程序。我在那里有一个表单,它使用一个 DropDownList,其中包含来自 Active Directory 的员工姓名。任何员工都可以提出请求并保存。
当员工离开公司并因此将其帐户从 Active Directory 中删除时,就会出现问题。当其他员工搜索数据库以找到可能对他有用的相关服务票证时,当尝试打开它时会引发错误,指示名称在 DropDownList 项中不存在。
我需要一个解决方案,以便功能保持不变(能够删除 Active Directory 条目),但不会引发错误。
如标签所示,我使用的是带有 VB 的 ASP.NET。也欢迎使用 C# 的解决方案。
提前感谢您对我的问题提出的建议。
更新:
我正在添加一些代码,以便更清晰。
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>
CODE BEHIND 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 限定已从 ActiveDirectory 中删除,因此它不存在于列表的值中。
这是填充 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
对此的任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: asp.net vb.net drop-down-menu