C# webBrowse 下拉列表自动选择问题

xiningzhao 2011-03-04 10:10:55
下面是下拉列表的的HTML代码,现在默认的选择是“苹果”,要用webBrowse模拟选择“橘子”(即更改原来默认的选择)代码该如何写?请指教,先谢谢了。

<TR>
<TD class=colorred align=right>水果</TD>
<TD align=left colSpan=3><SELECT id=fruit
<OPTION value=8 selected>苹果</OPTION> <OPTION
value=7>橘子</OPTION> <OPTION value=4>香蕉</OPTION></SELECT>
</SELECT></TD></TR>
<TR>
...全文
161 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiningzhao 2011-03-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xiningzhao 的回复:]
wuyq11,下面的代码是做何用的啊?
HtmlElement s= null;
if (webBrowser1.Document.All[i].GetAttribute("value") == "")
{
s= webBrowser1.Document.All[i];
s.SetAttribute("checked", "checked");
}
[/Quote]
终于明白了这段代码的意思:wuyq11其实是告诉了我两种解决方法,这种比较简单,就是搜索到select中的“fruit”然后对fruit的“selectedIndex"进行赋值即可。谢谢!
xiningzhao 2011-03-05
  • 打赏
  • 举报
回复
HtmlDocument doc = tempBrowser.Document;
IHTMLSelectElement select = (IHTMLSelectElement)doc.GetElementById("fruit").DomElement;
select.selectedIndex = 1;
我是这样写的,但总是报错,说是“未将对象引用设置到对象的实例”,这是什么原因造成的啊
xiningzhao 2011-03-04
  • 打赏
  • 举报
回复
wuyq11,下面的代码是做何用的啊?
HtmlElement s= null;
if (webBrowser1.Document.All[i].GetAttribute("value") == "")
{
s= webBrowser1.Document.All[i];
s.SetAttribute("checked", "checked");
}

jx0797 2011-03-04
  • 打赏
  • 举报
回复
你看下wuyq11 mshtml.dll这段 ,这个要添加mshtml.dll引用
xiningzhao 2011-03-04
  • 打赏
  • 举报
回复
jx0797,谢谢,还是不太明白。在W3School上看了看select的相关资料:

《selected 属性可设置或返回选项的 selected 属性的值。
该属性设置选项的当前状态,如果为 true,则该选项被选中。该属性的初始值来自 <option> 的 selected 属性。》

应该是改变option的selected属性,谁知道怎么改啊?

wuyq11 2011-03-04
  • 打赏
  • 举报
回复
HtmlElement s= null;
if (webBrowser1.Document.All[i].GetAttribute("value") == "")
{
s= webBrowser1.Document.All[i];
s.SetAttribute("checked", "checked");
}

mshtml.dll
using mshtml
mshtml.IHTMLSelectElement select =(mshtml.IHTMLSelectElement)
webBrowser1.Document.GetElementById("").DomElement;
select.selectedIndex =3;
jx0797 2011-03-04
  • 打赏
  • 举报
回复
翻了下MSDN ,你看下这段代码:

private void ShiftRows(String tableName)
{
if (webBrowser1.Document != null)
{
HtmlDocument doc = webBrowser1.Document;
HtmlElementCollection elems = doc.All.GetElementsByName(tableName);
if (elems != null && elems.Count > 0)
{
HtmlElement elem = elems[0];

// Prepare the arguments.
Object[] args = new Object[2];
args[0] = (Object)"-1";
args[1] = (Object)"0";

elem.InvokeMember("moveRow", args);
}
}
}

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