HtmlGenericControl列表保持返回null

时间:2016-07-15 14:17:44

标签: c# asp.net list htmlgenericcontrol

我有一个加载联系人列表的功能,并在每个联系人旁边放置一个复选框。任何获得勾选的联系人都会收到发送给他们的电子邮件。但在我的电子邮件功能中,列表总是回归零。

列表代码:

   <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";
        }
    }

加载联系人的代码:

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);
        }

我放行GetControls(ulCRMContacts.Controls);以查看是否可以获取控件,它在这里工作正常。但是当我尝试在我的电子邮件功能中再次呼叫GetControls(ulCRMContacts.Controls);时,它返回零。

  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);
}

它就像ulCRMContacts离开LoadContacts函数时丢失了值。

1 个答案:

答案 0 :(得分:1)

你必须(重新)在每个回发上创建动态控件,所以这不会起作用:

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

删除!IsPostBack - 检查它应该有效。如果您仍有问题,请将其移至Page_Init,这是适当的事件。

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