javascript - 如何在没有隐藏输入字段的情况下将值从 javascript (innerhtml) 传递到 asp.net 代码隐藏?

标签 javascript asp.net callback hidden-field mailmessage

我有一个 aspx 页面(主页面),我使用它在使用 window.loaded 事件单击按钮时在另一个 aspx 页面(弹出窗口)上填充一些 div。代码如下:

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

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>

弹出窗口.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)
    {

    }
}

现在,我一直在尝试将 div.innerhtml 的值传递给代码隐藏,因此我可以使用此 HTML 标记创建电子邮件。我尝试使用隐藏的 div,但我收到关于请求验证的 asp.net 错误:输入字段中的 html 代码,这是一个公平的观点。我似乎无法访问 div.InnerHtml。我得到值“\r\n\r\n”。我有我需要的值,但它是在 Javascript 中,我只需要一种方法来将这个值传递给 C#,这样我就可以发送电子邮件了。

当我单击 SendEmail 按钮时,我再次收到警报(因为正在调用 window.loaded)。我怎样才能使 Popup.aspx 只填充一次,而不是在每次单击按钮时填充?以及如何传递 innerhtml 值以使 SendEmail 工作?非常感谢您的关注。

最佳答案

要将一些数据发送到后面的代码,您有两种方法。 发布获取

一种是通过回发发送数据 - 这意味着您需要将它们添加到一个隐藏的或其他通过回发发送它们的输入控件中。

另一种是将它们作为参数添加到url。

这两种方法都可以与 ajax 调用一起使用。因此,选择一个并将您的数据发送到代码后面。

关于消息:

Request Validation: html code in an input field

这是通用的安全措施,在你的情况下,如果你知道你将在代码后面发送 html 代码,并且你知道你如何控制它,简单地禁用它并且不用担心 - 只要小心你的去向稍后渲染。

来自 MSDN Request Validation in ASP.NET :

This can be a problem if you want your application to accept HTML markup. For example, if your site lets users add comments, you might want to let users perform basic formatting using HTML tags that put text in bold or italics. In cases like these, you can disable request validation and check for malicious content manually, or you can customize request validation so that certain kinds of markup or script are accepted

更新

有效的示例代码: 主页

<head runat="server">
    <title></title>

    <script type="text/javascript">
      window.callback = function (doc) {
            doc.getElementById('SendBack').value = escape("<b>send me back</b>"); 
      }

      function openWindow() {
            var mywindow = window.open("Page2PopUp.aspx");          
      }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <a href="#" onclick="openWindow();return false">open pop up</a>
    </form>
</body>
</html>

弹出页面

<head runat="server">
    <title></title>
   <script type="text/javascript">
    function GetDataBack() {
        window.opener.callback(document);
    }
</script>
</head>
<body >
    <form id="form1" runat="server">
    <div>
        <input id="SendBack" name="SendBack" value="" type="hidden" />    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick="GetDataBack()" />    
        <asp:Literal runat="server" ID="txtDebugCode"></asp:Literal>    
    </div>
    </form>
</body>
</html>

并阅读它:

protected void Button1_Click(object sender, EventArgs e)
{
    txtDebugCode.Text = Server.UrlDecode(Request.Form["SendBack"]);
}

关于javascript - 如何在没有隐藏输入字段的情况下将值从 javascript (innerhtml) 传递到 asp.net 代码隐藏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14716559/

相关文章:

javascript - 如何使用 jQuery mobile 清除选项 HTML 对象?

c# - 将数据导出到 excel 文件 2003、2007 及更高版本

c# - 如何从 url 中删除查询字符串参数?

javascript - 如何将两个单独的查询传递给 promise 链末尾的回调函数?

javascript - ng-if、ng-show 还是自定义指令?关于最佳实践的建议

javascript - js中如何使用变量调用函数?

javascript - 即使是最基本的 Javascript 对我来说也失败了 - 但为什么呢?

asp.net - 使用位从存储过程返回 bool 值

iPhone - 将相机照片保存到 iPhone 相册时崩溃

animation - 动画结束前执行的 Tweenmax 回调