使用 C# 从 HTML5 下拉列表中获取用户选择的值
Get user-selected value from HTML5 dropdown list using C#
我希望我的 C# 代码从 HTML5 下拉列表中获取当前选择的选项。我们的情况可能与许多其他情况有所不同,因为这不是客户端-服务器 Web 应用程序,而是使用 HTML 作为其 UI 的桌面应用程序,以便更便于 Linux。为了支持这一点,我们使用带有 Gecko 引擎的嵌入式 Firefox 浏览器。
我是使用 C# 使用 HTML 的新手,我似乎找不到合适的 API 调用。
HTML 看起来像这样:
...
<div class="select">
<select id="ddlMyOptions" name="ddlMyOptions">
<option></option>
<option>first choice</option>
<option>second choice</option>
</select>
</div>
...
我用来尝试访问它的 C# 代码是:
IHTMLElement selectElement = documentHTML.GetElementByID("ddlMyOptions");
string value = selectElement.Items[selectElement.SelectedIndex].Value;
string text = selectElement.Items[selectElement.SelectedIndex].Text;
这是基于我在其他帖子中看到的示例,但这些帖子使用的是 asp.net,在我的例子中,没有为 selectElement 定义 Items 和 SelectedIndex。
此外,我看过一些关于使用 Javascript 的帖子,但如果可能的话,我更愿意将其全部保留在 C# 中。
我查看了在下拉列表中使用回调方法需要什么,这样我就可以在所选值更改时获取所选值并将其保存到我的 class 中的实例变量中,但我没有也不知道如何指定回调方法。
非常感谢任何帮助!
由于我的问题没有答案,我继续 JavaScript 解决方案:
function updateDDLSelection(elid) {
var el = document.getElementById(""ddlMyOptions"");
var val = el.options[el.selectedIndex].value;
el.setAttribute('value', val);
}
然后,我不得不将 HTML 从上面显示的内容更改为:
...
<div class="select">
<select id="ddlMyOptions" name="ddlMyOptions" onchange=""updateDDLSelection(event)"">
<option></option>
<option>first choice</option>
<option>second choice</option>
</select>
</div>
...
下面是修改后的 C# 代码,用于获取 JavaScript 函数设置的值:
IHTMLElement selectElement = documentHTML.GetElementByID("ddlMyOptions");
string value = selectElement.GetAttribute("value", 0).ToString();
希望这对处于类似情况的其他人有所帮助...
我希望我的 C# 代码从 HTML5 下拉列表中获取当前选择的选项。我们的情况可能与许多其他情况有所不同,因为这不是客户端-服务器 Web 应用程序,而是使用 HTML 作为其 UI 的桌面应用程序,以便更便于 Linux。为了支持这一点,我们使用带有 Gecko 引擎的嵌入式 Firefox 浏览器。
我是使用 C# 使用 HTML 的新手,我似乎找不到合适的 API 调用。
HTML 看起来像这样:
...
<div class="select">
<select id="ddlMyOptions" name="ddlMyOptions">
<option></option>
<option>first choice</option>
<option>second choice</option>
</select>
</div>
...
我用来尝试访问它的 C# 代码是:
IHTMLElement selectElement = documentHTML.GetElementByID("ddlMyOptions");
string value = selectElement.Items[selectElement.SelectedIndex].Value;
string text = selectElement.Items[selectElement.SelectedIndex].Text;
这是基于我在其他帖子中看到的示例,但这些帖子使用的是 asp.net,在我的例子中,没有为 selectElement 定义 Items 和 SelectedIndex。
此外,我看过一些关于使用 Javascript 的帖子,但如果可能的话,我更愿意将其全部保留在 C# 中。
我查看了在下拉列表中使用回调方法需要什么,这样我就可以在所选值更改时获取所选值并将其保存到我的 class 中的实例变量中,但我没有也不知道如何指定回调方法。
非常感谢任何帮助!
由于我的问题没有答案,我继续 JavaScript 解决方案:
function updateDDLSelection(elid) {
var el = document.getElementById(""ddlMyOptions"");
var val = el.options[el.selectedIndex].value;
el.setAttribute('value', val);
}
然后,我不得不将 HTML 从上面显示的内容更改为:
...
<div class="select">
<select id="ddlMyOptions" name="ddlMyOptions" onchange=""updateDDLSelection(event)"">
<option></option>
<option>first choice</option>
<option>second choice</option>
</select>
</div>
...
下面是修改后的 C# 代码,用于获取 JavaScript 函数设置的值:
IHTMLElement selectElement = documentHTML.GetElementByID("ddlMyOptions");
string value = selectElement.GetAttribute("value", 0).ToString();
希望这对处于类似情况的其他人有所帮助...