WinForm應用程式與JavaScript指令碼互動取值

來源:互聯網
上載者:User

 BingWay原創作品,轉載請註明作者和出處。

    最近做了一個小東西,用於WinForm應用程式擷取網站的使用者登入資訊(包括使用者名稱、密碼、角色),是一款Web網站輔助管理的小應用程式。

 

 1 <script type="text/javascript">
 2  function GetData()//用此方法,會彈出“缺少對象”錯誤
 3  {
 4     
 5     var username = document.getElementById("txtuname").value ;
 6     var password = document.getElementById("txtpwd").value ;
 7     var ddlrole = document.getElementById("ddlRole");//得到DropdownListid
 8     var role = ddlrole.options[ddlrole.selectedIndex].text ;//得到選中文本值
 9     return [username,password,role];
10  }
11  </script>

    本想用上面的指令碼,此指令碼在瀏覽器中運行一切正常,能得到值,但是跑到WinForm應用程式中,一直報“缺少對象”錯誤。調試再三未果的情況下,得出一個經典的結論,那就是傳說中的人品問題。後來,查出原因,發現是“document.getElementById("txtuname").value”跑到應用程式中就報“缺少對象錯誤。

//登入頁面
Parameters.username = txtuname.Text;//點擊登陸時,賦值
Parameters.pwd = txtpwd.Text;
Parameters.role = ddlRole.SelectedValue;

 

 //後台代碼

public string username = Parameters.username;//靜態全域變數傳值
public string password = Parameters.pwd;
public string role = Parameters.role;

 

//調用後台資料  
<script type="text/javascript">
  
         function GetData()
         {   
              var username ="<%= username %>";
              var password = "<%= password %>" ;
              var role ="<%=role%>" ;
              return [username,password,role];
         }
 </script>

 

  應用程式中,對指令碼的調用:

        object[] Data = webBrowser1.Document.InvokeScript("GetData");//擷取指令碼資料
        foreach( string info in Data ){
              info=info+","; //迴圈取值
        }
        string[] arr=info.Split(char.Parse(",")); 
        Parameters.username = arr[0].ToString(); //得到登入輸入使用者名稱
        Parameters.pwd = arr[1].ToString(); //得到登入輸入密碼
        Parameters.role = arr[2].ToString(); //得到登入選擇角色

  以上是我的方法,如果大家有更好的方法,還請不吝賜教!

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.