C# 在文档文本中最初未定义时检索元素属性值
假设我们有一个WebKitBrowser控件,并将DocumentText设置为C# 在文档文本中最初未定义时检索元素属性值,c#,webkit,open-webkit-sharp,C#,Webkit,Open Webkit Sharp,假设我们有一个WebKitBrowser控件,并将DocumentText设置为 wkb.DocumentText = @" <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'> <html> <head></head> <body> <form> <input type='text' id=
wkb.DocumentText = @"
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
<html>
<head></head>
<body>
<form>
<input type='text' id='test' name='smith'><br>
</form>
</body>
</html>
";
wkb.DocumentText=@”
";
如果我随后通过C#获取该输入字段中的用户输入,那么正确的方法是什么
如果我在HTML中预定义value属性,比如
<input type='text' id='test' name='smith' value='freedom'>
然后MessageBox.Show(wkb.Document.GetElementById(“test”).getAttribute(“value”)代码>准确显示“自由”。但是,如果没有定义值,结果总是空的(我假设它为null,而不仅仅是空的)
这对我来说很有意义,但我将获取用户输入,而不是预定义的值。我认为可以将整个文档保存为一个字符串并对其进行解析,但在标准IE浏览器控件中,使用element.value路由更简单。我的美味选项是什么?您无法在C#客户端读取表单值。要读取C#中的值,必须将表单发回(AJAX或完全发回)。如果您想查看某个值是否已更改,可以使用jQuery的.on('blur')…
捕获更改后的值并向服务器发送AJAX帖子。我还想补充一点,这种方法很有效,有点偏离viperguynaz的正确答案
实际上,您为webkit浏览器控件创建了一个导航方法,然后将webkit浏览器控件指向“事件”部分。您在HTML端执行功能上无用的表单提交,然后解析用户输入的参数,然后转到任何等待您的幻想
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using WebKit;
namespace test_run
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
wkb.DocumentText = @"
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
<html>
<head></head>
<body>
<form method='get'>
<input type='text' name='pike'><br>
<input type='submit' value='Submit'>
</form>
</body>
</html>";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void wkb_navigating(object sender, WebKitBrowserNavigatingEventArgs e)
{
string param1 = HttpUtility.ParseQueryString(e.Url.ToString()).Get(0);
MessageBox.Show(param1.ToString());
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Web;
使用WebKit;
名称空间测试运行
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
wkb.DocumentText=@“
";
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
}
私有void wkb_导航(对象发送方,WebKitBrowserNavigatingEventArgs e)
{
string param1=HttpUtility.ParseQueryString(e.Url.ToString()).Get(0);
Show(param1.ToString());
}
}
}