【问题标题】:Populating a dropdown list with a stored procedure (showing usernames) then showing the users full name instead of username - slow使用存储过程(显示用户名)填充下拉列表,然后显示用户全名而不是用户名 - 慢
【发布时间】: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 查询

标签: c# asp.net dropdown


【解决方案1】:

您在每次迭代中创建上下文可能需要一些时间。你可以试试这个:

    private void GetUsers()
     {
         OriginatorDropDown.Items.Clear();
         OriginatorDropDown.DataSource = GetUserData();
         OriginatorDropDown.DataBind();


using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
         foreach (ListItem ltItem in OriginatorDropDown.Items)
         {
             if (ltItem.Text != String.Empty)
             {


                     UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName
,ltItem.Text);
                     if (up != null)
                     {
                         ltItem.Text = up.DisplayName;
                     }


             }
         }
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多