本文介绍了使用JavaScript计算ItemTemplate控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



尝试在表格中计算文本框值时出现此错误.
Microsoft JScript运行时错误:``ContentPlaceHolder1_GD_Prod_ctl00_ctl04_TxtPPort''未定义

任何帮助表示赞赏.这是我的代码.

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

推荐答案

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);
}



看那里.您将TxtPPort传递给函数,然后再次在函数内部创建var TxtPPort.

您可以使用




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

You can use


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


Microsoft JScript runtime error: ''ContentPlaceHolder1_GD_Prod_ctl00_ctl04_TxtPPort'' is undefined



因此,该标识符会带来一些麻烦.复制ContentPlaceHolder1_GD_Prod_ctl00_ctl04_TxtPPort并尝试在您的代码中查找.不,它不在那里!错误消息显示它应该在您的代码中,但是您没有显示这部分代码.您认为任何人都可以找到您的错误?

我希望这个简单的想法可以帮助您找到错误,而无需进一步的帮助.

-SA



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


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''); 
}



您在上面的代码中关心的重要部分是如何在gridview中创建控件的id rowID +''_TxtPPort''
--Pankaj



Important part to your concern in above code is how the id for the controls in gridview is get created rowID + ''_TxtPPort''
--Pankaj


这篇关于使用JavaScript计算ItemTemplate控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 00:29