WebBrowser实现自动提交表单,如何提交radio和下拉框?

changton 2008-06-22 11:01:14
一般的text都可以提交,只是radio和下拉框出现了问题。请各位帮助!
<div class="ymld">
<label for="gender1"><input type="radio" value="M" id="gender1" name="sx" /> 男</label>
<label for="gender2"><input type="radio" value="F" id="gender2" name="sx" /> 女</label>
<input type="hidden" value="0" id="checkselected" name="checkselected"/>
</div>


-----------------------------------
<select id="secquestion" name="pw_q">
<option value="">- 选择一个问题 -</option>
<option value="您的宠物的名字?" >您的宠物的名字?</option>
<option value="您就读的第一所学校的名称?" >您就读的第一所学校的名称?</option>
</select>

我应该如何编程实现提交?谢谢各位了!
...全文
376 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
graybelt 2009-02-27
  • 打赏
  • 举报
回复
错了。是第二项。呵呵 下标从0开始的
graybelt 2009-02-27
  • 打赏
  • 举报
回复
if (doc.All[i].TagName.ToUpper().Equals("SELECT"))
{
switch (doc.All[i].Name)
{
case "City":
doc.All[i].SetAttribute("selectedIndex", "1");
break;
}
}
测试通过。选择第一项
szm880828 2009-02-05
  • 打赏
  • 举报
回复
能不能直接通过名称来找到第几个啊
changton 2008-06-24
  • 打赏
  • 举报
回复
自己顶一下,希望大家帮助!谢谢!
gray820928 2008-06-24
  • 打赏
  • 举报
回复
radio和下拉框都是根据value属性赋值的啊
wdgphc 2008-06-24
  • 打赏
  • 举报
回复
HtmlElement SelectSex = null;

//...循环

if (webBrowser1.Document.All[i].GetAttribute("value") == "M")
{
SelectSex = webBrowser1.Document.All[i];
SelectSex.SetAttribute("checked", "checked");
}

以上是对Radio的自动选择,我刚刚试过,应该没问题,下拉表的没做过.
changton 2008-06-23
  • 打赏
  • 举报
回复
恳请大家帮助!谢谢!
changton 2008-06-22
  • 打赏
  • 举报
回复
我想通过按钮的方式,实现内容的自动提交。
遇到的问题是:radio和下拉框,我不知道该如何赋值。
请各位帮忙!

private void button3_Click(object sender, EventArgs e)
{
HtmlDocument doc=webBrowser1.Document;
for(int i=0;i<doc.All.Count ;i++)
{
if(doc.All[i].TagName.ToUpper().Equals("INPUT"))
{
switch(doc.All[i].Name)
{
case "login": //登录名
doc.All[i].InnerText ="lockte1styyy";
break;
case "pw": //密码
doc.All[i].InnerText ="11158";
break;
case "pw2": //再次输入密码
doc.All[i].InnerText = "11158";
break;
case "ln": //姓名
doc.All[i].InnerText = "tttyyyttt";
break;
case "sx": //性别
// doc.All[i].SetAttribute = "checkselected";
break;
case "pw_q": //密码提示问题
doc.All[i].InnerText = "您的宠物的名字?";
break;
case "pw_a": //密码问题答案
doc.All[i].InnerText = "tttyyyttt";
break;
case "byear": //生日 (年)
doc.All[i].InnerText = "1980";
break;
case "bmon": //生日 (月)
doc.All[i].InnerText = "3";
break;
case "bday": //密码问题答案
doc.All[i].InnerText = "22";
break;
case "em": //生日 (年)
doc.All[i].InnerText = "1980gasgd@1425.com";
break;
}
}
}
家鸣 2008-06-22
  • 打赏
  • 举报
回复
提交radio和下拉框? 提交Form吧? webBrowser中,你可以很容易找到Html元素,然后触发它的事件。

private void test()
{
if (webBrowser1.Document != null)
{
HtmlDocument doc = webBrowser1.Document;
if (doc.Forms.Count > 0)
{
HtmlElement targetForm = doc.Forms[0];
HtmlElementCollection collectSelect = doc.GetElementsByTagName("Select");
if (collectSelect.Count >= 1)
{
targetFormElement = collectSelect[0];
targetFormElement.click();
}
}
}
}

110,539

社区成员

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

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

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