mardi 4 juillet 2017

Recover current value in checkBox during gridView Editing - WebForm C#

I have a simple checkBox in Editable gridView :

<asp:TemplateField HeaderText="Editable">
  <ItemTemplate>
      <asp:Label runat="server" Text="<%# Item.IsEditable %>" />
  </ItemTemplate>
  <EditItemTemplate>
      <asp:CheckBox ID="CheckBoxEditable " runat="server" Text="Editable"></asp:CheckBox>
  </EditItemTemplate>
</asp:TemplateField>

When I click on edit button in the row, I would like that checkBox is already checked if value is true. (IsEditable is a boolean) Text Field is easy because I have a BindItem on Text property in EditItemTemplate. But it's not the same for checkBox or dropdownlist

GridView

I use a UpdateItem Method to update my data in database. I tried a small condition to check my checkBox but it does'nt work.

public void GridViewRisquesAggravants_UpdateItem(IndexViewModel item)
{
 try
   {
    if (ModelState.IsValid)
      {
       CheckBox chbEdit = (CheckBox)GridView.Rows[this.GridView.EditIndex].FindControl("CheckBoxEditable") 
          if (item.IsEditable)
              chbEdit.Checked = true;

        new TypeService().Update(new Type
        {   
          IsEditable = item.IsEditable, 
        });
        this.GridView.DataBind();
      }
    }
    catch
    {
      throw;
    }
}

It makes sense because I am not in the right function to declare this. But I just have 3 methods in my webform.

SelectMethod="GridView_GetData"
UpdateMethod="GridView_UpdateItem"
DeleteMethod="GridView_DeleteItem"

Where can I do this? (And I have the same problem with datas on dropdownList. I don't know where I recover current value during editing)

Thanks in advance (Sorry I am beginner about webforms and my english is not perfect)

Evy




Aucun commentaire:

Enregistrer un commentaire