简体   繁体   English

为什么单击按钮后无法提交表单?

[英]Why won't this work to submit a form on the click of a button?

Im trying to submit a form on the click of a button in the c# web Browser control. 我试图通过单击c#Web浏览器控件中的按钮来提交表单。

[form code] [表单代码]

<form action="sendmessage.aspx" method="post" name="sendmessage">

[C# Code] [C#代码]

private void Form1_Load(object sender, EventArgs e)
{
    HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
    HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");

    IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
    element.text = "function submitForm() 
    { 
        document.getElementsByName('sendmessage')[0].value.submit(); 
    }";

    head.AppendChild(scriptEl);
}

private void button2_Click(object sender, EventArgs e)
{
    webBrowser1.Document.InvokeScript("submitForm");
}

Any help would be appreciated . 任何帮助,将不胜感激 。 Thanks 谢谢

If the code in your button isn't being hit, you need to bind the handler to it. 如果未按下按钮中的代码,则需要将处理程序绑定到该代码。 You can do this in the mark-up, in your load or in the properties window. 您可以在标记,负载或属性窗口中执行此操作。 The easiest way to do it is in your properties window. 最简单的方法是在属性窗口中。 Select the button, click the little lightning bolt in the properties tab and find the click event. 选择按钮,在属性选项卡中单击小闪电,然后找到click事件。 Select your button2_Click method and that will bind it to your button. 选择您的button2_Click方法,它将绑定到您的按钮。 That code should then be hit. 然后应该点击该代码。

Just a suggestion: instead of injecting JavaScript that way, declare the script on the client and either add to the onclick in the element (ugly) or add an event listener(better). 只是一个建议:与其以这种方式注入JavaScript,不如在客户端上声明脚本,然后将其添加到元素中的onclick(难看)或添加一个事件侦听器(更好)。 If your button is only going to fire client functionality, you dont' need to handle it at all on the server. 如果您的按钮仅用于启动客户端功能,则无需在服务器上进行任何处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM