locked
Populate Textboxes with Active Directory popup RRS feed

  • Question

  • User374657741 posted
     I have a page that when you click on an image of a book, a popup window appears and allows a user to search the active directory, which the results are populated in a listbox.  Once the user double clicks on the name the name populates a textbox.  I have been trying to find a way that not only the name populates one textbox but the chosen name's email populates a different textbox. Below is some of the code I am using:

    Page Code:
    <asp:TextBox ID="Name" runat="server"></asp:TextBox>
            <asp:TextBox ID="Email" runat="server"></asp:TextBox>     
            <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="Audio/addressbook.gif" onclick="Search_Click" /><br />

    Code behind Page:
       

     protected void Search_Click(object sender, ImageClickEventArgs e)
            {
                string script = "&lt;script type='text/javascript'>";
    
                script += "search_window=window.open('search.aspx?box=Form1.TextBox2','search_window','width=350,height=350');";
                script += "search_window.focus()";
                script += "&lt;/script>";
    
                Literal1.Text = script;
                
            }
    
    
            

    PopUp Page Code:
     

    protected void Page_Load(object sender, EventArgs e)
            {
                if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "move")
                {
                    int idx = ListBox1.SelectedIndex;
                    ListItem item = ListBox1.SelectedItem;
    
                    string strjscript = "&lt;script language='javascript'>";
                    strjscript += "window.opener." + HttpContext.Current.Request.QueryString["box"] + ".value = '" + item.ToString() + "';window.close();";
                    strjscript += "&lt;/script>";
                    Literal1.Text = strjscript;
                    
                }
                ListBox1.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(ListBox1, "move"));
            }
    
    
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                DirectoryEntry entry = new DirectoryEntry("LDAP://AD", "admin", "password", AuthenticationTypes.Secure);
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = ("(&(objectClass=user)(sn=" + TextBox1.Text + "*");
    
                SearchResultCollection results = search.FindAll();
    
                foreach (SearchResult srUser in results)
                {
                    try
                    {
                        DirectoryEntry de = srUser.GetDirectoryEntry();
    
                        string name = de.Properties["givenName"].Value.ToString() + " " + de.Properties["sn"].Value.ToString();
    string email = de.Properties["mail"].Value.ToString(); ListBox1.Items.Add(name); } catch { e.ToString(); } } }
     Thanks for any help
    Wednesday, June 3, 2009 11:13 AM

Answers

  • User-644392667 posted

     A couple of things first - wrap the DirectoryEntry and the DirectorySearcher in using statements. They are unmanaged and you are going to have memory issues if you do alot. This way they are Disposed of properly. Secondly, you do not need to do a srUser.GetDirectoryEntry(); That merely does an unneeded callback to the AD. Also make sure not not cache results, that can add to memory leakage...

    Here is an example:

            protected void Button1_Click(object sender, EventArgs e)
            {
                try
                {
                using (DirectoryEntry entry = new DirectoryEntry("LDAP://AD", "admin", "password", AuthenticationTypes.Secure))
                {
                    using (DirectorySearcher search = new DirectorySearcher(entry))
                    {
                        search.Filter = ("(&(objectClass=user)(sn=" + TextBox1.Text + "*");
                        search.CacheResults = false;
                        search.PropertiesToLoad.Add("ObjectClass");
                        search.PropertiesToLoad.Add("mail");
                        search.PropertiesToLoad.Add("givenName");
                        search.PropertiesToLoad.Add("sn");

                        SearchResultCollection results = search.FindAll();

                        foreach (SearchResult srUser in results)
                        {
                            string _email = String.Empty;
                            string _displayName;
                            if (srUser.Properties["ObjectClass"].Contains("user"))
                            {
                                StringBuilder _fullName = new StringBuilder();
                                if (srUser.Properties["givenName"].Count > 0)
                                {
                                    _fullName.Append(srUser.Properties["givenName"][0].ToString());
                                }
                                if (srUser.Properties["sn"].Count > 0)
                                {
                                    _fullName.Append(" ");
                                    _fullName.Append(srUser.Properties["sn"][0].ToString());
                                }
                                _displayName = _fullName.ToString().Trim();
                                ListBox1.Items.Add(_displayName);

                                if (srUser.Properties["mail"].Count > 0)
                                {
                                    _email = srUser.Properties["mail"][0].ToString();
                                }
                            }

                                ListBox1.Items.Add(name);

                            }
                        }
                    }
                }
                catch(Exception exc)
                {
                    throw exc;
                }
            }

    Hope this helps,

    m

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 3, 2009 12:49 PM