Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

I am getting this error when try to do calculate textbox values in grid.
Microsoft JScript runtime error: 'ContentPlaceHolder1_GD_Prod_ctl00_ctl04_TxtPPort' is undefined

Any help is appreciated. Here is my code.

client code:
function Total(TxtPPort, TxtBFPort, TxtLOPort, TxtTSPort) {
var TxtPPort = document.getElementById("TxtPPort");
var TxtBFPort = document.getElementById("TxtBFPort");
var TxtLOPort = document.getElementById("TxtLOPort");
var TxtTSPort = document.getElementById("TxtTSPort");
TxtTSPort.value = parseInt(TxtPPort.value) + parseInt(TxtBFPort.value) - parseInt(TxtLOPort.value);
}


vb code:
Private Sub GD_Prod_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles GD_Prod.ItemCreated
If TypeOf e.Item Is GridDataItem Then
Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
Dim T1 As TextBox = TryCast(item.FindControl("TxtPPort"), TextBox)
Dim T2 As TextBox = TryCast(item.FindControl("TxtBFPort"), TextBox)
Dim T3 As TextBox = TryCast(item.FindControl("TxtLOPort"), TextBox)
Dim T4 As TextBox = TryCast(item.FindControl("TxtTSPort"), TextBox)
T1.Attributes.Add("onblur", ((("Total(" + T1.ClientID & ",") + T2.ClientID & ",") + T3.ClientID & ",") + T4.ClientID & ")")
T2.Attributes.Add("onblur", ((("Total(" + T1.ClientID & ",") + T2.ClientID & ",") + T3.ClientID & ",") + T4.ClientID & ")")
T3.Attributes.Add("onblur", ((("Total(" + T1.ClientID & ",") + T2.ClientID & ",") + T3.ClientID & ",") + T4.ClientID & ")")
End If
End Sub
Posted
Updated 1-Mar-11 5:02am
v2
Comments
HimanshuJoshi 1-Mar-11 11:02am    
Edited to add pre blocks.

You are passing the client ID to the function Total. Then why you are creating another variable inside the function same like the variable you are passing?

C#
function Total(TxtPPort, TxtBFPort, TxtLOPort, TxtTSPort) {
var TxtPPort = document.getElementById("TxtPPort");
var TxtBFPort = document.getElementById("TxtBFPort");
var TxtLOPort = document.getElementById("TxtLOPort");
var TxtTSPort = document.getElementById("TxtTSPort");
TxtTSPort.value = parseInt(TxtPPort.value) + parseInt(TxtBFPort.value) - parseInt(TxtLOPort.value);
}


See there. You passing TxtPPort to the function and again creating a var TxtPPort inside the function.

You can use


C#
function Total(TxtPPort, TxtBFPort, TxtLOPort, TxtTSPort) {
var TxtPPort = document.getElementById(TxtPPort);
var TxtBFPort = document.getElementById(TxtBFPort);
var TxtLOPort = document.getElementById(TxtLOPort);
var TxtTSPort = document.getElementById(TxtTSPort);
TxtTSPort.value = parseInt(TxtPPort.value) + parseInt(TxtBFPort.value) - parseInt(TxtLOPort.value);
}


instead. Try that
 
Share this answer
 
Comments
Rohit.Net100 1-Mar-11 13:18pm    
tried above code also, by eliminating double quotes. but still no luck.
pankajupadhyay29 1-Mar-11 13:26pm    
nice just change variable names use different in parameter and inside function.
Albin Abel 1-Mar-11 14:04pm    
Hi another problem in your code is you are not passing the client id in side quotes. Client Id is a string. So need to pass to a script in single quotes as shown below.

TxtPPort.Attributes.Add("onblur", "Total('" + T1.ClientID + "','" + T2.ClientID + "','" + T3.ClientID + "','" + T4.ClientID + "')");
TxtBFPort.Attributes.Add("onblur", "Total('" + T1.ClientID + "','" + T2.ClientID + "','" + T3.ClientID + "','" + T4.ClientID + "')");
TxtLOPort.Attributes.Add("onblur", "Total('" + T1.ClientID + "','" + T2.ClientID + "','" + T3.ClientID + "','" + T4.ClientID + "')");

try using the above. This one is c# syntax. You can convert to vb.net if it gives syntax errors. But the idea is you need to pass the client ID in single quotes
Can you follow simple logic: read your own error message:

