title: call function from code behind in javascript without postback
body: <p>I have a list of customers that the user can choose from. When they select a customer, I need to load the contacts of that customer. I needed to call the function from JavaScript so I added a button:</p>
  
  <pre><code>&lt;asp:Button ID="btnSample" runat="server" style="float:right;display:none" OnClick="btnSample_Click" /&gt;   
  </code></pre>
  
  <p>and that button then gets triggered from my JavaScript function:</p>
  
  <pre><code>function OnGetCustomer(result){
                  document.getElementById('lblCustID').innerHTML = result.ID;
                  document.getElementById('lblCustName').innerHTML = result.Name; 
                  document.getElementById("btnSample").click();
  }
  </code></pre>
  
  <p>Code behind to load the contacts:</p>
  
  <pre><code>protected void btnSample_Click(object sender, EventArgs e)
      {
          RateSheet r = new RateSheet(ID, Company.Current.CompanyID);
          if (!string.IsNullOrEmpty(lblCustID.Text))
          {
              Customer c = new Customer(int.Parse(lblCustID.Text));
  
              bool e2 = false;
              foreach (Customer.CustomerContact cc in c.Contacts)
              {
                  HtmlGenericControl li = new HtmlGenericControl("li");
                  CheckBox cb = new CheckBox();
                  cb.ID = "cbCustContact_" + cc.ID;
                  cb.Checked = true;
                  if (!string.IsNullOrEmpty(cc.Email))
                  {
                      cb.Text = cc.Email;
                      cb.TextAlign = TextAlign.Right;
                      li.Controls.Add(cb);
                      ulCustContacts.Controls.Add(li);
                  }
              }
          }
      }
  </code></pre>
  
  <p>I need the value from <code>lblCustID</code> to find the customer's contacts. The problem is the button causes postback and I lose the customer I selected so <code>lblCustID</code> always comes back as zero. How do I stop postback so I don't lose any values?</p>