如题:
现在有一网站,已经成功的在网上使用,现在我要做一个C/S结构的sample,点击C/S窗体上的一个按钮,自动填写网站中某一个界面的表单
(要自动填写的web界面的实现方式是使用js点击一次按钮创建一行值为空的控件,我现在就是要给这些值为空的控件赋值,数据我是从XML中读取出来,XML中有多少条数据,在web界面上就有多少行)我的思路:
在C/S中放入一个webbrowser,webbrowser的url属性为需要自动填写表单的界面,在界面初始化的时候,我会找到界面上添加按钮(这个按钮唯一,在整个界面中只可能存在一个,当点击一次后,界面的图片会发生改变),然后根据这个按钮找到按钮的父级的父级tr,按钮根据tr为每个td中的控件赋值,代码如下:
public Form1()
        {
            InitializeComponent();
            webBrowser1.Url = new Uri("http://61.129.102.68:8018/");
            this.webBrowser1.ObjectForScripting = this;
            this.WindowState = FormWindowState.Maximized;
        }
 private void btnFuZhi_Click(object sender, EventArgs e)
        {
            ReadXml();
        }  private void GetControl()
        {
            HtmlElementCollection eleCollection = webBrowser1.Document.GetElementsByTagName("input");
            foreach (HtmlElement ele in eleCollection)
            {
                if (ele.GetAttribute("type") == "image" && ele.GetAttribute("src").Trim() == "http://61.129.102.68:8018/skin/spring/Images/Add.gif")
                {
                    control = ele;
                    return;
                }
            }
        }
        private void ReadXml()
        {
            if (control == null)
                return;
            string path = "file.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            HtmlElementCollection collection = webBrowser1.Document.All;
            XmlNodeList list = doc.SelectNodes("baosuibeian/baosuibeian");
            collection["ctl01$cphPage$TxtCodeID"].SetAttribute("value", list[0]["beianId"].InnerText);
            collection["ctl01$cphPage$RecordNo"].SetAttribute("value", list[0]["beiandanhao"].InnerText);
            collection["ctl01$cphPage$txtGross"].SetAttribute("value", list[0]["maozhong"].InnerText);
            list = doc.SelectNodes("baosuibeian/baosuibeian/beianmingxi/mingxi");
            foreach (XmlNode xn in list)
            {
                while (control.TagName.ToLower() != "tr")
                {
                    control = control.Parent;
                }                try
                {
                    control.Children[2].Children[0].SetAttribute("value", xn["beijianhao"].InnerText);
                    control.Children[2].Children[0].InvokeMember("onchange");
                    control.Children[3].Children[0].InvokeMember("click");
                    control.Children[6].Children[0].SetAttribute("value", xn["shuliang"].InnerText);
                    control.Children[6].Children[0].InvokeMember("onchange");
                    control.Children[10].Children[0].SetAttribute("value", xn["jine"].InnerText);
                    control.Children[11].Children[0].SetAttribute("value", xn["bizhi"].InnerText);
                    control.Children[11].Children[0].InvokeMember("onchange");
                    control.Children[13].Children[0].SetAttribute("innerText", "123");
                    control.Children[14].Children[1].SetAttribute("value", xn["mudidi"].InnerText);
                    control.Children[14].Children[0].InvokeMember("onchange");
                    // control.Children[15].Children[0].SetAttribute("value", xn["gongdanhao"].InnerText);
                    control.Children[15].Children[0].InvokeMember("onchange");
                    MessageBox.Show("dd");                    GetControl();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }            }
        }ReadXml方法就是自动填写表单的,现在的问题是如果ReadXml方法中没有MessageBox.Show("dd");这句话的话web界面在自动填写的时候,只有一条数据,最后一条数据会将前面所有的数据覆盖掉;如果有这句话的话,界面数据就会正常,但是我不想要这句话,而且数据正常.请问该如何修改代码呢?