In post
#4 I posted the following:
...Notice how I used the <%= %> syntax. This is ASP shorthand for calling the Response.Write() method. This will write the ClientID into the place where it is needed. Anything within the <% %> tags is executed server side. That means that this has to be either VB or C# code (whichever you're using for your server side code which will send the page to the server and then send a new page back to the client)...
Did you not understand what I was trying to say here?
Do you agree that server control attributes and properties can me manipulated within a Javascript function? Also by using document.getElementById('<%= ServerControlID.ClientID %>');
you can access those controls?
You cannot manipulate Server Control attributes and properties with JavaScript.
I said this already (see post
#10). Dormilich is right, once the server code is finished it generates text (HTML is just text) that is sent to the browser. The browser then reads the HTML-text that was generated by the server and displays things accordingly. You cannot access server-side controls once the page has been sent to the browser because all server-side objects are destroyed once the page is sent.
JavaScript works in the browser. It cannot access anything on the server for 2 reasons...there is no persistent connection open between the browser and the server...and the server-side controls do not exist after the page has been sent to the browser.
My main problem are these lines for code....
In the following code:
-
var variable = "TotalSteps";
-
var element = "'" + '<' + '%' + '= ' + 'txt' + variable + '.ClientID' + ' %' + '>' + "'"
You are declaring a JavaScript variable that contains a String that contains "<%=txtTotalSteps.ClientID %>"
I think you were expecting this to call the server-side code to get the ClientID of the "txtTotalSteps" TextBox...But this is not going to accomplish anything because this is just a String stored into a variable in your JavaScript code. You cannot call server code from JavaScript (without making a request to the server)
In other words, what you have here doesn't make any sense.
I mentioned before that you can pass the HTML (client side) text box element into the function that calculates the average. From there you can use the .parentNode property of the HTML text box element to try and find the row that the text box belongs to.
In this case you don't even need to use the ClientID. You just pass a reference to the function like this:
C# code:
-
txt1.Attributes["OnBlur"] = "CalcAverage(this)";
-
Notice how I'm passing "this" to the CalcAverage method? "this" is JavaScript that refers to the HTML text box element.
You have to change your CalcAverage method so that it takes a parameter in order for you to be able do anything with the element passed into the CalcAverage method:
JavaScript code:
-
<script language="javascript" type="text/javascript">
-
function CalcAverage(textBoxElement) {
-
-
}
Now, from here you can do a lot of things to accomplish what you want.
You can try what I suggested with the .parentNode of the HTML text box element to try and find a reference to the row that the text box belongs to......
Or you could try retrieving a reference to the table that contains all of the text boxes, use the document.getElementsByTagName method to retrieve all of the rows in the table ("tr" elements)...and then use the document.getElementsByTagName method again grab all input elements... and then check if the row contains the text box element that caused the onblur event.
There are many ways to solve this problem.
Please re-read this thread (because I have said all of this already but I think you've ignored what I was saying...).
If you don't understand something that we are saying please either look it up using google or say that you don't understand and we'll try to explain.
-Frinny