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

LVL 1
SaraDobAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Luis PérezSoftware Architect in .NetCommented:
How do you generate the text boxes?
Juan_BarreraCommented:
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;
     }    

Juan_BarreraCommented:
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;
     }    

OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

SaraDobAuthor Commented:
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

 
SaraDobAuthor Commented:
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

Juan_BarreraCommented:
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.
SaraDobAuthor Commented:
Sorry for late reply...
I had probably not explained it properly.It was for calculating the value of  (txtpricei*txtquantityi) of rows of textboxes.
i already sloved the problem. I used the below code to solve it.

Dim strCalcValue As String = "<script language='javascript'>"
strCalcValue += "function CalculateValue(txtQuantityID, txtPriceID, txtValueID,txtTotalForecastID)"
strCalcValue += "{"
strCalcValue += "var ForecastID=document.getElementById(txtTotalForecastID);"
strCalcValue += "var ThisForeCastvalue = parseInt(ForecastID.value);"
strCalcValue += "if (ThisForeCastvalue==0)"
strCalcValue += "{"
strCalcValue += "ThisForeCastvalue=0;"
strCalcValue += "}"
strCalcValue += "var txtQuanID = document.getElementById(txtQuantityID);"
strCalcValue += "var txtPriceID = document.getElementById(txtPriceID);"
strCalcValue += "var Quan = parseInt(txtQuanID.value);"
strCalcValue += "var price = parseInt(txtPriceID.value);"
strCalcValue += "var Tvalue = Quan*price;"
strCalcValue += "var txtTotalvalue = document.getElementById(txtValueID);"
strCalcValue += "txtTotalvalue.value=Tvalue;"
strCalcValue += "ForecastID.value=(ThisForeCastvalue+Tvalue);"
strCalcValue += "}"
strCalcvalue += "</" & "script>"
LitCalcValue.Text = strCalcValue

 
txtPrice1.Attributes.Add("onchange", "CalculateValue('" & txtQuan1.ClientID & "','" & txtPrice1.ClientID & "','" & txtValue1.ClientID & "','" & TxtNextMonthFore.ClientID & "');")
txtPrice2.Attributes.Add("onchange", "CalculateValue('" & txtQuan2.ClientID & "','" & txtPrice2.ClientID & "','" & TxtValue2.ClientID & "','" & TxtNextMonthFore.ClientID & "');")

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.