Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

webBrower controls to realize winform and webpage is interactive

by the3factory 3/30/2008 4:15:00 AM
Increases WebBrowser control:

private WebBrowser webBrowser1;
Quotes page's document object

HtmlDocument doc = webBrowser1.Document;//get web document
Had the document object, may look like js to operate doc equally, visits page's all objects.
HtmlElementCollection htmlElements = webBrowser1.Document.GetElementsByTag("input");//get all input elements
//access every input element in web form
foreach (HtmlElement el in htmlElements)
 
{
                    strInputName 
= el.GetAttribute("name").ToString();//get input element's name
                    
strInputValue = el.GetAttribute("value").ToString();//get input element's value
       }
winForm transfers webpage the function
/*web page function*/
<script>
function jsMethod(var jsParam)
{
   alert(param);
}

</script>
/*call jsMethod from winForm*/
private void callJsMethod(string Param)
{
HtmlDocument doc 
= webBrowser1.Document;
doc.InvokeScript(
"jsMethod",new object[]{"called by winForm"});
}

webPage transfers the winForm method
//winform code
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]//    
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
//This property lets you integrate dynamic HTML (DHTML) code with your client application code
public partial class Form2 : Form
{
    
public void winFormMethod(string param)
        
{
            MessageBox.Show(param);
        }


        
private void Form2_Load(object sender, EventArgs e)
        
{
            webBrowser1.ObjectForScripting 
= this;//important
        }

}

//web page code
<input name="callWinMethod" onclick="window.external.winFormMethod('called from DHTML')">

Must transfer winform the method, these two attributes are must
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]  
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
 webBrowser1.ObjectForScripting = this,。
Example
Following unifies a simple example, uses webbrowser to register automatically.
Analyzes webform the first structure, below this registers the page including two input frames: The user name and the password, as well as registers the button.

<HTML>
    
<HEAD>
        
<title>test html</title>        
    
</HEAD>
    
<body background="/bugnet/graphics/back2.gif">
        
<form name="mainform" method="post" action="bugl_login.aspx" id="mainform" >
            
<b>Enter name</b><input id="uid" type="text" maxLength="50" size="25" name="uid"><br>
            
<b>Enter Password</b><input type="password" maxLength="20" size="25" name="pwd">
            
<input type="submit" value="go" name="go">
        
</form>
    
</body>
</HTML>

After the page writes down webbrowser, procedure automatic packing user name and password, triggering debarkation button.

 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        
{

                
string strUID = "userName@sdccn.com";
                
string strPWD = "PWD";
                webBrowser1.Document.GetElementById(
"uid").InnerText = strUID;//fill name
                webBrowser1.Document.GetElementById("pwd").InnerText = strPWD;//fill pwd
                webBrowser1.Document.GetElementById("go").InvokeMember("click");//click go
              }

Registers automatically like this realizes, uses these to be possible to complete some repeatedly to register the work, but may also cause to use for the automation to test the webpage procedure.

Example二
The capture page data, following page has a form, how inside data takeoff?
 
Has a look at the page DOM structure, table, three lines of two rows
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 
<HEAD>
  
<TITLE> New Document </TITLE>
  
<META NAME="Generator" CONTENT="EditPlus">
  
<META NAME="Author" CONTENT="">
  
<META NAME="Keywords" CONTENT="">
  
<META NAME="Description" CONTENT="">
 
</HEAD>

 
<BODY>
  
<TABLE border=1>
  
<TR>
    
<TD>name</TD>
    
<TD>age</TD>
    
<TD>score</TD>
  
</TR>
  
<TR>
    
<TD>agan</TD>
    
<TD>18</TD>
    
<TD>99</TD>
  
</TR>
   
<TR>
    
<TD>asca</TD>
    
<TD>18</TD>
    
<TD>88</TD>
  
</TR>
  
</TABLE>
 
</BODY>
</HTML>
Understood that this tableau format may start to induct to winform DataTable, then demonstrates in DataGridView
 private DataTable ImportToDataTable()
        
{
            HtmlElementCollection htmlTabs 
= webBrowser1.Document.GetElementsByTagName("table");//get all tables in the dom           
            DataTable dt = null;
            DataRow dr 
= null;
            
string strValue = ""
            
int intII=0;
            
if(htmlTabs!=null&&htmlTabs.length>0)
            
{
                HtmlElement htmlTable 
= htmlElements[0];
                HtmlElementCollection htmlRows 
= htmlElement.GetElementsByTagName("tr");//get all rows
                HtmlElementCollection htmlCells = null;
                
foreach (HtmlElement htmlRow in htmlRows)
                
{
                    
if (htmlRow == htmlRows[0])//build table header
                    {
                        BuildHeader(
ref dt, htmlCells)
                    }

                    
else
                    
{
                        htmlCells 
= htmlRow.GetElementsByTagName("td");
                        
                        
                        dr 
= dt.NewRow();                        
                        
foreach (HtmlElement htmlCell in htmlCells)
                        
{
                            
if (htmlCell.InnerText!=null)
                            
{
                                strValue 
= htmlCell.InnerText.Trim();                
                                dr[intII
++= strValue;                           
                            }
                        
                        }

                        dt.Rows.Add(dr);
                    }

                }

            }

            
return dt;
       
        }


    
private void BuildHeader(ref DataTable dt, HtmlElementCollection htmlCells)
        
{
            
int intCols = htmlCells.Count;
            
if (dt == null)
            
{
                dt 
= new DataTable();
                
for (int i = 0; i < intCols; i++)
                    dt.Columns.Add(
"col" + i, Type.GetType("System.String"));
            }
            
        }
The example to the data which inducts simple by string processes, actually may make some thorough processing, for instance the use regular expression recognition different data type, hoped that this example can play the role which offers a few ordinary introductory remarks so that others may offer their valuable ideas.

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0