Text Boxes Focusing Not Workng

Posted by Shoyebaziz123 under ASP.NET on 4/2/2010 | Views : 1085 | Status : [Member] | Replies : 2
Hi All,

I have an application in which i have Master page and a folder for Javascript .
In .js File i have written code


function changeSColor(txtbox1)
{
txtbox1.style.background='silver';
txtbox1.style.font='bold';
}
function changeDColor(txtbox1)
{
txtbox1.style.background='White' ;
txtbox1.style.font='normal';
}

in master page

i have import its reference

<script src="JavaScript/JFocus.js" type="text/javascript"></script>

now in my default page i have added master page and
at page load

txtQtyPu.Attributes.Add("onFocus", "javascript:changeSColor(txtQtyPu)");
txtQtyPu.Attributes.Add("onBlur", "javascript:changeDColor(txtQtyPu)");

now while i am running the application

" Microsoft JScript runtime error: 'txtQtyPu' is undefined "

what is going wrong ?

Thanks




Responses

Posted by: Vuyiswamb on: 4/2/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
First of all the is a difference between onFocus and onfocus or onBlur and onblur , you must always remeber that javascript is case sensative. i did not manage to to reproduce your error but it does exactly what you want to do i have my small example here, first i created the page like this

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>

<script type="text/javascript" src="JFocus.js">

// txtQtyPu.Attributes.Add("onfocus", "javascript:changeSColor(txtQtyPu)");
// txtQtyPu.Attributes.Add("onblur", "javascript:changeDColor(txtQtyPu)");

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="txtQtyPu" onblur="changeSColor(txtQtyPu)" onfocus="changeDColor(txtQtyPu)" type="text" /><br />
<br />

<asp:TextBox ID="Text1" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></div>
</form>
</body>
</html>


and added an external javascript like this

// JScript File

function changeSColor(txtbox1)
{
txtbox1.style.background='silver';
txtbox1.style.font='bold';
return false;

}
function changeDColor(txtbox1)
{
txtbox1.style.background='White' ;
txtbox1.style.font='normal';
return false;
}


and tested it, when the focus on the the textbox the function changeDColor gets fired and the background turns white and when you move away the function changeSColor gets fired and the text become bold and the background turns silver.

Hope this is what you want

Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Shoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayeshl on: 4/5/2010 [Member] Starter

Up
0
Down
function changeSColor(txtbox1)
{
txtbox1.style.background='silver';
txtbox1.style.font='bold';
}
function changeDColor(txtbox1)
{
txtbox1.style.background='White' ;
txtbox1.style.font='normal';
}

in master page

i have import its reference

<script src="JavaScript/JFocus.js" type="text/javascript"></script>

now in my default page i have added master page and
at page load

txtQtyPu.Attributes.Add("onFocus", "javascript:changeSColor("+txtQtyPu.ClientId+")");
txtQtyPu.Attributes.Add("onBlur", "javascript:changeDColor("+txtQtyPu.ClientId+")");

try this code for textbox onFocus and onBlur

thanx
happy coding

from
Jayesh L

Shoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response