Microsoft JScript runtime error: 'ContentPlaceHolder1_GD_Prod_ctl00_ctl04_TxtPPort' is undefined


So, this identifier is a source of some trouble; copy ContentPlaceHolder1_GD_Prod_ctl00_ctl04_TxtPPort and try to find in your code. No, it is not there! Error message shows it should be in your code, but you don't show this part of code. How do you think anyone can find your bug?

I hope this simple idea helps you to find a bug without further help.

—SA
 
Share this answer
 
v2
Comments
Rohit.Net100 1-Mar-11 11:30am    
But, where should i define 'ContentPlaceHolder1_GD_Prod_ctl00_ctl04_TxtPPort' in my code.
Sergey Alexandrovich Kryukov 1-Mar-11 13:46pm    
No, your error message say it is already there. Find it!
--SA
Rohit.Net100 1-Mar-11 13:52pm    
i checked, 'ContentPlaceHolder1_GD_Prod_ctl00_ctl04_TxtPPort' wasn't exists on my page.
if i need to add it where should i add ?
Sergey Alexandrovich Kryukov 1-Mar-11 14:35pm    
How can I know that? Again, if the error message complain about identifier, this identified should be attempted to reference at this point. Find this point. Don't tell me ContentPlaceHolder1_GD_Prod_ctl00_ctl04_TxtPPort is not found, it would mean your error message does not exist.
--SA
Albin Abel 1-Mar-11 12:32pm    
hi that is the ClientID of the control TxtPPort. Make sure the control is there in your page
you should the following convention for accessing gridview controls in javascript you can either solve your problem by loop through all rows or you can pass rowno to your function.

var gridID = "<%= GridView1.ClientID %>";
var grid = document.getElementById(gridID);
var RowCount = parseInt("<%= GridView1.Rows.Count %>");
var selection="";
for (var i = 2; i <= RowCount + 1; i++) {
    var rowID = gridID + '_ctl' + (i > 9 ? i.toString() : '0' + i.toString());
    var txt1= document.getElementById(rowID + '_TxtPPort'); 
}


Important part to your concern in above code is how the id for the controls in gridview is get created rowID + '_TxtPPort'
--Pankaj
 
Share this answer
 
Comments
Rohit.Net100 1-Mar-11 13:07pm    
still i am getting same error, after using above javascript code.
pankajupadhyay29 1-Mar-11 13:08pm    
what changes have you made.
Rohit.Net100 1-Mar-11 13:17pm    
i change my JScript function:-


function Total(TxtPPort, TxtBFPort, TxtLOPort, TxtTSPort) {
var gridID = "<%= GD_Prod.ClientID %>";
var grid = document.getElementById(gridID);
var RowCount = parseInt("<%= GD_Prod.Items.Count %>");
var selection="";
for (var i = 2; i <= RowCount + 1; i++) {
var rowID = gridID + '_ctl' + (i > 9 ? i.toString() : '0' + i.toString());
var TxtPPort = document.getElementById(rowID + '_TxtPPort');
var TxtBFPort = document.getElementById(rowID + '_TxtBFPort');
var TxtLOPort = document.getElementById(rowID + '_TxtLOPort');
var TxtTSPort = document.getElementById(rowID + '_TxtTSPort');
TxtTSPort.value = parseInt(TxtPPort.value) + parseInt(TxtBFPort.value) - parseInt(TxtLOPort.value);
}
pankajupadhyay29 1-Mar-11 13:23pm    
this will change to--
function Total() {
var gridID = "<%= GD_Prod.ClientID %>";
var grid = document.getElementById(gridID);
var RowCount = parseInt("<%= GD_Prod.Items.Count %>");
var selection="";
for (var i = 2; i <= RowCount + 1; i++) {
var rowID = gridID + '_ctl' + (i > 9 ? i.toString() : '0' + i.toString());
var TxtPPort = document.getElementById(rowID + '_TxtPPort');
var TxtBFPort = document.getElementById(rowID + '_TxtBFPort');
var TxtLOPort = document.getElementById(rowID + '_TxtLOPort');
var TxtTSPort = document.getElementById(rowID + '_TxtTSPort');
TxtTSPort.value = parseInt(TxtPPort.value) + parseInt(TxtBFPort.value) - parseInt(TxtLOPort.value);
}
Rohit.Net100 1-Mar-11 13:39pm    
no luck :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900