Exploring the depths and potentials of ASP.NET RSS 2.0 or Subscribe to .BenRush by Email
 Thursday, March 15, 2007

The task is to add a confirmation dialog whenever someone clicks a Delete or Add or whatever button/link/image on your GridView or Repeater. To accomplish this will require a bit of JavaScript, but it's really not hard at all.

The Repeater has the ItemDataBound and the GridView has the RowDataBound events, and in order to attach your JavaScript to an object within EACH row or item, you will need to hook this event (my samples will be with the GridView, but the Repeater uses the same methodology).

protected void GridView_Views_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
   ImageButton button =
      (ImageButton)e.Row.Cells[0].Controls[0];
   button.Attributes.Add(
      "onclick", "if(!confirm('Delete this item?'))return false;");

   return;
}
return;
}

Many people become confused on how to get at a particular control once rendered into the browser DOM with their script because the name changes for each row or item of the parent control (GridView or Repeater). They become curious how to attach their script to something seemingly random such as:

ctl00$ContentPlaceHolder1$Repeater1$ctl00$btnDelete

The answer is, you don't. You attach it to the object as it's being rendered into the newly bound row like above. Here, nothing has been scoped and named out for the client DOM, it's still just an object in memory.

The way the script works is simple: the event model of the browser will prevent something from bubbling if you return a false or 0 value from the on* handler (such as onclick above). If you return a success or 1 value, then the event will continue to trickle up the call chain to whomever else is registered to handle it (which, in this case, would be the ASP.Net client page framework which initiates the __doPostback() call).

Recommended reading:

kick it on DotNetKicks.com
Thursday, March 15, 2007 10:27:40 AM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
ASP.Net | Programming
Tracked by:
http://freewebs.com/aspxfaq/07/sitemap4.html [Pingback]
http://freewebs.com/toltom/03/sitemap17.html [Pingback]
http://freewebs.com/toltom/04/celebrity-cruise.html [Pingback]
http://freewebs.com/toltom/05/sitemap18.html [Pingback]
http://freewebs.com/toltom/03/www-northwest-airlines.html [Pingback]
"http://kevruublog.tripod.com/162.html" (http://kevruublog.tripod.com/162.html) [Pingback]
"http://kevruublog.tripod.com/70.html" (http://kevruublog.tripod.com/70.html) [Pingback]
"http://hzwu9e.org/sitemap45.html" (http://hzwu9e.org/sitemap45.html) [Pingback]
"http://tlem3v.org/horny-sisters.html" (http://tlem3v.org/horny-sisters.html) [Pingback]
"http://topslots.nl.eu.org/05/sitemap17.html" (http://topslots.nl.eu.org/05/site... [Pingback]
"http://freewebs.com/amexa/35/columbus.html" (http://freewebs.com/amexa/35/colum... [Pingback]
"http://freewebs.com/amexa/48/city-of-columbia.html" (http://freewebs.com/amexa/... [Pingback]
"http://freewebs.com/amexa/17/wirtualna-polska.html" (http://freewebs.com/amexa/... [Pingback]
"http://pinofranc.homestead.com/05/canopy.html" (http://pinofranc.homestead.com/... [Pingback]
"http://pinofranc.homestead.com/02/inground-pool-liners.html" (http://pinofranc.... [Pingback]
"http://pinofranc.homestead.com/00/contractors.html" (http://pinofranc.homestead... [Pingback]
"http://xom6u-xxx.com/horny-nude-school-teachers.html" (http://xom6u-xxx.com/hor... [Pingback]
"http://d7evj-www.com/bath-girl.html" (http://d7evj-www.com/bath-girl.html) [Pingback]
"http://lasduunews.angelfire.com/78.html" (http://lasduunews.angelfire.com/78.ht... [Pingback]
"http://nabkoonews.tripod.com/101.html" (http://nabkoonews.tripod.com/101.html) [Pingback]
"http://zunvoonews.angelfire.com/184.html" (http://zunvoonews.angelfire.com/184.... [Pingback]
"http://yvaoi-ooo.com/dorm-sex.html" (http://yvaoi-ooo.com/dorm-sex.html) [Pingback]
"http://lasduunews.angelfire.com/114.html" (http://lasduunews.angelfire.com/114.... [Pingback]
"http://ccc2k-hhh.com/hustler-mag.html" (http://ccc2k-hhh.com/hustler-mag.html) [Pingback]
"http://xxx3t-xxx.biz/sous-chef.html" (http://xxx3t-xxx.biz/sous-chef.html) [Pingback]
"http://x8jji-www.biz/sex-swingers.html" (http://x8jji-www.biz/sex-swingers.html... [Pingback]
"http://ovx5a-eee.com/bubblebutt.html" (http://ovx5a-eee.com/bubblebutt.html) [Pingback]
"http://freewebs.com/rimoq/07/elizabeth-banks.html" (http://freewebs.com/rimoq/0... [Pingback]
"http://freewebs.com/tferma/08/siemens-hearing-aids.html" (http://freewebs.com/t... [Pingback]
"http://freewebs.com/tiltak/07/tutorials.html" (http://freewebs.com/tiltak/07/tu... [Pingback]
"http://freewebs.com/pentac/01/cindy-crawford-furniture.html" (http://freewebs.c... [Pingback]
"http://freewebs.com/amexa/50/federal-law-enforcement-jobs.html" (http://freeweb... [Pingback]
"http://fkdvh-rrr.com/pussy-photos.html" (http://fkdvh-rrr.com/pussy-photos.html... [Pingback]
"http://unibetkom.load4.net/00291-blog.html" (http://unibetkom.load4.net/00291-b... [Pingback]
"http://ramambo.nl.eu.org/08/simslots.html" (http://ramambo.nl.eu.org/08/simslot... [Pingback]
"http://harum.nl.eu.org/allstateinsurance.html" (http://harum.nl.eu.org/allstate... [Pingback]
"http://harum.nl.eu.org/free-nude-celeb.html" (http://harum.nl.eu.org/free-nude-... [Pingback]
"http://nfdi1ek.biz/chubby-busty.html" (http://nfdi1ek.biz/chubby-busty.html) [Pingback]
"http://ecjpkj4.biz/kentuckyderby-com.html" (http://ecjpkj4.biz/kentuckyderby-co... [Pingback]
"http://sanver.nl.eu.org/free-mature-porn-videos.html" (http://sanver.nl.eu.org/... [Pingback]
"http://meritblog.nl.eu.org/ubhonline.html" (http://meritblog.nl.eu.org/ubhonlin... [Pingback]
"http://kamo--kom.nl.eu.org/britney-spears-boob.html" (http://kamo--kom.nl.eu.or... [Pingback]
"http://freewebs.com/gabeganews/118.html" (http://freewebs.com/gabeganews/118.ht... [Pingback]
"http://drbutqt.com/www-porsche-com.html" (http://drbutqt.com/www-porsche-com.ht... [Pingback]
"http://ana--blog.nl.eu.org/dog-diabetes.html" (http://ana--blog.nl.eu.org/dog-d... [Pingback]
"http://jyakos9.biz/cameron-diaz-tara-reid-nude.html" (http://jyakos9.biz/camero... [Pingback]
"http://gpcmitr.biz/the-legend-of-zelda.html" (http://gpcmitr.biz/the-legend-of-... [Pingback]
"http://nasferablog.netfirms.com/169.html" (http://nasferablog.netfirms.com/169.... [Pingback]
"http://nt0poev.biz/www-phonebook.html" (http://nt0poev.biz/www-phonebook.html) [Pingback]
"http://nasferablog.netfirms.com/266.html" (http://nasferablog.netfirms.com/266.... [Pingback]
"http://taygevx.biz/nude-boys.html" (http://taygevx.biz/nude-boys.html) [Pingback]
"http://sero--kom.nl.eu.org/female-masturbation-techniques.html" (http://sero--k... [Pingback]
"http://bazo--mene.nl.eu.org/extreme-bikini-team.html" (http://bazo--mene.nl.eu.... [Pingback]
"http://nrpfka3.biz/localnet-com.html" (http://nrpfka3.biz/localnet-com.html) [Pingback]
"http://nasferablog.netfirms.com/548.html" (http://nasferablog.netfirms.com/548.... [Pingback]
"http://jmqp7tr.biz/kaiserpermente.html" (http://jmqp7tr.biz/kaiserpermente.html... [Pingback]
"http://hjftsic.biz/statefarminsuance.html" (http://hjftsic.biz/statefarminsuanc... [Pingback]
"http://freewebs.com/fremapblog/sitemap2.html" (http://freewebs.com/fremapblog/s... [Pingback]
"http://freewebs.com/sruone/coscos.html" (http://freewebs.com/sruone/coscos.html... [Pingback]
"http://freewebs.com/sruone/sitemap98.html" (http://freewebs.com/sruone/sitemap9... [Pingback]
"http://lopbafrea.homestead.com/58.html" (http://lopbafrea.homestead.com/58.html... [Pingback]
"http://mernokob.nl.eu.org/uk-pornstars.html" (http://mernokob.nl.eu.org/uk-porn... [Pingback]
"http://freewebs.com/vuter/03/free-debt-consolidation.html" (http://freewebs.com... [Pingback]
"http://vuter.homestead.com/00/direct-merchant.html" (http://vuter.homestead.com... [Pingback]
"http://cuter.homestead.com/00/sitemap18.html" (http://cuter.homestead.com/00/si... [Pingback]
"http://vuter.homestead.com/00/priority-health.html" (http://vuter.homestead.com... [Pingback]
"http://freewebs.com/datingblogger/100.html" (http://freewebs.com/datingblogger/... [Pingback]
"http://freewebs.com/datingblogger/448.html" (http://freewebs.com/datingblogger/... [Pingback]
"http://fasxen.netfirms.com/3.html" (http://fasxen.netfirms.com/3.html) [Pingback]

Computers Blogs - Blog Top Sites

Archive
<July 2009>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
Blogroll
About the author/Disclaimer

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

© Copyright 2009
Benjamin Rush
Sign In
Statistics
Total Posts: 444
This Year: 0
This Month: 0
This Week: 0
Comments: 128
Themes
Pick a theme:
All Content © 2009, Benjamin Rush
DasBlog theme 'Business' created by Christoph De Baene (delarou)