Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I m having a gridview, which contains some textBox in each row. I need o perform some calculation on this textbox value using JAVAScript..
I am doing this with usual way, but it gives me an error like .."This txt_box does not exist in the current context.."
so plz do suggest a proper solution for this problem:
Here is my code:
in GRID VIEW
C#
<ItemTemplate>
                                
                                <asp:TextBox ID="txt_Install" runat="server" Text='<%# Eval("total") %>'  ></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-Width="20%" HeaderText="Recieved Installment">
                            <ItemTemplate>
                            <asp:TextBox ID="txt_rcvInstl" runat="server" onKeyUp="DueAmt()"></asp:TextBox>
                                <%--<asp:Label ID="txt_InstallDate" Text='<%# Eval("Installmentdate") %>' runat="server"></asp:Label>--%>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-Width="20%" HeaderText="Due Amount">
                            <ItemTemplate>
                            <asp:TextBox ID="txt_dueAmt" runat="server"></asp:TextBox>
                               
                            </ItemTemplate>



in JavaScript :
XML
<script type="text/javascript">

    function DueAmt() {
        var install = document.getElementById('<%= txt_Install.ClientID %>').value;
        var RcvInstl = document.getElementById('<%= txt_rcvInstl.ClientID %>');
        var Due = document.getElementById('<%= txt_dueAmt.ClientID %>');

        if (install != "") t1 = install.value;
        if (RcvInstl != "") t2 = RcvInstl.value;
        Due.value = parseFloat(t1) - parseFloat(t2);


    }
</script>
Posted

 
Share this answer
 
v2
Just try this
C#
function DueAmt() {
    var install = document.getElementById('<%= GridView1_ctl02_txt_Install.ClientID %>').value;
    var RcvInstl = document.getElementById('<%= GridView1_ctl02_txt_rcvInstl.ClientID %>');
    var Due = document.getElementById('<%= GridView1_ctl02_txt_dueAmt.ClientID %>');

    if (install != "") t1 = install.value;
    if (RcvInstl != "") t2 = RcvInstl.value;
    Due.value = parseFloat(t1) - parseFloat(t2);
 
Share this answer
 
Comments
rahulDer 20-Mar-14 7:34am    
thank you ..but this code does not help me..it's for autocomplete textbox but I need to perform calculation with textbox value.
You are not able to get the text of asp.net textbox controls because when page is being rendered its id got change. You can see using View page source.
There are many ways to get the value of textbox. Easiest way as given below (I am not saying that it is a good practice but for time being you can do).
Modified:
<asp:textbox id="txt_Install" runat="server" name="mytextbox" onkeyup="DueAmt()" xmlns:asp="#unknown"></asp:textbox>


Sample of script code:
function DueAmt() {
            var install = document.getElementsByName("ctl00$MainContent$MainContent_txt_Install")[0].value;
            alert(install);         
        }

Here “ctl00$MainContent$MainContent_txt_Install” is taken from View page source.
 
Share this answer
 
v2
Comments
rahulDer 20-Mar-14 7:51am    
still not getting ..:(
Snesh Prajapati 20-Mar-14 7:57am    
I am saying when page is being rendered in the browser. Right click then you will get an options like View page source.
rahulDer 20-Mar-14 8:06am    
ya but problem is that, this code even does not allow me to debug my project and showing error like this :
Error 33 The name 'grd_InstallView_ctl02_txt_Install' does not exist in the current context E:\WorkingProjects\ERP_upd\Admin\RecieptMaster.aspx 22
Member 10313148 25-Mar-14 1:25am    
i have gried in that to textboxes. i want javascript for first textbox value should be displayed in second grid please help me its uregent

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