Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. get control id from the content page

get control id from the content page

Scheduled Pinned Locked Moved ASP.NET
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S srinandan

    hi, i have need a functionality like as soon we click the button at the moment that button will hide and after that the functionality should fire written in code behind along with that button.i am working as follows; function check() { document.getElementById('Button1').style.visibility= "hidden"; return false; } code behind as follows: protected void Page_Load(object sender, EventArgs e) { Button1.Attributes.Add("onclick", "javascript:return check()"); } protected void Button1_Click(object sender, EventArgs e) { Response.Write("success"); } this not working properly. and i have to do this for a content page having this Button1 so the problem is the the getElementById is not working. Regards, Srinandan..

    C Offline
    C Offline
    ca8msm
    wrote on last edited by
    #2

    When the HTML is written out to the Page, your button's ID in the HTML probably isn't "Button1". That means your javascript method will fail. To get around this, write the javascript into a String on the Server-Side and add it to the Page by using the RegisterClientScriptBlock method. When inserting the Button's ID, use MyButton.ClientID to get the correct id.

    Mark, http://aspnetlibrary.com

    1 Reply Last reply
    0
    • S srinandan

      hi, i have need a functionality like as soon we click the button at the moment that button will hide and after that the functionality should fire written in code behind along with that button.i am working as follows; function check() { document.getElementById('Button1').style.visibility= "hidden"; return false; } code behind as follows: protected void Page_Load(object sender, EventArgs e) { Button1.Attributes.Add("onclick", "javascript:return check()"); } protected void Button1_Click(object sender, EventArgs e) { Response.Write("success"); } this not working properly. and i have to do this for a content page having this Button1 so the problem is the the getElementById is not working. Regards, Srinandan..

      S Offline
      S Offline
      Sylvester george
      wrote on last edited by
      #3

      if you say return false; the page will not submit

      Regards, Sylvester G sylvester_g_m@yahoo.com

      S 1 Reply Last reply
      0
      • S Sylvester george

        if you say return false; the page will not submit

        Regards, Sylvester G sylvester_g_m@yahoo.com

        S Offline
        S Offline
        srinandan
        wrote on last edited by
        #4

        thanks for reply if i will make the return true then the button will hide for moment but i have need to hide for that time. srinandan..

        S 1 Reply Last reply
        0
        • S srinandan

          thanks for reply if i will make the return true then the button will hide for moment but i have need to hide for that time. srinandan..

          S Offline
          S Offline
          Sylvester george
          wrote on last edited by
          #5

          when the button again want to visible for you?

          Regards, Sylvester G sylvester_g_m@yahoo.com

          S 1 Reply Last reply
          0
          • S srinandan

            hi, i have need a functionality like as soon we click the button at the moment that button will hide and after that the functionality should fire written in code behind along with that button.i am working as follows; function check() { document.getElementById('Button1').style.visibility= "hidden"; return false; } code behind as follows: protected void Page_Load(object sender, EventArgs e) { Button1.Attributes.Add("onclick", "javascript:return check()"); } protected void Button1_Click(object sender, EventArgs e) { Response.Write("success"); } this not working properly. and i have to do this for a content page having this Button1 so the problem is the the getElementById is not working. Regards, Srinandan..

            J Offline
            J Offline
            Jesse Squire
            wrote on last edited by
            #6

            In order to ensure that controls have a unique id on the page, ASP.NET will rename your controls as it renders them. In order for you to retrieve the id of a server control on the client side, you will need to progratically reference the ClientID property of the button. Using the ClientID property, you could refactor your code into something like: Client Script:

            <script language="javascript" type="text/javascript">

            function check(buttonId)

            {

            button = document.getElementById(buttonId);

            if (button)

            {

            button.style.visibility = "hidden";

            }

            return false;

            }

            </script>

            Server Code:

            protected void Page_PreRender(object sender, EventArgs ea)

            {

            button1.Attributes.Add("onclick", String.Format("javascript:return

            S 1 Reply Last reply
            0
            • S Sylvester george

              when the button again want to visible for you?

              Regards, Sylvester G sylvester_g_m@yahoo.com

              S Offline
              S Offline
              srinandan
              wrote on last edited by
              #7

              thanks Sylvester As soon the button will click at the moment it should be hide after that the Button1_Click() events should fire. in this method i am check some condition if the functionality goes success then no need to show that button if functionality failure then the button should visible its all. regards, Srinandan

              S 1 Reply Last reply
              0
              • J Jesse Squire

                In order to ensure that controls have a unique id on the page, ASP.NET will rename your controls as it renders them. In order for you to retrieve the id of a server control on the client side, you will need to progratically reference the ClientID property of the button. Using the ClientID property, you could refactor your code into something like: Client Script:

                <script language="javascript" type="text/javascript">

                function check(buttonId)

                {

                button = document.getElementById(buttonId);

                if (button)

                {

                button.style.visibility = "hidden";

                }

                return false;

                }

                </script>

                Server Code:

                protected void Page_PreRender(object sender, EventArgs ea)

                {

                button1.Attributes.Add("onclick", String.Format("javascript:return

                S Offline
                S Offline
                srinandan
                wrote on last edited by
                #8

                thanks you Jesse but problem with here when return false then the server code is not firing i have need to visible=hidden in client and then fire the server code Regards, Srinandan

                J 1 Reply Last reply
                0
                • S srinandan

                  thanks Sylvester As soon the button will click at the moment it should be hide after that the Button1_Click() events should fire. in this method i am check some condition if the functionality goes success then no need to show that button if functionality failure then the button should visible its all. regards, Srinandan

                  S Offline
                  S Offline
                  Sylvester george
                  wrote on last edited by
                  #9

                  in the catch block you can make the button visible like the following line Button1.Visible = true;

                  Regards, Sylvester G sylvester_g_m@yahoo.com

                  S 1 Reply Last reply
                  0
                  • S Sylvester george

                    in the catch block you can make the button visible like the following line Button1.Visible = true;

                    Regards, Sylvester G sylvester_g_m@yahoo.com

                    S Offline
                    S Offline
                    srinandan
                    wrote on last edited by
                    #10

                    hi Sylvester I am doing this but this is not my problem.the problem with me when the button goes hidden in client the the server code is not firing. Regards, Srinandan..

                    1 Reply Last reply
                    0
                    • S srinandan

                      thanks you Jesse but problem with here when return false then the server code is not firing i have need to visible=hidden in client and then fire the server code Regards, Srinandan

                      J Offline
                      J Offline
                      Jesse Squire
                      wrote on last edited by
                      #11

                      I'm sorry, I must have misunderstood your post. If you'd like the server code to fire, simply change the javascript function to return true instead.

                      --Jesse "... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi

                      S 1 Reply Last reply
                      0
                      • J Jesse Squire

                        I'm sorry, I must have misunderstood your post. If you'd like the server code to fire, simply change the javascript function to return true instead.

                        --Jesse "... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi

                        S Offline
                        S Offline
                        srinandan
                        wrote on last edited by
                        #12

                        thanks Jesse:) my problem has been solved if rmove the return value or return the true. thanks for making reply. Regards, Srinandan

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups