在javascript中从嵌套gridview获取文本框更改的值 [英] Get textbox changed value from nested gridview in javascript
问题描述
我想在Javascript中计算onblur事件中两个文本框值的总和。这些文本框在嵌套的Gridview中。
它已经加载了已经加载的值(当表单加载第一次时)很好但是当我更改文本框的值时它没有'在Javascript中获取更改的值仍然是第一次加载值。
为什么会发生这种情况?
这里我如何将Javascript分配给嵌套的Gridview文本框。
gr_pendingApproval 是主网格视图和 gr_correctionDT 是嵌套的Gridview。
代码:
I want to calculate sum of two text box values on "onblur" event in Javascript. These text boxes are inside nested Gridview.
It get the already loaded value (when form load first time) fine but when i change the value of text box it doesn't get the changed value in Javascript still it is getting the first time loaded value.
Why is this happening?
Here how i assign the Javascript to nested Gridview text boxe.
gr_pendingApproval is Main Gridview and gr_correctionDT is nested Gridview.
Code:
Protected Sub gr_pendingApproval_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gr_pendingApproval.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim LyceonianNo As String = gr_pendingApproval.DataKeys(e.Row.RowIndex).Value.ToString()
Dim gr_correctionDT As GridView = DirectCast(e.Row.FindControl("gr_correctionDT"), GridView)
gr_correctionDT.DataSource = SqlDS_pendingApproval_SP
SqlDS_pendingApproval_SP.SelectParameters.Clear() 'Remove all select parameters
'from the datasource for the expanded details
SqlDS_pendingApproval_SP.SelectParameters.Add("lycNo", CInt(LyceonianNo)) 'Add the select parameter
'to the datasource for the expanded details using the unique ID of the record
gr_correctionDT.DataBind()
Dim rowIndex As Integer = CInt(e.Row.DataItemIndex) + 1
evtHandler = "viewDetail(" + gr_pendingApproval.ClientID + "," + rowIndex.ToString + ",1)"
DirectCast(e.Row.FindControl("btn_viewCorrectionDetail"), HtmlInputButton).Attributes.Add("onclick", evtHandler)
evtHandler = "viewDetail(" + gr_pendingApproval.ClientID + "," + rowIndex.ToString + ",0)"
DirectCast(e.Row.FindControl("lnk_otherDT"), HtmlAnchor).Attributes.Add("onclick", evtHandler)
For Each row As GridViewRow In gr_correctionDT.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim rowInnerIndex As Integer = CInt(row.DataItemIndex) + 1
Dim btn As HtmlInputButton = DirectCast(row.FindControl("btn_viewIndividualDetail"), HtmlInputButton)
Dim tboxCommon As TextBox
evtHandler = "viewIndividualDetail(" + gr_correctionDT.ClientID + "," + rowIndex.ToString + "," + rowInnerIndex.ToString + ")"
btn.Attributes.Add("onclick", evtHandler)
evtHandler = "updateValue(" + gr_correctionDT.ClientID + "," + rowIndex.ToString + "," + rowInnerIndex.ToString + ")"
tboxCommon = DirectCast(row.FindControl("Txt_defer"), TextBox)
tboxCommon.Attributes.Add("onblur", evtHandler)
tboxCommon = DirectCast(row.FindControl("Txt_subtract"), TextBox)
tboxCommon.Attributes.Add("onblur", evtHandler)
End If
Next
End If
End Sub
Java脚本函数
Java Script Function
function updateValue(theGrid, parentRowIdx, childRowIndex) {
var grName = "ContentPlaceHolder1_gr_pendingApproval_gr_correctionDT_" + (parentRowIdx - 1);
var GridCntrl = document.getElementById(grName);
var totalOrg = 0;
var cellCount = GridCntrl.rows[childRowIndex].cells.length - 2;
var deferOrg = GridCntrl.rows[childRowIndex].cells[3].children[0].value;
var defer = GridCntrl.rows[childRowIndex].cells[3].children[1].value;
var subtractOrg = GridCntrl.rows[childRowIndex].cells[5].children[0].value;
var subtract = GridCntrl.rows[childRowIndex].cells[5].children[1].value;
totalOrg = +deferOrg + +subtractOrg;
if ((+defer + +subtract) <= totalOrg) {
}
else {
GridCntrl.rows[childRowIndex].cells[2].children[1].value = deferOrg;
GridCntrl.rows[childRowIndex].cells[5].children[1].value = subtractOrg;
alert("Cannot made correction")
}
}
图片1
http://i.stack.imgur.com/qWy4k.jpg [ ^ ]
图片2
http://i.stack.imgur.com/hEczP.jpg [ ^ ]
Image 1
http://i.stack.imgur.com/qWy4k.jpg[^]
Image 2
http://i.stack.imgur.com/hEczP.jpg[^]
推荐答案
你需要在javascript中编写一个通用函数来计算你想要添加的文本框中所有输入值的总和。 />
首先你需要找到文本框的id和值。
这个看到aspx浏览器中的页面,看到文本框的id,然后写一个共同的f。
在javascript中进行操作并在文本框的onblur上调用它。
喜欢这样
you need to write a common function in javascript that calculate the sum of all enter value in textbox that you want add.
for this first of all you need find out the textbox id and values.
for this see the aspx page in browser,see the id of textbox,
then write a common function in javascript and call it on onblur of textbox.
like that
<asp:TextBox CssClass="txtbox" ID="txtAmount" onblur="addvalue(this.id,this.value)" runat="server"
value='<%#Container.DataItem("amount") %>' MaxLength="15" Width="100px"></asp:TextBox>
函数addvalue(id,value){
**这里写代码**
}
看到像这样的控制ID
document.getElementById(grid1__ctl1_grid2__ctl1_txtbox1)
function addvalue(id,value){
**write code here**
}
see control id like this
document.getElementById("grid1__ctl1_grid2__ctl1_txtbox1")
这篇关于在javascript中从嵌套gridview获取文本框更改的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!