views:

234

answers:

4

Anyone know how parse the DOM and determine what row is selected in an ASP.NET ListView? I'm able to interact with the DOM via the HtmlElement in Silverlight, but I've not been able to locate a property indicating the row is selected.

For reference, this managed method works fine for an ASP.NET ListBox

var elm = HtmlPage.Document.GetElementById(ListBoxId);

foreach (var childElm in elm.Children)
{
    if (!((bool)childElm.GetProperty("Selected")))
    {
       continue;
    }
}
A: 

I don't have my dev environment up to test this, but could you call GetProperty('selectedIndex') on the ListBoxID element? Then from that you figure out which child is selected and return that child using the elm.Children.

Edit: Got my dev environment up this morning and did some testing. Here is a code snippet that worked for me:

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");

Of course, you'll have to change "textSelect" to the name of your html select element. The linq query is needed since the Children property is made up of ScriptableObjects and only about half of them are the option elements which is what you care about.

Bryant
+1  A: 

If your listview has a specific css class for selected row, you can try to filter on it

mathieu
A: 

I am actually using the ListView, rather than the ListBox. Any thoughts on how I would determine the selected row(s) for that control?

javacavaj
+1  A: 

The suggestion mathieu provided should work fine. Since you mentioned row, I would say add an id to the tr element in your ListView that you can then find with jQuery.

So,

<tr id='selectedRow'>
......
</tr>

$(document).ready(function() {
    $("#selectedRow").click(function() {
        alert('This is the selected row');
    });

});
beckelmw