Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: Adding attributes to asp controls

Reply
Thread Tools

Re: Adding attributes to asp controls

 
 
David Archuleta
Guest
Posts: n/a
 
      10-15-2003
Harry,

Off hand I would say that you could use RegisterClientScriptBlock to
register a script like:

<script>
var enableConfirm = false;

function ConfirmPost()
{
if (enableConfirm == false) //Confirm prompt disabled return true to
post.
return true;

if (confirm ("Do you want to post?"))
return true;
else
return false;
}
</script>

then call this.RegisterOnSubmitStatement("ConfirmTest", "enableConfirm=true;
ConfirmPost();"); to set the enableConfirm flag and call the ConfirmPost
function.

Hope this helps.

Regards,

David

"Harry" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks David. This helps a lot!! But this also raises another question. I
> have a save button and a cancel button on the page. The cancel button also
> triggers this javascript as well. Is there a way to have it not trigger

when
> I click the Cancel button? CausesValidation does not do the trick.
>
>
> "David Archuleta" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > If you want client code to affect whether or not the form posts, you

must
> > register the script on the server side using RegisterOnSubmitStatement:
> >
> > this.RegisterOnSubmitStatement("ConfirmTest", "if (confirm('Do you want

to
> > post?')) return true; else return false;");
> >
> >
> > This will cause the client onsubmit code to include your own validation
> > script in the submit "pipeline". Returning false, will stop the form

from
> > submitting, otherwise return true.
> >
> > Hope this helps.
> >
> > Dave
> >
> > "Harry" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > Hi,
> > > I've got an imagebutton control that when clicked, calls the

> > "SaveBtn_Click"
> > > funtion. I wold like to add another onClick attribute that should call

> the
> > > javascript function for confirm. If the user selects OK, then continue

> and
> > > submit the form. If they click Cancel, then it does nothing and does

not
> > > submit. My problem is that when I add the attribute to the control

from
> > the
> > > code behind, it
> > > doesn't call the javascript and goes directly to submit the form.
> > >
> > > It produces 2 "onClick" attributes for the imagebutton. Don't know why

> > this
> > > is happening. Please see the code below.
> > > Thanks
> > > Harry
> > > ************************************************** ****************
> > > *********************** THE CODE

**************************
> > > ************************************************** ****************
> > >
> > >
> > > Here's my imagebutton:
> > > <asp:ImageButton Runat="server" ID="btnSave" OnClick="SaveBtn_Click"
> > > ImageUrl="..\images\save_icon.gif />
> > >
> > >
> > > Here's where i add an attribute to the imagebutton
> > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> > > System.EventArgs) Handles MyBase.Load
> > >
> > > btnSave.Attributes.Add("onClick", "conf();")
> > >
> > > end sub
> > >
> > >
> > >
> > > Here's the Javascript to call before calling the delegated function
> > >
> > > <script language="javascript">
> > > <!--
> > > function conf()
> > > {
> > > if(confirm("By clicking OK, you agree . Click CANCEL to

stop"))
> > > {
> > > document.Form1.submit();
> > > }
> > > }
> > > // -->
> > > </script>
> > >
> > >
> > > Here is the function called only if they click OK form the confirm

> message
> > > box :
> > > Public Sub SaveBtn_Click(ByVal sender As Object, ByVal e As
> > > ImageClickEventArgs) Handles btnSave.Click
> > > Do some stuff here
> > > end sub
> > >
> > >
> > > Here is the ViewSource:
> > > <input type="image" name="btnSave" onclick="if

> > (typeof(Page_ClientValidate)
> > > == 'function') Page_ClientValidate(); " language="javascript"

> id="btnSave"
> > > onClick="conf();" src="/images/add_icon.gif" border="0" />
> > >
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
 
Harry
Guest
Posts: n/a
 
      10-27-2003
Thanks David, I've got it working properly.

"David Archuleta" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> Harry,
>
> Off hand I would say that you could use RegisterClientScriptBlock to
> register a script like:
>
> <script>
> var enableConfirm = false;
>
> function ConfirmPost()
> {
> if (enableConfirm == false) //Confirm prompt disabled return true

to
> post.
> return true;
>
> if (confirm ("Do you want to post?"))
> return true;
> else
> return false;
> }
> </script>
>
> then call this.RegisterOnSubmitStatement("ConfirmTest",

"enableConfirm=true;
> ConfirmPost();"); to set the enableConfirm flag and call the ConfirmPost
> function.
>
> Hope this helps.
>
> Regards,
>
> David
>
> "Harry" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Thanks David. This helps a lot!! But this also raises another question.

I
> > have a save button and a cancel button on the page. The cancel button

also
> > triggers this javascript as well. Is there a way to have it not trigger

> when
> > I click the Cancel button? CausesValidation does not do the trick.
> >
> >
> > "David Archuleta" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > If you want client code to affect whether or not the form posts, you

> must
> > > register the script on the server side using

RegisterOnSubmitStatement:
> > >
> > > this.RegisterOnSubmitStatement("ConfirmTest", "if (confirm('Do you

want
> to
> > > post?')) return true; else return false;");
> > >
> > >
> > > This will cause the client onsubmit code to include your own

validation
> > > script in the submit "pipeline". Returning false, will stop the form

> from
> > > submitting, otherwise return true.
> > >
> > > Hope this helps.
> > >
> > > Dave
> > >
> > > "Harry" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > Hi,
> > > > I've got an imagebutton control that when clicked, calls the
> > > "SaveBtn_Click"
> > > > funtion. I wold like to add another onClick attribute that should

call
> > the
> > > > javascript function for confirm. If the user selects OK, then

continue
> > and
> > > > submit the form. If they click Cancel, then it does nothing and does

> not
> > > > submit. My problem is that when I add the attribute to the control

> from
> > > the
> > > > code behind, it
> > > > doesn't call the javascript and goes directly to submit the form.
> > > >
> > > > It produces 2 "onClick" attributes for the imagebutton. Don't know

why
> > > this
> > > > is happening. Please see the code below.
> > > > Thanks
> > > > Harry
> > > > ************************************************** ****************
> > > > *********************** THE CODE

> **************************
> > > > ************************************************** ****************
> > > >
> > > >
> > > > Here's my imagebutton:
> > > > <asp:ImageButton Runat="server" ID="btnSave" OnClick="SaveBtn_Click"
> > > > ImageUrl="..\images\save_icon.gif />
> > > >
> > > >
> > > > Here's where i add an attribute to the imagebutton
> > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> > > > System.EventArgs) Handles MyBase.Load
> > > >
> > > > btnSave.Attributes.Add("onClick", "conf();")
> > > >
> > > > end sub
> > > >
> > > >
> > > >
> > > > Here's the Javascript to call before calling the delegated function
> > > >
> > > > <script language="javascript">
> > > > <!--
> > > > function conf()
> > > > {
> > > > if(confirm("By clicking OK, you agree . Click CANCEL to

> stop"))
> > > > {
> > > > document.Form1.submit();
> > > > }
> > > > }
> > > > // -->
> > > > </script>
> > > >
> > > >
> > > > Here is the function called only if they click OK form the confirm

> > message
> > > > box :
> > > > Public Sub SaveBtn_Click(ByVal sender As Object, ByVal e As
> > > > ImageClickEventArgs) Handles btnSave.Click
> > > > Do some stuff here
> > > > end sub
> > > >
> > > >
> > > > Here is the ViewSource:
> > > > <input type="image" name="btnSave" onclick="if
> > > (typeof(Page_ClientValidate)
> > > > == 'function') Page_ClientValidate(); " language="javascript"

> > id="btnSave"
> > > > onClick="conf();" src="/images/add_icon.gif" border="0" />
> > > >
> > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
attributes ala java annotations or .Net attributes? Kyle Schmitt Ruby 3 07-24-2007 07:48 PM
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
WebControl.Attributes.Add and custom attributes P4trykx ASP .Net 2 01-31-2007 04:33 PM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM
adding attributes to radio button list <asp:radiobuttonlist .../> don't work? psb ASP .Net 2 09-22-2004 03:53 PM



Advertisments