2016-07-15 3 views
0

연락처 목록을로드하고 각 연락처 옆에 확인란을 추가하는 기능이 있습니다. 틱을 얻은 모든 연락처는 이메일을받습니다. 그러나 내 이메일 기능에서 목록은 항상 0으로 되돌아옵니다. 목록HtmlGenericControl 목록이 null을 계속 반환합니다.

코드 :

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);을 넣고 여기가 잘 작동 :

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

코드 연락처를로드합니다. 하지만 내 이메일 기능으로 다시 GetControls(ulCRMContacts.Controls);을 호출하면 0이 반환됩니다. 그것이 바로는 LoadContacts 기능을 중단으로 ulCRMContacts의 값을 잃는처럼

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

이 있습니다.

+1

언제 'EmailFollowup'을 실행하고 있습니까?'LoadContacts'는 언제 실행합니까? – Andrei

+0

@Andrei 'LoadContacts'는 페이지가로드되는 즉시 실행됩니다. 'EmailFollowup'은 이메일 버튼을 클릭하기 전까지는 실행되지 않습니다. – user123456789

+0

버튼에 AutoPostBack = "true"가 있으면 문제가 있습니다. LoadContacts() 함수에서 if (! IsPostBack) {..} 조건을 넣어야합니다. – Kami

답변

1

당신은 (재) 너무 모든 포스트 백에 동적 컨트롤을 만드는, 그래서이 작동하지 않습니다

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(); 
} 
관련 문제