Saigetsu、 2019-11-14 14:14 采纳率: 0%
浏览 345

为什么WinForm不能显示innerHTML拼接的表格?

以下是WinForm中的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using mshtml;

namespace Project3
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //winForm调用js的前提
            webBrowser1.ObjectForScripting = this;
        }

        public void btnSelect(String val) {
            //javascript要调用winform时必须加这一句
            this.webBrowser1.ObjectForScripting = this;
            //MessageBox.Show(val, "winForm窗口");
            //调用winform里面的调用XML方法
            XmlDocument Xml = new XmlDocument();
            //读取xml(project3.xml)
            Xml.Load("project3.xml");
            //获取根节点的所有子节点列表(sqldata)
            XmlNodeList list = Xml.DocumentElement.GetElementsByTagName("sqldata");
            //为了获取row的个数(for循环次数)
            XmlNodeList list_1 = Xml.DocumentElement.GetElementsByTagName("row");
            //foreach来遍历所有ReceiptFees为获取到val的同级Tag数据
            foreach (XmlNode node in list)
            {
                for (int i = 0; i < list_1.Count; i++)
                {
                    //遍历全部信息
                    if (val.Equals("全部"))
                    {
                        String s1 = node.ChildNodes[i].ChildNodes[0].InnerText;//No
                        String s2 = node.ChildNodes[i].ChildNodes[1].InnerText;//ReceiptFees
                        String s3 = node.ChildNodes[i].ChildNodes[2].InnerText;//MedicalContent
                        String s4 = node.ChildNodes[i].ChildNodes[3].InnerText;//price

                        String[] data = new String[4];
                        data[0] = s1; data[1] = s2; data[2] = s3; data[3] = s4;
                        this.webBrowser1.Document.InvokeScript("showdata", data);
                    }
                    else
                    {
                        //当ReceiptFees的值equals等于val的值时,将node的所有子节点打印到html页面
                        if (node.ChildNodes[i].ChildNodes[1].InnerText.Equals(val))
                        {
                            String s1 = node.ChildNodes[i].ChildNodes[0].InnerText;//No
                            String s2 = node.ChildNodes[i].ChildNodes[1].InnerText;//ReceiptFees
                            String s3 = node.ChildNodes[i].ChildNodes[2].InnerText;//MedicalContent
                            String s4 = node.ChildNodes[i].ChildNodes[3].InnerText;//price
                            String[] data = new String[4];
                            data[0] = s1; data[1] = s2; data[2] = s3; data[3] = s4;                            
                             this.webBrowser1.Document.InvokeScript("showdata", data);
                        }
                    }
                }
            }

        }

以下是HTML代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>未缴费处方项目信息</title>
    <link rel="stylesheet" href="style.css">
    <script language="javascript" type="text/javascript">
        function btnSelect()
        {
            //获取下拉框中的数据val
            var val = document.getElementById("3").value;
            //将下拉框获取到的费用数据传递给winform
            window.external.btnSelect(val);
        }
        //被winForm调用来执行的方法(方法名与winForm中对应)
    </script>

</head>
<body>
    <div id="base" class="base">
        <!--            题头                          -->
        <div id="1" class="title">
            <h2 class="head_font">下方是你未缴费处方项目信息</h2>
        </div>
        <!--            选择                          -->
        <div id="2" class="choose">
            <!-- 选择下拉框 -->
            <div id="" class="second_1">
                <select id="3" class="select">
                    <option value="全部">全部</option>
                    <option value="检查费">检查费</option>
                    <option value="西药费">西药费</option>
                    <option value="材料费">材料费</option>
                </select>
            </div>
            <!-- 选择按钮  -->
            <div id="" class="second_2">
                <input id="4" class="input" type="submit" value="查询XML信息" onclick="btnSelect()" />
            </div>
        </div>
        <!--  表格     -->
        <div class="table">
            <table border="0" cellspacing="0" cellpadding="0" id="t_1">
                <tr class="table_top">
                    <td>项目编号</td>
                    <td>项目类型</td>
                    <td>项目名称</td>
                    <td>项目价格</td>
                </tr>
            </table>
        </div>
    </div>
</body>

<script language="javascript" type="text/javascript">
    function showdata(s1, s2, s3, s4) {
        **//alert(s1); alert(s2); alert(s3); alert(s4); 经过测试这一句是执行成功的!**
        var htmls = "<TR><TD>" + s1 + "</TD><TD>" + s2 + "</TD><TD>" + s3 + "</TD><TD>" + s4 + "</TD></TR>";
        **document.getElementById("t_1").innerHTML += htmls;**//这一句执行失败,请各位帮帮忙看看为什么!

   }
</script>
</html>
  • 写回答

1条回答 默认 最新

  • threenewbee 2019-11-14 17:01
    关注

    肯定是可以的,建议你调试下,首先
    this.webBrowser1.Document.InvokeScript("showdata", data);
    传入硬编码的字符串,而不是data,看能不能加载表格,如果不能,就是你js的问题,调试下s1 s2 s3 s4有没有正确得到值

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作