Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Passing ClientID to Javascript from asp.net textbox

Posted on 2007-10-02
3
Medium Priority
?
21,473 Views
Last Modified: 2013-11-07
I have developed a site that uses both static ASP.net Textboxes and Dynamically created textboxes to hold dollar amounts.  I have also created a javascript that allows the user to type in " 1+2+3..." and onBlur will add the totals together.  Where i am running into an issue is I am not sure how to use the same function for all the textboxes.  I understand that the client ID will tell the javascript where to look, but how to I pass that client ID from the asp.net side in the onBlur? I know I will have to pass an argument un the javascript function, but have had trouble figuring this out. Any help is appreciated.

Javascript:
function solve() {
       
         with (Math) {
              answer = eval(document.getElementById("<%=txtCandy.ClientID%>").value);
              document.getElementById("<%=txtCandy.ClientID%>").value = answer;
         }
       
}

ASP textbox:
<td><asp:TextBox
          Width = "90px"
          ID="txtCandy"
          runat="server"
          ToolTip="Candy Total for the Day"
           />
       
    </td>

Attributes added in the Page_load section:
        txtCandy.Attributes.Add("onBlur", "solve(this);return false;")
0
Comment
Question by:JaCrews
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
dakyd earned 2000 total points
ID: 20001949
Well, it looks like you're already passing "this" to the javascript function, so you already have what you need.  The "this" keyword will pass a reference to the object from which the function was called, so in a sense, your javascript function is already dynamic.  Give this a shot:

Javascript:
function solve(obj) {
         with (Math) {
              answer = eval(obj.value);
              obj.value = answer;
         }
       
}

Attributes added in the Page_load section:
        txtCandy.Attributes.Add("onBlur", "solve(this);return false;")

You'd just add the same onBlur for every other textbox that you want to exhibit the same behavior.  Hope that helps.
0
 

Author Comment

by:JaCrews
ID: 20002070
I am so angry right now! thank you so much, it's always the little things that get me.
0
 
LVL 19

Expert Comment

by:dakyd
ID: 20002213
Heh, we've all done it.  At least you got it working now.  Cheers.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question