Monday, July 9, 2007

asp.net: how to autoGenerateSelectButton on gridView manually...

(I'll preface my post with "so" as the stereotypical microsoftie does)...

so I have this asp.net application that displays a grid of data. I want the user to be able to click anywhere on any row of that grid to start editing that row; something like data entry on a spreadsheet, but a little more controlled. dotNet 2.0 introduced a new control "gridview" to replace whatever else was used before. the event that gridview has that I want to use to trigger going into edit mode is SelectedIndexChanged - seems like a simple way to ID the row of interest and work on it. "seems" is the key word, because actually they made it hard to fire the event if you want to do anything different from the (ugly) way the control designers premade for you...

what they want you to do is use the "AutoGenerateEditButton" property to do the magic. funny they call it AutoGenerateEdit "Button" because it makes a URL looking link on the grid - not a button! then when you edit in the grid things jump around to fit the new fields that pop into the cells. looks awful.

so what you have to do to fake it out (which I'd rather not have to do, but there you go) was revealed on a google group search here

the trick is in Page_Load to add this code:

for (int i = 0; i++)
{
GridView1.Rows[i].Attributes.Add("onclick",
ClientScript.GetPostBackEventReference(GridView1, "Select$" + i));
}

(sorry I haven't figured how to make code format right under html yet).
also in the .aspx <%@ Page... %>directive you have to add a property not shown in VS: EnableEventValidation="False"

it works, but yuck! next, can I tackle edit mode?...

No comments:

About Me

My photo
smug SoCal catalyst prone to puffery. practitioner of verbal aikido. pearl caster for swine. professional. intuitive. clean. approachable. unique. forgiven sinner. ubiquitous. reads. geekiness rating ~10%. Meyers-Briggs = iNTj. musical thinker. into nature. project manager for a decent place which gives plenty of time, money and chances to prove his talent. dabbler, a dilettante, jack of all trades, master of none. lives in a nice place with a great wife and kids. his boys and attitude keeps him young. hangs out a lot, never bored. listens to XM. doesn't do favorites. tries to play the blues on acoustic guitar. loves the beach. draws. chillz' an groovz'. smokes a pipe. lefty.