繁体   English   中英

HtmlGenericControl列表不断返回null

[英]HtmlGenericControl list keeps returning null

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

清单代码:

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

就像它离开LoadContacts函数后,就会失去ulCRMContacts的值。

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

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

删除!IsPostBack -check,它应该可以工作。 如果仍然有问题,请将其移至Page_Init ,这是适当的事件。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM