Search code examples
c#asp.netlisthtmlgenericcontrol

HtmlGenericControl list keeps returning null


I have a function that loads a list of contacts and puts a checkbox next to each contact. Any contact that gets tick then gets an email sent to them. But in my email function the list is always coming back as zero.

Code for list:

   <div id="viewMenuDropFollowup" style="top:0px;right:10px; text-align: left; display:none">
                    <strong>Email to</strong> <a onclick="OpenEmail()" style="float:right;font-weight:bold;cursor:pointer">X</a>
                    <ul runat="server" id="ulCRMContacts">

                    </ul>
                    <asp:TextBox runat="server" ID="txtEmailTo" ></asp:TextBox>
                    <asp:LinkButton runat="server" ID="btnEmail3" CssClass="btnEmail" OnClick="btnEmail_Click" Text="Email" ToolTip="Email to selected contacts" OnClientClick="return CheckEmail()"></asp:LinkButton>
                </div>
                <a id="btnOpenEmail" onclick="OpenEmail()" class="EmailClass"><strong>Email</strong></a>

function OpenEmail() {
        if (document.getElementById("viewMenuDropFollowup").style.display === "block") {
            document.getElementById("viewMenuDropFollowup").style.display = "none";
        } else {
            document.getElementById("viewMenuDropFollowup").style.display = "block";
        }
    }

Code to load contacts:

protected void Page_Load(object sender, EventArgs e)    
{
    if (!Page.IsPostBack)
    {
        LoadContacts();
     }    
}

     protected void LoadContacts()
        {
            Customer c = new Customer(int.Parse(CustomerID));

            foreach (Customer.CustomerContact cc in c.Contacts)
            {
                HtmlGenericControl li = new HtmlGenericControl("li");
                CheckBox cb = new CheckBox();
                cb.ID = "cbCRMContact_" + cc.ID;
                cb.Checked = true;
                if (!string.IsNullOrEmpty(cc.Email))
                {
                    cb.Text = cc.Email;
                    cb.TextAlign = TextAlign.Right;
                    li.Controls.Add(cb);
                    ulCRMContacts.Controls.Add(li);
                }
            }

            GetControls(ulCRMContacts.Controls);
        }

I put the line GetControls(ulCRMContacts.Controls); to see if I can get the controls and it works fine here. But when I try call GetControls(ulCRMContacts.Controls); again in my email function it returns zero.

  protected void btnEmail_Click(object sender, EventArgs e)
        {
            if (EmailFollowup(new lookupCRMCustomerContact(Company.Current.CompanyID, int.Parse(Request.QueryString["CustID"]))))
            {
                DisplayMsg("Follow up has been emailed");
            }
            else
            {
                DisplayMsg("An Error Occurred sending email. Please contact Support");
            }
        }

public bool EmailFollowup(lookupCRMCustomerContact q)
{
      GetControls(ulCRMContacts.Controls);
}

It's like it loses the values in ulCRMContacts as soon as it leave the LoadContacts function.


Solution

  • You have to (re-)create dynamic controls on every postback too, so this won't work:

    protected void Page_Load(object sender, EventArgs e)    
    {
        if (!Page.IsPostBack)
        {
            LoadContacts();
         }    
    }
    

    Remove the !IsPostBack-check and it should work. If you still have issues move it to Page_Init which is the appropriate event.

    protected void Page_Init(object sender, EventArgs e)    
    {
        LoadContacts();   
    }