title: How do I pass values from javascript (innerhtml) to asp.net codebehind without hidden input field?
body: <p>I have an aspx page (mainpage), that I use to populate some divs on another aspx page (popup) on button click using the window.loaded event. Code is below:</p>
  
  <p>Script on mainpage.aspx</p>
  
  <pre><code>  &lt;script type="text/javascript"&gt;
  
        window.callback = function (doc) {
  
            if (document.getElementById('GridView2') != null)
            {
                // Get Gridview HTML and wrap it with &lt;table&gt; tag
                var temp = document.getElementById('GridView2').innerHTML;
                var temp2 = "&lt;table&gt;" + temp + "&lt;/table&gt;";
  
                doc.getElementById('foo').innerHTML = temp2; // this works fine
            }
            else
            {
                // GridView is missing, do nothing!
            }
        }
  
        function openWindow() {
  
            // Only open a new Compose Email window if there is a Gridview present
            if ((document.getElementById('GridView2') != null)) {
                var mywindow = window.open("Popup.aspx");
            }
            else {
                alert("Please create GridView first");
            }
        }
  </code></pre>
  
  <p></p>
  
  <p>Code on Popup.aspx</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;
      function loaded() {
          window.opener.callback(document);
          alert(document.getElementById('foo').innerHTML); //This alerts the code I need
  
          //var input = document.createElement("input");
          //input.setAttribute("type", "hidden");
          //input.setAttribute("name", "testinput");
          //input.setAttribute("runat", "server");
          //input.setAttribute("value", document.getElementById('foo').innerHTML);
  
          ////append to form element that you want .
          //document.getElementById("foo2").appendChild(input);
      }
  &lt;/script&gt;
  
  &lt;asp:Button OnClick="Send_Email_Button_Click" ID="SendEmail" Text="Send Email" CssClass="Button1" runat="server" /&gt;
              &lt;div id="foo" runat="server"&gt;
  
              &lt;/div&gt;
  </code></pre>
  
  <p>Popup.aspx.cs</p>
  
  <pre><code>protected void Send_Email_Button_Click(object sender, EventArgs e)
  {
      string subject = String.Format("TEST EMAIL");
      string mailto = "me@mysite.com";
      string mailfrom = Environment.UserName + "@mysite.com";
      string mailBody = "&lt;h1&gt;Testing&lt;/h1&gt;";
  
      MailMessage mail = new MailMessage(mailfrom, mailto, subject, null);
      mail.IsBodyHtml = true;
      mail.Body = mailBody;
      SmtpClient smtpClient = new SmtpClient("smtphost");
      smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
      try
      {
          smtpClient.Send(mail);
      }
      catch (Exception ex)
      {
  
      }
  }
  </code></pre>
  
  <p>Now, I'm stuck trying to pass the value of the div.innerhtml to codebehind, so I can create an email with this HTML markup. I tried using a hidden div, but I got an asp.net error about Request Validation: html code in an input field, which is a fair point. I can't seem to access div.InnerHtml. I get the value "\r\n\r\n". I have the value I need, but it's in Javascript, I just need a way to get this value to C# so I can send an email.</p>
  
  <p>When I click on the SendEmail button, I get the alert again (because window.loaded is being called). How can I make it so that the Popup.aspx is only populate once, and not at every button click? And how to pass the innerhtml value to get SendEmail to work? Thanks so much for looking.</p>