views:

1521

answers:

4

Ok I need to change the value of a hidden field in a gridview and here is what I have so far:

for(var i = 0; i < gv_Proofs.rows.length; i++)
{
    var tbl_Cell = gv_Proofs.rows[i].cells[0];
    var sdiFound = false;

    for(var x = 0; x < tbl_Cell.childNodes.length; x++)
    {
        if(tbl_Cell.childNodes[x].id == "_ctl0_MasterContentPlaceHolder_gv_Proofs__ctl2_lbl_SDI")
        {
            if(tbl_Cell.childNodes[x].innerHTML == sdi)
                sdiFound = true;
        }
        if(tbl_Cell.childNodes[x].id == "_ctl0_MasterContentPlaceHolder_gv_Proofs__ctl2_lbl_Updated" && sdiFound)
            tbl_Cell.childNodes[x].value = "true";
    }
}

can anyone tell me what I am doing wrong? Thank You!

A: 

Edit: classic case of check before you post. Apparently setting hidden=true actually prevents databinding which will be why the JS is having issues. I would say this is one of those typical problems with ASP.NET which make me curse it's very name, but you might just choose to adapt the control with one of the many googleable solutions.

Comments about using element ID in ASP.NET left intact because it's evil.


Hard to know without more code (when is this executing exactly? is something else overwriting it? what does the markup look like?) but I can say right now that referencing an element by it's ASP.NET deformed ID is a Bad Idea(TM).

If you can I suggest you try and modify this in the codebehind where the reference is handled for you, or at least write out the JS there where you can use clientID. Failing that I would try and find the element through an expanded element hunter - your own or a library util like jquery et al's "$" function - which will allow you to use a robust CSS or Xpath argument instead.

annakata
A: 

Your first statement is only true if the childNode id is (truncated) lbl_SDI. Your second statement is only true if the first statement is true (through sdiFound) AND the ID is (truncated) lbl_Updated.

So there's no way for the second statement to ever be true. The variable sdiFound will only be true when the node id isn't lbl_Updated.

I think a lot of people probably disagree with you referencing the .NET ID specifically as well. Have you considered using:

<%= lbl_SDI.ClientID %>

in your javascript?

Mike Robinson
A: 

well, can you tell what is happening? it seems like you are forgetting to reset the sdiFound variable to false, or breaking off the loop when you do find it.

as far as your question:

if you run the hidden html input as a server control, then it will be displayed by the GridView and it will have a unique id. you could then find it by using getElementById. You can put this inside a loop that builds the right id for you and you should be able to find your control.

Victor
+1  A: 

I got it working. The above loop was working just right but apparently my value of sdi was not always getting set right, and therefore the value I was checking was always set to false. So the above worked perfectly in my case if anyone ever has this issue again.

jmein