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. Trouble in using Master page and javascript ???

Trouble in using Master page and javascript ???

Scheduled Pinned Locked Moved ASP.NET
javascriptquestion
13 Posts 5 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.
  • V vicky457

    Hi, I am using a Master page for a child page that requires do Client-side data validation using Javascript. The function in Javascript need to take the form as argument, so it can access all the controls within the form. The javascript is registered in the child page. When I tried to pass the form argument into javascript funciton, it alway fails. If I do it without using master page, it works fine. Is there anything particular I need to take care when using a master page for a child page which contains client-side validation? Here is how the code I used: Can anybody give me some hint about what I do wrong? Thanks in advance!

    I Offline
    I Offline
    iamrashida83
    wrote on last edited by
    #2

    why dont u use something like this: ...="return DataValidation('Form1')"

    Regards, Rashida Jabeen http://www.akaas.net[^]

    V 1 Reply Last reply
    0
    • V vicky457

      Hi, I am using a Master page for a child page that requires do Client-side data validation using Javascript. The function in Javascript need to take the form as argument, so it can access all the controls within the form. The javascript is registered in the child page. When I tried to pass the form argument into javascript funciton, it alway fails. If I do it without using master page, it works fine. Is there anything particular I need to take care when using a master page for a child page which contains client-side validation? Here is how the code I used: Can anybody give me some hint about what I do wrong? Thanks in advance!

      G Offline
      G Offline
      Grapes R Fun
      wrote on last edited by
      #3

      Hi Vicky! That approach dowsn't work because during page rendering (i.e. result page from server to client) the names of controls change IF you have created a nested environment. For example, if you have a master page that has ContentPlaceHolder1, and one nested child page displayed through that ContentPlaceHolder1 with a text box control and a button (with their default names) this would be rendered with these id's: ctl00_ContentPlaceHolder1_TextBox1 (instead of TextBox1) ctl00_ContentPlaceHolder1_Button1 (instead of Button1) If you have a nested master page, then the controls would have these id's: ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_TextBox1 (instead of TextBox1) ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_Button1 (instead of Button1) You can pass those control id's to your javascript, it should work. However, there are other ways of 'programmatically' retrieving the server control's id, but that requires interfacing with ICallbackEventHandler and yada yada yada... I find it best to keep things simple :) BTW Vicky, I couldn't see your code! Can you paste it once more so maybe I could be more specific in my answer?

      Nila "...for that, I will need a large cup of coffee and a brand muffin!" -Samantha Bea

      V 1 Reply Last reply
      0
      • V vicky457

        Hi, I am using a Master page for a child page that requires do Client-side data validation using Javascript. The function in Javascript need to take the form as argument, so it can access all the controls within the form. The javascript is registered in the child page. When I tried to pass the form argument into javascript funciton, it alway fails. If I do it without using master page, it works fine. Is there anything particular I need to take care when using a master page for a child page which contains client-side validation? Here is how the code I used: Can anybody give me some hint about what I do wrong? Thanks in advance!

        A Offline
        A Offline
        Anshuman Singh
        wrote on last edited by
        #4

        see if you accessing a textbox with id like txtName then better user of document.getelementById('<% txtName.clientId %>').value it will work with your master pages.

        Anshuman Singh [anshumas@rediffmail.com](mailto:Anshuman Singhanshumas@rediffmail.com)

        1 Reply Last reply
        0
        • V vicky457

          Hi, I am using a Master page for a child page that requires do Client-side data validation using Javascript. The function in Javascript need to take the form as argument, so it can access all the controls within the form. The javascript is registered in the child page. When I tried to pass the form argument into javascript funciton, it alway fails. If I do it without using master page, it works fine. Is there anything particular I need to take care when using a master page for a child page which contains client-side validation? Here is how the code I used: Can anybody give me some hint about what I do wrong? Thanks in advance!

          A Offline
          A Offline
          ayeleteric
          wrote on last edited by
          #5

          You can also check the Id of your controls in run-time clicking on viewsource.

          Eric H.

          1 Reply Last reply
          0
          • G Grapes R Fun

            Hi Vicky! That approach dowsn't work because during page rendering (i.e. result page from server to client) the names of controls change IF you have created a nested environment. For example, if you have a master page that has ContentPlaceHolder1, and one nested child page displayed through that ContentPlaceHolder1 with a text box control and a button (with their default names) this would be rendered with these id's: ctl00_ContentPlaceHolder1_TextBox1 (instead of TextBox1) ctl00_ContentPlaceHolder1_Button1 (instead of Button1) If you have a nested master page, then the controls would have these id's: ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_TextBox1 (instead of TextBox1) ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_Button1 (instead of Button1) You can pass those control id's to your javascript, it should work. However, there are other ways of 'programmatically' retrieving the server control's id, but that requires interfacing with ICallbackEventHandler and yada yada yada... I find it best to keep things simple :) BTW Vicky, I couldn't see your code! Can you paste it once more so maybe I could be more specific in my answer?

            Nila "...for that, I will need a large cup of coffee and a brand muffin!" -Samantha Bea

            V Offline
            V Offline
            vicky457
            wrote on last edited by
            #6

            Hi Nila, Here is the code I used when calling the JavaScript function: Would you mind to refer me more information about ICallbackEventHandler? Anyway, I will try the approach of passing control id. Thank you very much for your help!

            V G 3 Replies Last reply
            0
            • V vicky457

              Hi Nila, Here is the code I used when calling the JavaScript function: Would you mind to refer me more information about ICallbackEventHandler? Anyway, I will try the approach of passing control id. Thank you very much for your help!

              V Offline
              V Offline
              vicky457
              wrote on last edited by
              #7
              1 Reply Last reply
              0
              • V vicky457

                Hi Nila, Here is the code I used when calling the JavaScript function: Would you mind to refer me more information about ICallbackEventHandler? Anyway, I will try the approach of passing control id. Thank you very much for your help!

                V Offline
                V Offline
                vicky457
                wrote on last edited by
                #8

                It seems that the forum doesn't take some special characters. Let me try again : This is the button control : .... ID="btnSubmit" runat="server" Text="onClick" onclientclick="return DataValidation(Form1)" ....

                G 1 Reply Last reply
                0
                • I iamrashida83

                  why dont u use something like this: ...="return DataValidation('Form1')"

                  Regards, Rashida Jabeen http://www.akaas.net[^]

                  V Offline
                  V Offline
                  vicky457
                  wrote on last edited by
                  #9

                  I did try to pass 'Form1'.. but it doesn't work either.

                  1 Reply Last reply
                  0
                  • V vicky457

                    Hi Nila, Here is the code I used when calling the JavaScript function: Would you mind to refer me more information about ICallbackEventHandler? Anyway, I will try the approach of passing control id. Thank you very much for your help!

                    G Offline
                    G Offline
                    Grapes R Fun
                    wrote on last edited by
                    #10

                    Hi Vicky, Your best bet as far as learning how to implement and work with ICallbackEventHandler interface is to look up msdn2: http://msdn2.microsoft.com/en-us/library/system.web.ui.icallbackeventhandler(VS.80).aspx There are many other developer sites, including codeproject, where you can get real-life examples of ICallbackEventHandler implementation. However, when you are doing your research, make sure to look up only those that have implemented TWO methods associated with this interface void RaiseCallbackEvent(String eventArgument) and String GetCallbackResult() Some examples are from 2.0 beta and have only one method. Here is a good code example: http://msdn2.microsoft.com/en-us/library/ms366515(vs.80).aspx

                    Nila "...for that, I will need a large cup of coffee and a brand muffin!" -Samantha Bea

                    1 Reply Last reply
                    0
                    • V vicky457

                      It seems that the forum doesn't take some special characters. Let me try again : This is the button control : .... ID="btnSubmit" runat="server" Text="onClick" onclientclick="return DataValidation(Form1)" ....

                      G Offline
                      G Offline
                      Grapes R Fun
                      wrote on last edited by
                      #11

                      Vicky, the "onClick" should be the name of the client side method you pass onto the javascript, and not the text property of your button. Anyways, this is the correct button name for you: ctl00_ContentPlaceHolder1_btnSubmit Let me know if you are still having problems.

                      Nila "...for that, I will need a large cup of coffee and a brand muffin!" -Samantha Bea

                      V 1 Reply Last reply
                      0
                      • G Grapes R Fun

                        Vicky, the "onClick" should be the name of the client side method you pass onto the javascript, and not the text property of your button. Anyways, this is the correct button name for you: ctl00_ContentPlaceHolder1_btnSubmit Let me know if you are still having problems.

                        Nila "...for that, I will need a large cup of coffee and a brand muffin!" -Samantha Bea

                        V Offline
                        V Offline
                        vicky457
                        wrote on last edited by
                        #12

                        Nila, I have tried the simpliest Javascript function. Still no luck... Here is what I did: ---Master page : include ..script type="text/javascript" src="Test.js" .. in the head tag. ---Child page: within Content tag, only have one button named Button1, then onclientclick event call "return TestForm(aspnetForm);", where aspnetForm is the form name I got from rendered HTML page by viewing source. ---Test.js file: Only one function which should take aspnetForm as parameter and do a simple alert Button1's value: function TestForm(form) { with (form) { alert (_ctl0:ContentPlaceHolder1:Button1.value); return false; } } where _ctl0:ContentPlaceHolder1:Button1 is the Button1's name I got form rendered HTML page by viewing source. Basically, what I try to do is to include a .js file in master page, then pass form into a javascript function,finally access the controls in the form to check some controls's value and do data validation. I really have no idea what is wrong with this simple test... Could you tell what I am missing? I really appreciate your help! Thanks a lot!

                        G 1 Reply Last reply
                        0
                        • V vicky457

                          Nila, I have tried the simpliest Javascript function. Still no luck... Here is what I did: ---Master page : include ..script type="text/javascript" src="Test.js" .. in the head tag. ---Child page: within Content tag, only have one button named Button1, then onclientclick event call "return TestForm(aspnetForm);", where aspnetForm is the form name I got from rendered HTML page by viewing source. ---Test.js file: Only one function which should take aspnetForm as parameter and do a simple alert Button1's value: function TestForm(form) { with (form) { alert (_ctl0:ContentPlaceHolder1:Button1.value); return false; } } where _ctl0:ContentPlaceHolder1:Button1 is the Button1's name I got form rendered HTML page by viewing source. Basically, what I try to do is to include a .js file in master page, then pass form into a javascript function,finally access the controls in the form to check some controls's value and do data validation. I really have no idea what is wrong with this simple test... Could you tell what I am missing? I really appreciate your help! Thanks a lot!

                          G Offline
                          G Offline
                          Grapes R Fun
                          wrote on last edited by
                          #13

                          Vicky, this sounds bizzare... although I had issues too when I would point to a javascript file instead of embedding it within the HTML. If you only want an alert box, then do this: In your child page, add the following line within the Page_Load event: protected void Page_Load(object sender, EventArgs e) { ... if (!Page.IsPostBack) //executes only during the initial load { this.Button1.Attributes.Add("onclick", "alert('any message can go here...');"); } } This would be a simple way to do alert. Try and see if it works, if not, send your source code to me NightHawk13@hotmail.com (if you feel comfortable doing so) and I will try to correct your code.

                          Nila "...for that, I will need a large cup of coffee and a brand muffin!" -Samantha Bea

                          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