controlling javascript in asp.net, C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jay123
    New Member
    • Sep 2008
    • 121

    controlling javascript in asp.net, C#

    hi guys,
    is their any way i can control javascript on button click event.

    like i have
    Code:
    <asp:Button ID="Savebutton" runat="server" OnClick="Savebutton_Click"  OnClientClick="return confirm('Are you sure?');" />
    now everytime i click savebutton , javascript fires and i get a message " are you sure". now what i want is this javascript to fire on some condition

    if condition is met i gets this message else savebutton does it saving without poping up this message.
    Last edited by Frinavale; May 25 '09, 06:44 PM. Reason: Moved to ASP.NET Answers from .NET
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Write a JavaScript function...

    Comment

    • searockruz
      New Member
      • May 2009
      • 7

      #3
      Code:
      if(condition)
      {
      ScriptManager.RegisterClientScriptBlock(this,this.GetType(),"HelloWorld","javascript:alert('Hello World');",true);
      }
      Last edited by searockruz; May 23 '09, 05:29 AM. Reason: code blocks

      Comment

      • jay123
        New Member
        • Sep 2008
        • 121

        #4
        Originally posted by searockruz
        Code:
        if(condition)
        {
        ScriptManager.RegisterClientScriptBlock(this,this.GetType(),"HelloWorld","javascript:alert('Hello World');",true);
        }
        thanks searockruz for ur reply, but where should i write this code.

        in a button click event, in html javascript code or somewhere else

        Comment

        • searockruz
          New Member
          • May 2009
          • 7

          #5
          in the button click event inside your if condition

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by jay123
            thanks searockruz for ur reply, but where should i write this code.

            in a button click event, in html javascript code or somewhere else
            Hi Jay :)

            You can place the function in several different places.
            You could place it in an external .js file, you could place it in the <head> or <body> section of your page... It all depends on what you prefer and when you need to call the function.

            In your case it doesn't really matter where you put it (because you're calling the function after the page has been loaded in the browser)...so just pick one


            Your function should look something like the following (which checks some condition and if that condition is met will prompt the user to confirm their action):

            JavaScript Code:
            Code:
            function ConfirmSave(){
              if(someCondition)
              {
                return confirm('Are you sure?');
              }
            
             return true;
            }
            Like I said this can go pretty much anywhere on your page...

            You call the function in your onclientclick event:
            Code:
            <asp:Button ID="Savebutton" runat="server" OnClick="Savebutton_Click"  OnClientClick="return ConfirmSave();" />
            -Frinny

            Comment

            • jay123
              New Member
              • Sep 2008
              • 121

              #7
              Originally posted by searockruz
              Code:
              if(condition)
              {
              ScriptManager.RegisterClientScriptBlock(this,this.GetType(),"HelloWorld","javascript:alert('Hello World');",true);
              }
              hi searockruz,
              i did try ur code but it didnt help or rather it doesnt do anything

              this is what i did:

              if(condition)
              {
              ur script code
              }


              now whats happeneing is user gets a msg as "r u sure?" and now how does user close this message?? moreover this code runs after whole coding in that button click runs, so elimanating the whole meaning of msg given to user.

              Comment

              • jay123
                New Member
                • Sep 2008
                • 121

                #8
                Originally posted by Frinavale
                Hi Jay :)

                You can place the function in several different places.
                You could place it in an external .js file, you could place it in the <head> or <body> section of your page... It all depends on what you prefer and when you need to call the function.

                In your case it doesn't really matter where you put it (because you're calling the function after the page has been loaded in the browser)...so just pick one


                Your function should look something like the following (which checks some condition and if that condition is met will prompt the user to confirm their action):

                JavaScript Code:
                Code:
                function ConfirmSave(){
                  if(someCondition)
                  {
                    return confirm('Are you sure?');
                  }
                
                 return true;
                }
                Like I said this can go pretty much anywhere on your page...

                You call the function in your onclientclick event:
                Code:
                <asp:Button ID="Savebutton" runat="server" OnClick="Savebutton_Click"  OnClientClick="return ConfirmSave();" />
                -Frinny
                thanks frinny,

                i have been trying ur option given earlier, but perhaps i am not that good in wrting javascript code. my requirement is

                when a page loads initially i have stored a value of one of the dropdown control in a viewstate, and then after making some changes to the page. when user tries to save that page. that same drop down value is compared to viewstate value, if they are equal. user dont get any msg else a msg is shown to him that "r u sure?".

                now that viewstate value has been stored in a Literal on page laod named Lit

                i have tried this coding
                Code:
                if(document.getElementById("ctl00_ContentPlaceHolder1_dropdown1").value != document.getElementById("Lit").Value)
                         {
                             alert('r u sure?');
                             return false;
                         }
                but to no use.
                any help

                Comment

                • jay123
                  New Member
                  • Sep 2008
                  • 121

                  #9
                  thanks everyone, hwo took some time to answer my question. the answer given by everyone was to some extend correct. its just my javascript basics need to be polished.

                  couple of things on which i wasted some time and wont like ant one of u to waste is:

                  1) label doesnt have value property in javascript, if we want to assign or retrieve value from label. we should use innertext property.

                  2) for dropdown use

                  Code:
                  var dd1 = document.getElementById("ctl00_ContentPlaceHolder1_dropdown1");
                           var dd2 = dd1.selectedIndex;
                           var dd3 = dd1.options[dd2].value;
                  where "ctl00_ContentP laceHolder1_dro pdown1" is a HTML name of dropdown taken from source of a page, and at end dd3 will have value of selectedindex of dropdown.

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    :) I'm glad you sorted it out Jay.

                    The reason your DropDownList has an id of "ctl00_ContentP laceHolder1_dro pdown1" is because you're using a master page (or place holder). The ContentPlaceHol der control gives every control in it a unique name so that you can have a DropDownList named "dropdown1" in a content (aspx) page being displayed in ContentPlaceHol der1 and you have another DropDownList named "dropdown1" in another content (aspx) page being displayed in ContentPlaceHol der2.

                    The way to reference the "ctl00_ContentP laceHolder1_dro pdown1" in code is through the ClientID of the control.

                    For example, you would place the following in your asp code:
                    Code:
                    var dropDownClientID = "<%=dropdown1.ClientID %>";
                    var dd1 = document.getElementById(dropDownClientID);
                    var dd2 = dd1.selectedIndex;
                    var dd3 = dd1.options[dd2].value;
                    Please note that <%= is short hand for Response.Write( )....so the above code writes the ClientID of dropdown1 into the page (into your JavaScript) at compile/runtime.


                    It's true that an ASP label doesn't have a value property in JavaScript. The reason is because ASP Labels are rendered as <span> tags. If you want access to the text/content being displayed in a <span> tag you have to use the innerHTML property (or innerText property as you have discovered).

                    For example:
                    Code:
                    var labelClientID = "<%=Label1.ClientID %>";
                    var labelSpan = document.getElementById(labelClientID);
                    var labelText = labelSpan.innerHTML;
                    alert(labelText);
                    One more thing to note is that I don't think that you can access things stored in ViewState in your JavaScript (since it's encrypted). Instead use HiddenFields.

                    Comment

                    • jay123
                      New Member
                      • Sep 2008
                      • 121

                      #11
                      thanks frinny,
                      hats off to your clear ideas and simple writing technique u have. yah i forgot to mention but i did use hidden fields to compare values.

                      Cheers and thanks a lot for replying.

                      Comment

                      Working...