Link to home
Start Free TrialLog in
Avatar of SaraDob
SaraDob

asked on

how to get Client Id of Dynamically created textboxes

Hi all,
I have a group of text boxes.  Quantity and  price.
But, the textboxes are dynamically generated.
Say: txtQuantity1  txtPrice1
        txtQuantity2  txtPrice2
When a user enters in txtPrice1, i'm calling a javascript function to calculate the total amount.(price1* quantity1).Thats Simple!!!
But how  i send and capture the dynamically created textbox names in javascript.
Here i'm using Master pages.
Please have a look at the code snippet and correct whatever i'm doing, or better, suggest a best way to do it.
Thanks
Appreciate any help.

Here is is .aspx code
 
<asp:TextBox ID="txtQuan1" runat="server" ></asp:TextBox>
<asp:TextBox ID="txtValue1" runat="server" Onkeypress ="return ClcAmount('document.FrmMonthlySalesReport.txtValue1','document.FrmMonthlySalesReport.txtQuan1')"></asp:TextBox>
 
Here is My javascript in the same page
 function CalcAmount(txtbox1,txtbox2)
     {
    
    var obj1 =document.getElementById('<%=txtbox1.ClientID%>').value;
//========I get an error here, It is recognising <%=txt1.clientid%> instead of <%=txtvalue1.clientid)
     var obj2 = document.getElementById('=txtbox2<%.ClientID%>').value;
     var obj3 = document.getElementById("TxtNextMonthFore.ClientID");
     var Amount = parseInt(obj1) * parseInt(obj2);
     obj3.value = Amount;
     }     
       </script>

Open in new window

Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

How do you generate the text boxes?
Hi there, if you are passing the textboxes as parameters, you should have to find them again:

function CalcAmount(txtbox1,txtbox2)
     {
     var obj1 =txtbox1.value;
     var obj2 =txtbox2.value;
     var obj3 = document.getElementById("TxtNextMonthFore.ClientID");
     var Amount = parseInt(obj1) * parseInt(obj2);
     obj3.value = Amount;
     }    

Just as a clarification:

If what you pass as "txtbox1" and "txtbox2" is the ID and not the control itself, then it would be:

function CalcAmount(txtbox1,txtbox2)
     {
   
    var obj1 =document.getElementById(txtbox1).value;
    var obj2 = document.getElementById(txtbox2).value;
     var obj3 = document.getElementById("TxtNextMonthFore.ClientID");
     var Amount = parseInt(obj1) * parseInt(obj2);
     obj3.value = Amount;
     }    

Avatar of SaraDob
SaraDob

ASKER

Thanks for looking into this :
how can I use these following when using master pages.Will not the masterpages change the Id of the textboxes?:
var obj1 =document.getElementById(txtbox1).value;
 var obj2 = document.getElementById(txtbox2).value;

or
var obj1 =txtbox1.value;
var obj2 =txtbox2.value;
I'm confused.Can you please correct me, if i'm wrong

 
Avatar of SaraDob

ASKER

I will reframe my Question: Attention experts..Please help me.

Thanks for those you have replied and given me an idea how to solve it.
But since i'm in the process of learning javascript and asp.net, i might need a little more explanation from your side.
To Make myself clear:

I have textboxes txtQuantity and txt Price, when User enters price, i need to calculate the amount=Price*quantity of these textboxes.Since i'm using master pages, i have use client id for each textboxes while passing vales ot retieving them in javascript, how do I do THAT?
These textboxes looks like this:
 

<asp:TextBox ID="txtValue1" runat="server" Onkeypress ="return CalcAmount('document.FrmMonthlySalesReport.txtValue1.value','document.FrmMonthlySalesReport.txtQuan1.value')"></asp:TextBox> 
<asp:TextBox ID="txtComments1" runat="server"></asp:TextBox> 
 
 
I have included the javascript function for calculation: 
 var obj1 =txtbox1.value;
     var obj2 = txtbox2.value;
     'var obj3 = document.getElementById("TxtNextMonthFore.ClientID");
     var Amount = parseInt(obj1) * parseInt(obj2);
     alert(Amount)
    'bj3.value = Amount

Open in new window

SaraDob,

Sorry, I'm not sure I completely understand what you are trying to do.
Have you already solved the problem? If not, if you want, try to post some more details.
ASKER CERTIFIED SOLUTION
Avatar of SaraDob
SaraDob

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial