How can I change the value of a hidden field in a Gridview using Javascript?

Ok I need to change the value of a hidden field in the gridview and this 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!

+1


source to share


4 answers


I got it working. The above loop worked correctly, but apparently my sdi value was not always correct, so the value I was checking was always false. So the above worked great in my case in case anyone else has this problem.



+1


source


Edit: the classic case of checking before shipping. Apparently setting hidden = true actually prevents data binding, which is why JS has problems. I'd say this is one of those typical ASP.NET problems that makes me curse that name, but you can just pick a control with one of the many googleable solutions.

The comments about the use of element ID in ASP.NET were left untouched because it is evil.




Hard to know without code (when is this executed exactly?) Is there something else rewriting it? What is markup?), But I can tell right now that referencing an element using its deformed ASP.NET ID is a Bad Idea (TM).

If you can suggest you try and change this in the code where the link is processed for you, or at least write out the JS where you can use the clientID. Otherwise I'll try to find the element via the extended hunter element - your own or library will use the jquery function and other "$" functions, which will allow you to use a reliable CSS or Xpath argument.

0


source


Your first statement is only valid if the childNode is (truncated) lbl_SDI. Your second statement is only valid if the first statement is true (via sdiFound) AND the identifier (truncated) is lbl_Updated.

So, there is no way the second statement can always be true. The sdiFound variable will only be valid when the id node is not lbl_Updated.

I think a lot of people probably disagree that you are also referencing .NET ID. You thought you were using:

<%= lbl_SDI.ClientID %>

      

in your javascript?

0


source


Okay, can you tell me what's going on? it seems like you are forgetting to reset the sdiFound variable to false or abort the loop when you find it.

as far as your question is:

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

0


source







All Articles