簡體   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