我正在处理的应用程序是Service Desk App。我有一个表单,它使用DropDownList,其中包含来自Active Directory的员工姓名。任何员工都可以提出请求并保存。
当员工离开公司并因此从Active Directory中删除其帐户时出现问题。当某个其他员工搜索数据库以查找可能属于他的相关服务票证时,当尝试打开它时会抛出一个错误,指示该名称在DropDownList项目中不存在。
我需要的是一个解决方案,以便功能保持不变(能够删除Active Directory条目),但不会抛出任何错误。
我正在使用标签,ASP.NET和VB。 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>
代码后面的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
任何有关此的帮助将不胜感激。谢谢。
答案 0 :(得分:0)
您的问题没有太多关于代码的细节。但听起来你的应用程序试图在DDL中选择一个不存在的值(不再)了。在选择之前检查一下:
Dim ddl As DropDownList
Dim item As ListItem = ddl.Items.FindByValue("Kostopoulos")
If item IsNot Nothing Then
ddl.SelectedIndex = ddl.Items.IndexOf(item)
Else
ddl.Items.Add(New ListItem("Not available"))
ddl.Enabled = False
End If
答案 1 :(得分:0)
首先你应该从Dropdownlist中找到listitem,如
ListItem li=DropdownID.Items.FindByValue("");
if (li != null)
DropdownID.SelectedValue = li.Value;
有关详细信息,请参阅msdn