【发布时间】:2020-01-30 09:23:19
【问题描述】:
我有一个下拉菜单 -
<asp:DropDownList ID="OriginatorDropDown" DataTextField="User"
DataValueField="User" runat="server" CssClass="form-control">
<asp:ListItem Selected="True" Text="" Value="" /> </asp:DropDownList>
我在页面加载时填充它。然而它非常缓慢.. 我的名单上只有大约 30 人,不应该这么慢。任何 请给点意见?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetUsers();
}
}
private void GetUsers()
{
OriginatorDropDown.Items.Clear();
OriginatorDropDown.DataSource = GetUserData();
OriginatorDropDown.DataBind();
foreach (ListItem ltItem in OriginatorDropDown.Items)
{
if (ltItem.Text != String.Empty)
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName
,ltItem.Text);
if (up != null)
{
ltItem.Text = up.DisplayName;
}
}
}
}
}
public DataSet GetUserData()
{
using (SqlConnection myConnection = new SqlConnection(ElectronicPayments))
{
//Get status descriptions
using (SqlCommand cmd = new SqlCommand("GetAllUsers", myConnection))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet user = new DataSet();
myConnection.Open();
da.Fill(user);
return user;
}
}
}
【问题讨论】:
-
这很慢,因为您正在为每个用户进行 AD 用户查找查询。全名应该已经存储在数据库中。如果您使用 WebForms 及其成员资格和用户配置文件提供程序,则信息应该已经存在。您不必每次使用用户名时都执行 AD 查询