Re: Adding attributes to asp controls

Discussion in 'ASP .Net' started by David Archuleta, Oct 15, 2003.

  1. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:...
    > > > 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" />
    > > >
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    David Archuleta, Oct 15, 2003
    #1
    1. Advertising

  2. David Archuleta

    Harry Guest

    Thanks David, I've got it working properly.

    "David Archuleta" <> wrote in message
    news:#...
    > 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:...
    > > > 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" <> wrote in message
    > > > news:...
    > > > > 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" />
    > > > >
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Harry, Oct 27, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. psb
    Replies:
    2
    Views:
    17,133
    Achintya Jha
    Sep 22, 2004
  2. Max
    Replies:
    1
    Views:
    506
    Joe Kesselman
    Sep 22, 2006
  3. P4trykx
    Replies:
    2
    Views:
    1,859
    bruce barker
    Jan 31, 2007
  4. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    390
    Bruno Desthuilliers
    Jun 20, 2007
  5. Kyle Schmitt
    Replies:
    3
    Views:
    211
    Kyle Schmitt
    Jul 24, 2007
Loading...

Share This Page