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:
Script on mainpage.aspx
<script type="text/javascript">
window.callback = function (doc) {
if (document.getElementById('GridView2') != null)
{
// Get Gridview HTML and wrap it with <table> tag
var temp = document.getElementById('GridView2').innerHTML;
var temp2 = "<table>" + temp + "</table>";
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 on Popup.aspx
<script type="text/javascript">
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);
}
</script>
<asp:Button OnClick="Send_Email_Button_Click" ID="SendEmail" Text="Send Email" CssClass="Button1" runat="server" />
<div id="foo" runat="server">
</div>
Popup.aspx.cs
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 = "<h1>Testing</h1>";
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)
{
}
}
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.
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.
To send some data to code behind you have two methods. The Post and the Get.
The one is to send data with post back – meaning that you need to add them inside a hidden or other input control that send them with post.
The other is to add them to the url, as parameters.
Both of this methods can be used with ajax call. So select one and send your data to code behind.
About the message:
This is security measure for general purpose, in your case if you know that you going to send html code on code behind, and you know how you control that, simple disabled it and do not worry – just be careful what you going to render later.
From MSDN Request Validation in ASP.NET:
Update
example code that works:
Main page
PopUp Page
and read it :