Accessing contnet page's control in the Javascript code of master page

Hello

 I have one master page in which i put javascript code, CSS and one Content place Holder.

In my content page i add two control one for Login and another for pass word and one Custom validator control.

Now i want to call the Javascript code which is written in Master page as the Clinet function in my Custom Validator control to run to validate the value entered in the login and password(TextBox) control.

How i do this. My content page having Server side script that i want to call to validate page at the server side.

For submitting the page i use Button control and in its PostBackUrl i want to post the page on some other aspx page.

But neither i am able to validate the data of login and password control using Client side script(written in the master page) or using the server side script(written in the content page).

What should i do. Please help

Thank you.

0
crazybird_2005
11/19/2006 2:01:05 PM
asp.net.navigation-controls 13714 articles. 0 followers. Follow

9 Replies
820 Views

Similar Articles

[PageSpeed] 15

If your client function is written in master page, it must be available to all content pages.

Hope you have set the ControlToValidate & ClientValidationFunction properties correctly. To give you a hint of how to validate, your client function should look something like:

function clientFunction(sender,args)//must have two arguments

{

 //args.Value -- this is the Text in your textbox & you need to validate this

//set args.IsValid = true/false  based on your validation carried on args.Value 

 

To carryout the validation on the server, just define a handler for the ServerValidate event of your custom validator as:

validator.ServerValidate += new ServerValidateEventHandler(ServerValidateHandler); 

Then carry out validation in the handler like:

void ServerValidateHandler(object source, ServerValidateEventArgs args)
    {

 try
            {
                // Attempt validation on args.Value.
               
            }
            catch (ArgumentException e)
            {
                // Validation failed.
                args.IsValid = false;
            }

 

That's all.

Hope this helps.

Cheers!! 

 


Home Is Where the Wind Blows
http://pushpontech.blogspot.com
0
pushp_aspnet
11/20/2006 7:02:34 AM

Hello

 

Thank you very much for your reply. Bu as i write the code in the Page_Load event in VS.Net 2005

validator.ServerValidate += new ServerValidateEventHandler(ServerValidateHandler); 

it gives error message that ServerValidate event you cann't control directly, you need to use RaiseEvent function.

Another problem that i am facing now regarding validation using ClientSide script through CustomValidator control

is that when i enter value in the TextBox then only it call the javaScript function, but i donot enter any value in the TextBox it will not call the ClientSide javascript code.

 

I check the code, in simple case that without using Master page concept, this problem i face when i am trying to use Master page concept.

What should i do.

 

0
crazybird_2005
11/25/2006 7:27:07 AM

>it gives error message that ServerValidate event you cann't control directly, you need to use RaiseEvent function.

can you show me the relevant code(.aspx and .aspx.cs), that works fine for me. 

 >is that when i enter value in the TextBox then only it call the javaScript function, but i donot enter any value in the TextBox it will not call the ClientSide javascript code.

client validation code is executed whenever your textbox loses focus.



Home Is Where the Wind Blows
http://pushpontech.blogspot.com
0
pushp_aspnet
11/25/2006 9:02:41 AM

hello

Thank you very much for the fast response.

Here is the code

<%

@ Page Language="VB" MasterPageFile="~/SecondMasterPage.master" AutoEventWireup="false" CodeFile="FirstContentPage.aspx.vb" Inherits="Default2" title="Untitled Page" %>

<

asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<

br/><br/>

<asp:Label

id="lblLoginName"

Text="Login Name"

AssociatedControlID="txtLoginName"

Runat="server" />

<asp:TextBox

id="txtLoginName"

Runat="server" />

<asp:CustomValidator

id="valLogin"

ControlToValidate="txtLoginName"

Text="(Login field should not be Blank)"

ClientValidationFunction="valLogin_ClientValidate"

Runat="server" SetFocusOnError="True" ValidateEmptyText="True" />

<br/><br/>

<asp:Label

id="lblPassword"

Text="Password"

AssociatedControlID="txtPassword"

Runat="server" />

<asp:TextBox

id="txtPassword"

TextMode="Password"

Runat="server" />

<asp:CustomValidator

id="valPassword"

ControlToValidate="txtPassword"

Text="(Password field should not be Blank)"

ClientValidationFunction="valPassword_ClientValidate"

Runat="server" SetFocusOnError="True" ValidateEmptyText="True" />

<br/><br/>

<asp:Button

id="btnSubmit"

Text="Submit"

Runat="server" />

</

asp:Content>

 

Code behind page of the page

-------------------------------------------

Partial

Class Default2

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

 

 

End

Class

 

In the page_load event i want to write the code that you gave me

valPassword.ServerValidate += new ServerValidateEventHandler(ServerValidateHandler)

But it shows error in the above line.

 

I have one more problem

I want to call clinet side javascript code on Blur() event of TextBox,

it call properly but the problem is this in that code i am not able to access the control by its Id

What should i do.

Plz help

thank you

0
crazybird_2005
11/25/2006 10:07:26 AM
crazybird_2005:

In the page_load event i want to write the code that you gave me

valPassword.ServerValidate += new ServerValidateEventHandler(ServerValidateHandler)

But it shows error in the above line.

 

I have one more problem

I want to call clinet side javascript code on Blur() event of TextBox,

it call properly but the problem is this in that code i am not able to access the control by its Id

What should i do.

Hi,

The code i gave you is written in c#. You need to write the corresponding vb.net version.

this: valPassword.ServerValidate += new ServerValidateEventHandler(ServerValidateHandler)    would not work

I guess the vb.net version would look like:     AddHandler valPassword.ServerValidate AddressOf ServerValidateHandler

For your second problem, I didn't understand why do you want to access the textbox control when you already have its text in args.Value 

your client function may look like :  

------------------------------------------------- 

function valPassword_ClientValidate(sender, args)

{

 if(args.Value == "")

args.IsValid = false; 

-------------------------------------------------

Still in case you need to know, you can access your textbox as:

var txtPsswd = document.getElementById("<%=txtPassword.ClientID%>");

One last suggession: I think a simple required field validator would have served your purpose.

Hope this helps.

cheers!! 


Home Is Where the Wind Blows
http://pushpontech.blogspot.com
0
pushp_aspnet
11/25/2006 11:02:57 AM

Hello,

 

Thank you vry much for your support.

Actually when i am calling the Client side script using CustomValidator ClientScriptValidate property then it shows the erorr message whenuser submit the form and what i want as soon as LoginName and Password text box lose the focus it check whether user has entered value in the LoginName field or not.

If not then it shows proper error message.

As you gave suggestion to use the following code

var txtPasswd = document.getElementByID("<%=txtPassword.ClientID%>");

i write the code in the client script written in the Master page , but when i run the code it shows the error that txtPassword is not defined(code written in the client script")

I am giving the code for your reference:

function

valLogin_ClientValidate()

{

var cLoginName = document.getElementById("<%=txtLoginName.ClientID%>");

var mLoginName = cLoginName.value;

if(mLoginName.length == 0)

{

alert(

"Please enter value in the Login name field");

document.getElementById(

"<%=txtLoginName.ClientID%>").setfocus();

}

 

}

function valPassword_ClientValidate()

{

var cPassword = document.getElementById("<%=txtPassword.ClientID%>");

var mPassword = cPassword.value;

if(mPassword.length == 0)

{

alert(

"Please enter value in the Password name field");

document.getElementById(

"<%=txtPassword.ClientID%>").setfocus();

}

 

}

 

One more question, do we need to register the Client script if we write it in the CodeBehind file. If yes then how?

Plz help.

Thank you

0
crazybird_2005
11/27/2006 6:01:54 AM
crazybird_2005:

var txtPasswd = document.getElementByID("<%=txtPassword.ClientID%>");

i write the code in the client script written in the Master page , but when i run the code it shows the error that txtPassword is not defined(code written in the client script")

first thing, syntax error: getElementByID    its  getElementById

So you are writing your validation script in the master page itself, then you need to find the controls of the content page:

var txtPasswd = document.getElementById('<%=ContentPlaceHolder1.FindControl("txtPassword").ClientID%>');  //keep the syntax

 


Home Is Where the Wind Blows
http://pushpontech.blogspot.com
0
pushp_aspnet
11/27/2006 7:29:14 AM

Hi, I have the same problem, but with the server validator

protected

void Page_Load(object sender, EventArgs e)

{

validator.ServerValidate += new ServerValidateEventHandler(ServerValidateHandler); 

}

 

and then the function as follows:

void

ServerValidateHandler(object source, ServerValidateEventArgs args)

{

args.IsValid =

false;

}

but it is still redirecting me to the page that i have on the PostbackUrl property

 Please Helpme.

 

0
Alejo0481
11/30/2006 6:00:56 PM

did you call Validate mathod on your validator?

myCustomValidator.ServerValidate += new ServerValidateEventHandler(ServerValidateHandler);

say on buton click you want to do your validation then in handler for button click:

 myCustomValidator.Validate(); //this calls the servervalidate handler

Hope this clarifies.

 


Home Is Where the Wind Blows
http://pushpontech.blogspot.com
0
pushp_aspnet
12/1/2006 5:56:59 AM
Reply:

Similar Artilces:

How can I access the property a control on a master page from the master page's code behind.
On my master page I have a label. I want to change this label's text property based on an element of the querystring. (label.text = "NewValue")All child pages using this master will display the same thing so I was trying to put the code in the master page so as not to have to duplicate it for the child pages or put it in a base class and use FindControl.Is it possible to access from a master page's codebehind the property of a server control that is placed on a master page's aspx? If so how?(I saw similar questions, but they all came from the view of a child accessing the property.)Thank you...

How to access a Master's page control from Default.aspx page
I have a Label control in a MasterPage.master. How can I to modify this Text propierty from Default.aspx. The master page of default.aspx is MasterPage.master. Thanks Hi, you could expose the label's text as a public property on the master page. In the webform use the @MasterType directive and you can strongly typed call the property. Take a look at this page: http://msdn2.microsoft.com/en-us/library/xxwa0ff0.aspx. Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Workaround for non working Mark as answer buttons....

Accessing a master page's control in a nested content Page(Urgent)
Hi all, I have a masterpage rm_master.masterand another masterpage rm_sub.master which inherits from rm_master.master. Now I want to access a dropdownlist control in a content page which derieves from rm_select_master. the code i used wasDropDownList DropDownList1 = (DropDownList)Master.FindControl("DropDownList1");string a; a = DropDownList1.Text;   The error i got was "Object reference not set to an instance of an object". Please help.. Thanks in advance.. Bharath You can try Page.FindControl or use the mastertype directivePlease remember to click “...

How to set a Master Page's control's property through content page on a postback event?
Hi all, i m using a master page in my application. i have a panel on master page and a tab container with three tab panels on content page. i want to set/change  the visibility of the panel control when the tab is changed on the content page. By default the panel is invisible. To implement this i executed the following steps: 1. Set AutoPostBack of tacontainer to true 2. Wrote an event handler for ActiveTabIndexChanged event of the tab container 3 Wrote the following code on Tab changed event   if (TabContainerEmpPerformance.ActiveTabIndex == 1) { ...

Accessing controls from the master page's code behind
When I select an item in the TreeView on my master page the navigateurl property on the selected node takes the user to a new page. When the page loads for some reason, all nodes in my treeview are expanded even though I set ExpandDepth="1" on the treeview. So since that wouldn't work, I tried to use the OnSelectedNodeChanged method. In the code behind of my master I tried to access the treeview but when I try to compile I get: "Object reference not set to an instance of an object.". I know I am typing the treeview's id right because intellisense is picking it up. Here is some pertinent...

CS0012 when referencing controls on master page's master page.
 I'm working with a content page that has a master page (we'll call it ChildMaster). ChildMaster also has a master page (ParentMaster). There is a control on ParentMaster I need to access from the content page. The code looks like this:  ControlType controlInstance = (ControlType) Master.Master.FindControl("controlID");  When I upload, I get CS0012 telling me that ParentMaster is defined in an assembly that is not referenced, and I need to add a reference to some assembly with a funky random-looking name (which changes when I reupload ParentMaster). Up t...

How can I access Master page properties in a user control's code-behind?
I have a  page that uses a master page, and also has a user control on it, I would like to acces the master page class properties from code-behind in user control. Ho could I do this? Note I am NOT asking how to access Master page properties in page's code behind but a user control's code behind.sun21170 UserControl has the Page property. In Page property, you can to access the MasterPage. Is it posible you coul dgive a code example .... could use this functionality myself ... many thanks I just found the answer to my question. The answer is as below and it's in ...

Problem Accessing Page Controls in a page that has a master page.
Hi,  I have a problem with accessing the controls of a page that has a masterpage. I am trying to access the Page.Controls from the code behind in the class file but no controls are returned.I can access single controls (ex. txtAddress.Text) but not the whole array of controls (ex. Page.Controls).  The controls structure is..                                 MasterPage  >  PlaceHolder > MyPage.aspx &n...

How to access a control in the Content Page from javascript in Master Page?
How do I access a control in the Content Page from javascript in Master Page? Say, I have a dropdown list 'CountryDDL' in my Content Page. When I click on a button in the Content Page, I call a javascript function. The javascript file is included in the master page. From within this javascript function, I have to access CountryDDL and find the selected index etc... Because of Master page - content page hierarchy, document.getElementById('CountryDDL') doesnt seem to work. Is there  any alternate approach? use  document.getElementById('<%Countr...

Content controls have to be top-level controls in a content page or a nested master page that references a master page.
Why in the world am I getting this error? The solutions I have seen either don't apply or don't work.  Here is my master page code:<%@ Master Language="C#" AutoEventWireup="false" CodeFile="TechLMSMasters/MainSide.master.cs" Inherits="MainSide" %> <%@ Register TagPrefix="usercontrol" TagName="Search" Src="~/library/uc/Search.ascx" %> <%@ Register TagPrefix="usercontrol" TagName="UserName" Src="~/library/uc/UserNameDisplay.ascx" %><!DOCTYPE html PUBLI...

Error : Content controls have to be top-level controls in a content page or a nested master page that references a master page.
when I make a directory on my root, and in that directory I put a masterpage and a web form ... I get always an error : Content controls have to be top-level controls in a content page or a nested master page that references a master page.   Does somebody know what the problem is ? Is it possible you can't set a masterpage in a directory ... ?Kind Regards,Joeri Pansaerts.From Bruges, Belgium :) i've founded ... I had to nest my masterpage ...Kind Regards,Joeri Pansaerts.From Bruges, Belgium :) Hi Joeri Pansaerts, I am receiving the same error as the one posted her...

ERROR Content controls have to be top-level controls in a content page or a nested master page that references a master page.
This all used to work until about 5 minutes ago. Did I change something? For some reason I am now getting this error: Content controls have to be top-level controls in a content page or a nested master page that references a master page. What I have:  I have a masterpage inside of a directory. That directory is a first level directory with its own web.config file....The whole thing is protected by the web.config file above it (in the main public directory). That main public directory has a masterpage as well. I am not currently linking the two masterpages together becuase...

accessing content page controls from a javascript which is included in master page
Hi - Is there any way of accessing the controls (for example getting textbox's text) of content page from the javascript which is included in the master page? "OR" How can I inlcude javascript in a content page which is integrated/created with a master page? Here is the situation where I am stuck: I have several aspx pages. On button click events I insert data into the database from those pages. Some pages require administrator and other team memeber's attention, so I send email to the users on button click events too. but the javascript that sends emails had to inclu...

Access parent page's controls from a user control
I have a .ascx user control that contains a button. I place this user control on a .aspx web page. The web page contains a textbox. When I click on the button in the user control I want the data in the textbox (in the web page) to be saved to the database. I want to write this code in the click event of the user control's button. Is there any way I can access the parent web page's controls from the user control? Add event to user control which fires on page when button inside user control is clicked. view post 779045Jigar Desai-----------------------Do not forget to "Mark as Answer...

Web resources about - Accessing contnet page's control in the Javascript code of master page - asp.net.navigation-controls

Resources last updated: 11/29/2015 1:10:54 AM