Select Gridview line with checkbox

Asked

Viewed 742 times

6

    function Selecionar(elemento,cordefundo)
        {
            var Inputs = elemento.getElementsByTagName("input");
            var cor = elemento.style.backgroundColor; //manter a cor default do elemento
            for(var i = 0; i < Inputs.length; ++i)
            {
                if(Inputs[i].type == 'checkbox')
                {
                    Inputs[i].checked = !Inputs[i].checked;
                    elemento.style.backgroundColor = cordefundo;
                    elemento.onclick = function()
                    {
                        Selecionar(this,cor);
                    };         
                }         
            }       
        }

protected void gvSelecao_PreRender(object sender, EventArgs e){
        GridView gv = (GridView)sender;
        foreach (GridViewRow row in gv.Rows)
            row.Attributes.Add("onclick", "Selecionar(this,'#FFFF00');");
        }

I took these two codes from a website to make you select gridview lines by clicking on them.

That part I didn’t understand

 elemento.onclick = function()
                {
                    Selecionar(this,cor);
                };         

Why does it call the function again? Actually I understand, it calls the function again because it is part of logic: it returns the default color of the line and unchecks ckbox.

But I do not understand, because for me when the function was called it was terminated, but when debugging I saw that when I click again on the line falls straight into that part of the function:

elemento.onclick = function () {
                        Selecionar(this, cor);
                    };

That I found a mystery, because as I said, for me the function was called and ended. I took a print of what appears when I click again on the line: inserir a descrição da imagem aqui

Can someone explain to me how this works? Why the function is not closed? If there were several functions, they would remain open?

  • Take a look at your stack trace, as you are with your active Debugger, you stopped running and after your select code was executed, the break point went to something in jquery. It has nothing to do with your onclick.

  • Could you please insert the HTML of your page? @Willian

  • Any results? Made progress on the problem?

1 answer

1

Willian,

it is not calling the function again. The stretch you are doubting means:

Should there be the event of click in the element, then call the Select(this, color) method again. The parameter this means the element that has been identified the on-screen click event. Its name is Callback and is widely used in Javascript.

This explains why when you click on the element again the first execution is the part of the function part;

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.