读书人

js里获得GridView模板列的TextBox做批

发布时间: 2012-03-21 13:33:15 作者: rapoo

js里获得GridView模板列的TextBox做批量增加的问题
例: gridview模板列分别有三个文本框TextBox1(基本),TextBox2(津贴),TextBox3(合计).现在我想在前台页面js里获得当用户输入后的这三个文本框的值然后再作实时计算。如果只是在页面上很简单.直接就TextBox3的onkeyup()事件里写个TextBox1和TextBox2之和的函数就是了。但现在的问题是我要在gridview模板列里去取这三个值,并且每一行都可以得到并计算。我试过这种方式去实现:

<asp:TemplateField HeaderText= "实发 ">
<ItemStyle HorizontalAlign= "Center " />
<ItemTemplate>
<asp:TextBox ID= "TextBox1 " runat= "server " BorderStyle= "None " Width= "55px " onkeyup= "getadd(this) "> </asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "应发 ">
<ItemStyle HorizontalAlign= "Center " />
<ItemTemplate>
<asp:TextBox ID= "TextBox2 " runat= "server " BorderStyle= "None " Width= "55px " onkeyup= "getadd(this) "> </asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "合计 ">
<ItemStyle HorizontalAlign= "Center " />
<ItemTemplate>
<asp:TextBox ID= "TextBox3 " runat= "server " BorderStyle= "None " Width= "55px " > </asp:TextBox>


</ItemTemplate>
</asp:TemplateField>


function getadd()
{
var t1=document.getElementById( "GridView1_ctl02_TextBox1 ").value-0;
var t2=document.getElementById( "GridView1_ctl02_TextBox2 ").value-0;
document.getElementById( "GridView1_ctl02_TextBox3 ").value=t1+t2;
//alert(t1+t2);
}

但这样的话只能够满足第一行,因为gridview1_ctl02_TextBox就变成死的了。我现在想让这个gridview每一行都可以如上题去计算而得到我想要的结果。希望各位高手能够帮助我。谢谢了... ...

[解决办法]
下面是我前段时间写的代码

分3步
1,把LinkButton 换成你需要的textbox
Private Sub SetJS()
Dim intx As Integer
Dim intcount As Integer 'GrideView
Dim linkBtnEdit As LinkButton
intcount = Me.grvDetail.Rows.Count
If intcount > 0 Then
For intx = 0 To intcount - 1
linkBtnEdit = CType(Me.grvDetail.Rows(intx).Cells(5).FindControl( "lbtnUpdate "), LinkButton)
linkBtnEdit.Attributes.Add( "onclick ", "return calbtnclick( ' " & Trim(Me.grvDetail.Rows(intx).Cells(0).Text) & " ', ' " & Trim(Me.grvDetail.Rows(intx).Cells(1).Text) & " ', ' " & Trim(Me.grvDetail.Rows(intx).Cells(2).Text) & " '); ")
Next
End If
End Sub

2,增加隐藏域hidRowinfo
javaScript

function calbtnclick(strId1,strId2,strId3)
{
document.getElementById( "hidRowinfo ").value=strId1 + "/ " + strId2 + "/ " + strId3 ;
}

3,
在邦定gridView时候调用Call SetJS()

读书人网 >C#

热点推荐