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

by volkanuzun 2/12/2008 4:03:00 AM

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"


[<!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 :)

Comments

9/29/2008 4:24:22 AM

Muruganantham

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

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

Muruganantham in

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



About the author

Volkan Uzun




E-mail me Send mail

Twitter

Calendar

<<  December 2008  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Flickr Badge

www.flickr.com
This is a Flickr badge showing public photos from volkanuzun. Make your own badge here.

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in