Trouble in using Master page and javascript ???
-
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!
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)
-
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!
You can also check the Id of your controls in run-time clicking on viewsource.
Eric H.
-
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
-
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!
-
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!
-
why dont u use something like this: ...="return DataValidation('Form1')"
Regards, Rashida Jabeen http://www.akaas.net[^]
-
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!
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)
andString 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).aspxNila "...for that, I will need a large cup of coffee and a brand muffin!" -Samantha Bea
-
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)" ....
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
-
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
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!
-
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!
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