How to know what is the checkbox that launched the OnCheckedChanged event in asp.net?

1

I hope someone can help me with the following. I show user information in a table, and also in the row of each user there is a checkbox that says If the user is blocked or not, I want that when the checkbox of a user change state update their Disabled property in the database. The view would be like this:

<asp:GridView runat="server" ID="ListaDeUsuarios"
                ShowFooter="True" GridLines="Vertical" CellPadding="4"
                CssClass="table table-striped table-bordered"
                ItemType="Proyecto.Models.Usuario" DataKeyNames="Id" 
                SelectMethod="GetUsuarios"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="Bloqueado">
                      <ItemTemplate>
                          <asp:CheckBox Checked=<%# Item.Disabled %> runat="server" 
                                        AutoPostBack="True"
                                        OnCheckedChanged="Check_Clicked"></asp:CheckBox>
                      </ItemTemplate>
                    </asp:TemplateField> 
                </Columns>
            </asp:GridView>

My question is what to put in the method

 public void Check_Clicked(object sender, EventArgs e) 
    {

    }

let me know which is the checkbox that launched the event and and which user do you represent?

    
asked by Mr.K 08.08.2017 в 02:05
source

2 answers

2

In general, the sender parameter corresponds to the control that launched the event, so in this case:

public void Check_Clicked(object sender, EventArgs e) 
{
    var checkbox = (CheckBox)sender;

    // ...
}

checkbox contains the correct checkbox

    
answered by 08.08.2017 в 02:22
1

In this way I resolved:

in the definition of checkboxes add a username property

<asp:CheckBox Checked=<%# Item.Disabled %> runat="server" 
                                        AutoPostBack="True" username = "<%# Item.UserName %>"
                                        OnCheckedChanged="Check_Clicked"></asp:CheckBox>

and defined the event like this

 public void Check_Clicked(object sender, EventArgs e) 
    {
        if (sender != null)
        {
            CheckBox checkbox = (CheckBox)sender;
            string username = checkbox.Attributes["username"];

            UserManager manager = new UserManager();
            Usuario usuario = manager.FindByName(username);

            usuario.Disabled = checkbox.Checked;
            manager.Update(usuario);
        }
    }

Thanks for the help.

    
answered by 08.08.2017 в 02:59