使用Webbrowser将文本框值添加到网页C#中?

时间:2016-05-26 14:15:29

标签: c# winforms webbrowser-control inspect-element

我正在为网页上的文本框添加价值。 这是Inspect Element代码:

   <input type="text" name="DERIVED_LAM_GRADE_1$0" id="DERIVED_LAM_GRADE_1$0" 
   tabindex="90" value="0.00" class="PSEDITBOX" style="width:44px; 
   text-align:right; " maxlength="6" onchange="addchg_win0(this);oChange_win0=this;">

这是我的C#代码: 这给了我异常&#34;对象引用未设置为对象的实例。&#34;

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
   using System.Windows.Forms;

    namespace WindowsFormsApplication2
 {
  public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string username = textBox1.Text;

        HtmlDocument doc = this.webBrowser1.Document;
        doc.GetElementById("DERIVED_LAM_GRADE_1$0").SetAttribute("value", username);


    }
}
}

我不知道什么是错的。我给了一个完美的身份,但仍然是它的抛出异常。

设计师代码

   namespace WindowsFormsApplication2
  {
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.webBrowser1 = new System.Windows.Forms.WebBrowser();
        this.splitContainer1 = new System.Windows.Forms.SplitContainer();
        this.button1 = new System.Windows.Forms.Button();
        this.label2 = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.textBox1 = new System.Windows.Forms.TextBox();
        ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
        this.splitContainer1.Panel1.SuspendLayout();
        this.splitContainer1.Panel2.SuspendLayout();
        this.splitContainer1.SuspendLayout();
        this.SuspendLayout();
        // 
        // webBrowser1
        // 
        this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.webBrowser1.Location = new System.Drawing.Point(0, 0);
        this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
        this.webBrowser1.Name = "webBrowser1";
        this.webBrowser1.Size = new System.Drawing.Size(527, 309);
        this.webBrowser1.TabIndex = 0;
        this.webBrowser1.Url = new System.Uri("http://cms.nust.edu.pk/psp/ps/?cmd=login", System.UriKind.Absolute);
        // 
        // splitContainer1
        // 
        this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.splitContainer1.Location = new System.Drawing.Point(0, 0);
        this.splitContainer1.Name = "splitContainer1";
        this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
        // 
        // splitContainer1.Panel1
        // 
        this.splitContainer1.Panel1.Controls.Add(this.button1);
        this.splitContainer1.Panel1.Controls.Add(this.label2);
        this.splitContainer1.Panel1.Controls.Add(this.label1);
        this.splitContainer1.Panel1.Controls.Add(this.textBox2);
        this.splitContainer1.Panel1.Controls.Add(this.textBox1);
        // 
        // splitContainer1.Panel2
        // 
        this.splitContainer1.Panel2.Controls.Add(this.webBrowser1);
        this.splitContainer1.Size = new System.Drawing.Size(527, 398);
        this.splitContainer1.SplitterDistance = 85;
        this.splitContainer1.TabIndex = 1;
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(330, 31);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 4;
        this.button1.Text = "Insert";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(149, 61);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(53, 13);
        this.label2.TabIndex = 3;
        this.label2.Text = "Password";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(149, 15);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(55, 13);
        this.label1.TabIndex = 2;
        this.label1.Text = "Username";
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(208, 58);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(100, 20);
        this.textBox2.TabIndex = 1;
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(208, 12);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 20);
        this.textBox1.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(527, 398);
        this.Controls.Add(this.splitContainer1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.splitContainer1.Panel1.ResumeLayout(false);
        this.splitContainer1.Panel1.PerformLayout();
        this.splitContainer1.Panel2.ResumeLayout(false);
        ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
        this.splitContainer1.ResumeLayout(false);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.WebBrowser webBrowser1;
    private System.Windows.Forms.SplitContainer splitContainer1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox1;
}
  }

0 个答案:

没有答案