簡體   English   中英

從c#調用onclick方法

[英]call onclick method from c#

我想在C#中調用onclick。 我把一個WebBrowser放到基於C#的程序中,在網站上有一個用js函數綁定的按鈕

type="button" 
onclick="submitEdgeStory();" 
value="Invia la news" class="submit"

我想打電話給onclick方法。 我嘗試用以下方法調用submitEdgeStory

 webBrowser1.Document.InvokeScript("submitEdgeStory()");

要么

 webBrowser1.Document.Forms[1]
                     .SetAttribute("onclick", "return submitEdgeStory()");

但沒辦法..我不知道怎么稱呼這個功能......

PS:我沒有使用ASP.NET 它只是一個桌面應用程序

而且這也是js功能

var gPageIsOkToExit = false;

    function submitEdgeStory()
    {
        // Set a variable so that our "before unload" exit handler knows not to verify
        // the page exit operation.
        gPageIsOkToExit = true;

        // Do the submission.
        // var frm = document.getElementById("thisform");
        frms = document.getElementsByName("ATISUBMIT");

        if (frms)
        {
            if (frms[0])
                frms[0].submit();
        }
    }

    window.onbeforeunload = function (event) 
    {
        // See if this is a safe exit.
        if (gPageIsOkToExit)
            return;

        if (!event && window.event) 
                event = window.event;

        event.returnValue = "You have not hit the Submit Button to submit your story yet.";
    }

如果您為按鈕分配了ID

type="button" 
onclick="submitEdgeStory();" 
value="Invia la news" class="submit" id="buttonid"

那么你可以做 -

WebBrowser1.Document.GetElementById("buttonid").InvokeMember("submit")

我這樣解決了

        foreach (HtmlElement item in webBrowser1.Document.All)
        {
            if ( item.OuterHtml != null)
            {
                if (item.OuterHtml.Contains("submitEdgeStory"))
                {

                    item.InvokeMember("click");
                    break;
                }
            }
        }

webBrowser.Document.Forms[0].All.GetElementsByName("buttonname")[0].InvokeMember("click"); 應該工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM