Saturday, March 31, 2012

There MUST be a better way!

When we want to confirm a user action before a post back occurs, we've been
writing code like this:

function onClickDelete()
{
var aResponse = window.confirm("Delete this record?");
if ( aResponse )
__doPostBack('theLayout$_ctl1$buttonDelete','') ;
return 0;
}

This seems like an "un-natural" act - is there a better way?

-- dwathe OOP approach would be to subclass the button, and add a confim property,
that can be set in the designer, or in the code behind. the new control
would then handle rendering the required javascript.

-- bruce (sqlwork.com)

"dwa" <da@.hotmail.com> wrote in message
news:O$CrRbe5DHA.2696@.TK2MSFTNGP09.phx.gbl...
> When we want to confirm a user action before a post back occurs, we've
been
> writing code like this:
> function onClickDelete()
> {
> var aResponse = window.confirm("Delete this record?");
> if ( aResponse )
> __doPostBack('theLayout$_ctl1$buttonDelete','') ;
> return 0;
> }
> This seems like an "un-natural" act - is there a better way?
> -- dwa
"bruce barker" <nospam_brubar@.safeco.com> wrote in message
news:%23bSpRse5DHA.1428@.TK2MSFTNGP12.phx.gbl...
> the OOP approach would be to subclass the button, and add a confim
property,
> that can be set in the designer, or in the code behind. the new control
> would then handle rendering the required javascript.

Yes. OOP as in: "Oooooops! This took a lot longer to complete than it
should have (2 hours as opposed to 2 minutes), and now we've got another
component to unit test..."

<ggg
There ought to be a 5 *second* approach to solve such a problem. It's
throw-away.

-- dwa
You can do this from code to the button, i'm not quite sure yet how you can
set onClick attributes to a web control from the designer.

Button1.Attributes.Add("onClick", "javascript:return confirm('are you
sure');")

"dwa" <da@.hotmail.com> wrote in message
news:O$CrRbe5DHA.2696@.TK2MSFTNGP09.phx.gbl...
> When we want to confirm a user action before a post back occurs, we've
been
> writing code like this:
> function onClickDelete()
> {
> var aResponse = window.confirm("Delete this record?");
> if ( aResponse )
> __doPostBack('theLayout$_ctl1$buttonDelete','') ;
> return 0;
> }
> This seems like an "un-natural" act - is there a better way?
> -- dwa
If the button is called btn1, add the following code to the Page_load: (in
vb)

Dim btn as button = btn1

btn.Attributes.Add("onClick")="javascript: return etc. ..."

The code in the onclick event of the button will only fire when the
javascript returns TRUE.

"Michael Ramey" <raterus@.localhost> schreef in bericht
news:Ov7pjye5DHA.2416@.TK2MSFTNGP10.phx.gbl...
> You can do this from code to the button, i'm not quite sure yet how you
can
> set onClick attributes to a web control from the designer.
> Button1.Attributes.Add("onClick", "javascript:return confirm('are you
> sure');")
> "dwa" <da@.hotmail.com> wrote in message
> news:O$CrRbe5DHA.2696@.TK2MSFTNGP09.phx.gbl...
> > When we want to confirm a user action before a post back occurs, we've
> been
> > writing code like this:
> > function onClickDelete()
> > {
> > var aResponse = window.confirm("Delete this record?");
> > if ( aResponse )
> > __doPostBack('theLayout$_ctl1$buttonDelete','') ;
> > return 0;
> > }
> > This seems like an "un-natural" act - is there a better way?
> > -- dwa
Problem with doing this sort of client side rerouting is that.. sometimes
the client has script accessed turned off, using a low level browser, etc.
etc. all sorts of problems. Therefore I decided to just make a simple server
sideded dialog box... STILL an ugly hack though. =P

"Martin" <anolis@.mail2world.com> wrote in message
news:40183939$0$89909$1b2cd167@.news.wanadoo.nl...
> If the button is called btn1, add the following code to the Page_load: (in
> vb)
> Dim btn as button = btn1
> btn.Attributes.Add("onClick")="javascript: return etc. ..."
> The code in the onclick event of the button will only fire when the
> javascript returns TRUE.
>
> "Michael Ramey" <raterus@.localhost> schreef in bericht
> news:Ov7pjye5DHA.2416@.TK2MSFTNGP10.phx.gbl...
> > You can do this from code to the button, i'm not quite sure yet how you
> can
> > set onClick attributes to a web control from the designer.
> > Button1.Attributes.Add("onClick", "javascript:return confirm('are you
> > sure');")
> > "dwa" <da@.hotmail.com> wrote in message
> > news:O$CrRbe5DHA.2696@.TK2MSFTNGP09.phx.gbl...
> > > When we want to confirm a user action before a post back occurs,
we've
> > been
> > > writing code like this:
> > > > function onClickDelete()
> > > {
> > > var aResponse = window.confirm("Delete this record?");
> > > if ( aResponse )
> > > __doPostBack('theLayout$_ctl1$buttonDelete','') ;
> > > return 0;
> > > }
> > > > This seems like an "un-natural" act - is there a better way?
> > > > -- dwa
> > >

0 comments:

Post a Comment