【问题标题】:DOM ParsingDOM 解析
【发布时间】:2010-09-16 04:29:41
【问题描述】:
有谁知道如何解析 DOM 并确定在 ASP.NET ListView 中选择了哪一行?我可以通过 Silverlight 中的 HtmlElement 与 DOM 进行交互,但我无法找到指示该行被选中的属性。
作为参考,此托管方法适用于 ASP.NET 列表框
var elm = HtmlPage.Document.GetElementById(ListBoxId);
foreach (var childElm in elm.Children)
{
if (!((bool)childElm.GetProperty("Selected")))
{
continue;
}
}
【问题讨论】:
标签:
c#
asp.net
dom
silverlight-2.0
【解决方案1】:
如果你的列表视图有一个特定的 CSS 类用于选定的行,你可以尝试对其进行过滤
【解决方案2】:
mathieu 提供的建议应该可以正常工作。既然您提到了行,我会说在您的 ListView 中的 tr 元素中添加一个 id,然后您可以使用 jQuery 找到它。
所以,
<tr id='selectedRow'>
......
</tr>
$(document).ready(function() {
$("#selectedRow").click(function() {
alert('This is the selected row');
});
});
【解决方案3】:
我没有我的开发环境来测试这个,但是你能在 ListBoxID 元素上调用 GetProperty('selectedIndex') 吗?然后从中找出选择了哪个孩子并使用 elm.Children 返回该孩子。
编辑:今天早上启动了我的开发环境并进行了一些测试。这是一个对我有用的代码 sn-p:
HtmlElement elem = HtmlPage.Document.GetElementById("testSelect");
int index = Convert.ToInt32(elem.GetProperty("selectedIndex"));
var options = (from c in elem.Children
let he = c as HtmlElement
where he.TagName == "option"
select he).ToList();
output.Text = (string)options[index].GetProperty("innerText");
当然,您必须将“textSelect”更改为您的 html 选择元素的名称。需要 linq 查询,因为 Children 属性由 ScriptableObjects 组成,其中只有大约一半是您关心的选项元素。