2

I have a .net 4 asp.net website. There is a listview with a checkbox in each "cell" of the listview. I'd like to attach some javascript to these checkboxes in that when the check box is checked/unchecked, it changes the color of teh asp:tablecell. The listview is bound to a collection.

I don't want to do a postback on each check change as there may be quite a few checkboxes changed.

Am i taking the wrong approach or would javascript be a good approach and if so, can someone help with the javascript. thanks

2 Answers 2

1

I do this in the ItemDataBound event of the ListView. Say you have the following javascript function:

function changeClass(checkBoxId, containerId) {
    var checkBox = document.getElementById(checkBoxId);
    var container = document.getElementById(containerId);
    if(checkBox.checked == true) container.style.backgroundColor = "#AAA";
    else  container.style.backgroundColor = "#FFF";
}

Then, you can use FindControl in ItemDataBound like so:

protected void ListView1_ItemDataBound(object sender, EventArgs e)
{
    var checkBox = e.Item.FindControl("nameOfCheckBox");
    var container = e.Item.FindControl("nameOfTableCell");

    checkBox.Attributes["onclick"] = "javascript:changeClass('"
        + checkBox.ClientID + "','"
        + container.ClientID + "');";
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for that... i'm still having a problem in giving the asp:tablecell an ID. I've tried something like this ID='<%# databinder.eval(container.dataitem,"ID")%>'.
Do you have to give the tablecell such a specific ID? My solution was designed for situations where the tablecell has an ID of, say, "TableCell1".
Thanks for the help.. i figured out what i was doing wrong. I need to make the check box just be 2 states, but this got me going. Thanks
0

On the server side you can attach the javascript events to the controls to attributes..property

like below//

seversidecontrolID.Attributes.Add("onclick","Javascript:
methodName(this);")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.