1

Custom Attribute for XHTML, .NET, Checkbox event in the gridview

by volkanuzun 12. February 2008 10:03

I have a gridview and of course a template field. I dont know why, but whenever i drag to a gridview to my page, i always put a template field. Maybe bad design dunno. Anyways, i put a checkbox on the template field and autopostback for the checkbox is true. So what i aim to do, whenever a checkbox is checked in the gridview, handle the checked event, and update some database tables. In order to do this, i need to figure out which checkbox is clicked, so i can get the rowid and from there i can get the data key and go to database ...

Himmm, gridview does not fire rowcommand event when the checkbox is clicked, so i couldnt really figure out which check box is clicked. I looked for a place in the checkbox properties to put the row id somewhere in the rowbound event, so maybe i can read it later ?! I couldnt find any :))

Then i thought of adding a custom attribute to the checkbox, and store the datakey in this custom attribute, and read this datakey in the click event. Himm When you say custom attribute, xhtml, validation; i am sure there is more than just adding the attribute. So my friend Brian, helped me to findout a good article : http://www.alistapart.com/articles/customdtd/ which explains custom dtd attributes. So the first thing i did is handle the rowdatabound event and add the custom attribute as below :

if (e.Row.RowType == DataControlRowType.DataRow)
{
 CheckBox ckbDisplay = e.Row.FindControl("ckbDisplay") as CheckBox;
 ckbDisplay.Attributes.Add("RowID", gridEvents.DataKeys[e.Row.RowIndex].Value.ToString());
           
}

 

 

And of course there is the checkbox check event :

protected void ckbDisplay_CheckedChanged(object sender, EventArgs e)
{

CheckBox ckbDisplay = sender as CheckBox;
string sKey = ckbDisplay.Attributes["RowID"];

// do some stuff here....

}

 

I run the page, and look at the source code, what .NET framework does is wrapping the input type='checkbox' (which is asp:CheckBox) , with <span RowID='somevalue... . I checked this with different browser, and .NET framework generated the same source code. So i went to my doc type decleration and changed the decleration as follows :


 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"


[&lt;!ATTLIST span RowID CDATA #IMPLIED>]

>

Above, i am telling to the xhtml validator that, my span elements may ( #IMPLIED means may, #REQUIRED means must ) have an attribute RowID.

So, that easy :)

Tags: , , ,

Comments

Muruganantham
Muruganantham India
9/29/2008 10:24:22 AM #

This is good, but Instead of modifying document header, you can use InputAttributes properties of checkbox.

ckbDisplay.InputAttributes.Add("RowID",value);

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Original Design by Laptop Geek, Adapted by onesoft