javascript Onclick function is not working in Contzent page

I have a javascript Onclick function.It works fine when not set in content page.But now when I set this in  a content page ,when I click the textbox I get the error:
Microsoft javascript runtime error:Object required.
The code in my content page ( .aspx.cs) is
 tb.Attributes.Add("onclick", "Update();");

the code of the content page (.aspx)is
<script type="text/javascript">
    function Update {
        var tblCols = "<%=aa %>";
           for (j = 0; j < tblCols - 1; j++) {
            var tot1 = 0;
            for (i = (j + (aa - 1) + 1); i < ((7 * (aa - 1)) + 1); i = i + 10) {
                var sum = document.getElementById("textbox" + i).value;
                var tot1 = parseInt(sum) + parseInt(tot1);
            }
            document.getElementById("textbox" + (j + 1)).value = tot1;
        }
</script>

Open in new window


I get the error pointing to this line
var sum = document.getElementById("textbox" + i).value;

How can make this Onclick function work in the content page?

asudhaaAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
You could use ClientID if you were dealing with a single control. But your loop suggests that you are dealing with a series of textboxes instead, so that won't really work.

If you are only calling your Update method in a single context then you should just modify your javascript function to use the prefixed control id.

i.e.
<script type="text/javascript">
    function Update {
        var tblCols = "<%=aa %>";
           for (j = 0; j < tblCols - 1; j++) {
            var tot1 = 0;
            for (i = (j + (aa - 1) + 1); i < ((7 * (aa - 1)) + 1); i = i + 10) {
                var sum = document.getElementById("ctl00_mainContent_textbox" + i).value;
                var tot1 = parseInt(sum) + parseInt(tot1);
            }
            document.getElementById("textbox" + (j + 1)).value = tot1;
        }
</script>

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
The most likely cause of your problem it that your textbox controls names will be getting altered during code generation because of their nesting level. Start by running your project, viewing the source for the page and taking a look at what the IDs for your textboxes actually are.
0
 
khan_webguruCommented:
Use like this

 
MasterPageBodyTag = (HtmlGenericControl)Page.Master.FindControl("MainBody"); 
            MasterPageBodyTag.Attributes.Add("Onload", "Update()");

Open in new window


reference: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_22103475.html

Or this

 
Page.ClientScript.RegisterStartupScript(this.GetType(),
            "update", "setTimeout('Update()',3000)", true);

Open in new window


Why settimeout

Because when you will debug this code that will register script onload and try to fire the event before complete page render so I just assuming that your page will take max 3 sec to render so I set 3000 in milisec you can change accordingly.  

otherwise just copy paste the code and it will work without any error.

Thanks,
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Carl TawnSystems and Integration DeveloperCommented:
@khan_webguru

If you actually read the original question properly you'll notice that te function is being triggered from a "click" event. So how exactly is dumping the code into the onload even remotely relevant !?!?
0
 
khan_webguruCommented:
In your case you are binding the click event so you can avoid the error by just replacing this piece of code.


 
<script type="text/javascript">
    function Update {
        var tblCols = "<%=aa %>";
           for (j = 0; j < tblCols - 1; j++) {
            var tot1 = 0;
            var sum = 0;
            for (i = (j + (aa - 1) + 1); i < ((7 * (aa - 1)) + 1); i = i + 10) {
                if(document.getElementById("textbox" + i))
                {
                    sum = document.getElementById("textbox" + i).value;                
                    tot1 = parseInt(sum) + parseInt(tot1);
                }
            }
            if(document.getElementById("textbox" + (j + 1)))
            {
            document.getElementById("textbox" + (j + 1)).value = tot1;
            }
        }
</script>

Open in new window


Just checking if you are getting wrong ID and object is not declare so avoid errors.

Thanks,
0
 
asudhaaAuthor Commented:
As you said the textbox Id has been altered from
textbox
to
ctl00_mainContent_textbox

Can I change in my program from textbox to ctl00_mainContent_textbox?

If I do so it is working.Is that correct to change the textbox ID or what should i do to get the correct textbox ID again after the onclick function?
0
 
khan_webguruCommented:
Post table HTML too so that we can look into this I think you want to display sum in last column like below

val1    val2   val3   val4   sum

    2       4        5       6      17

Thanks,
0
 
khan_webguruCommented:
In above scnario you can do this easily by JQuery like this

 
var total = 0;

$('#Grid > tr').each( function() {
    $(this).find('input:not(:last)).each( function() {
       total = total + this.val();
    });
    $(this).find('input:last).val(total);
});

Open in new window


#Grid would be your table ID and this code will just add each column <input tags values instead of last column that would be a sum column and display sum of each row.

To run this code you have to just add JQuery class in your head section  

That is the File: http://code.jquery.com/jquery-1.5.2.js

ref: http://stackoverflow.com/questions/638206/how-do-i-select-all-text-boxes-in-a-table-excluding-the-last-one-in-the-table-row

hope that concept might help you.

Thanks,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.