CheckBoxList qui conserve les valeurs
Bonjour à tous,
J'ai un gridView avec plusieurs lignes que je dois classer dans différentes catégories. Pour cela j'ai ajoué un bouton sur chaque ligne du gridview qui permet d'éditer la catégorie de la ligne. Lors d'un clic sur ce bouton une ModalPopUp s'affiche avec la CheckBoxList. Je choisis ensuite la catégorie que je veux attribuer à ma ligne en cochant une CheckBox puis je valide. Les données sont alors transmises en base. Ensuite je reviens au GridView ou je peux selectionner une seconde ligne. Pareil, en cliquant sur le bouton la ModalPopUp s'affiche, la CheckBoxList est décochée sur ma page, mais quand je fais le pas à pas de mon programme je m'aperçois que la catégorie de la ligne d'avant est restée, ce qui fait que lorsque je valide ma ligne est affectée à deux catégories.
Voici la question comment faire pour que la checkBoxList Soit réinitialisée avant la modification de la prochaine ligne ? J'utilise un UpdatePanel, pour l'actualisation que je mets a jour après chaque modif, et je fais un ChkBoxList.ClearSelection() mais malheureusement ça ne marche pas ! (J'ai aussi essayé de tout remettre à zéro avec une boucle foreach item in chkBoxList , item.selected = false).
Avez-vous d'autres idées ou y'a-t-il quelque chose que je fais mal ?
voici mon code :
C# :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| protected void ButtonPopUp_Click(object sender, EventArgs e)
{
Button ButtonPopUp = sender as Button;
GridViewRow row = (GridViewRow)ButtonPopUp.NamingContainer;
row.BackColor = System.Drawing.Color.Coral;
HiddenActionType.Value = "UPDATE";
hidCategoryEditIndex.Text = "";
hidCategoryEditIndex.Text = Scrub(row.Cells[1].Text);
HidProgramId.Text = "";
HidProgramId.Text = row.Cells[3].Text;
txtCategoryLabel.Text = "";
txtCategoryLabel.Text = Scrub(row.Cells[4].Text);
Session["Title"] = txtCategoryLabel;
TitleMovie.Text = "";
TitleMovie.Text = Scrub(row.Cells[4].Text);
ShopName.Text = "";
ShopName.Text = Scrub(row.Cells[5].Text);
LabelTab.Text = "";
LabelTab.Text = Scrub(row.Cells[1].Text);
var infos = VodBUS.GetSubCategories(false);
ChkBoxList.DataSource = infos;
ChkBoxList.DataBind();
mpeCategory.Show();
pnlCategory.Style["display"] = "block";
UpdatePanelMagique.Update();
UpdatePanel.Update();
}
/*Sauvegarde la selection et les mets dans une datatable avant de faire les modifications sur le serveur*/
protected void ButtonPopUpSave_Click(object sender, EventArgs e)
{
List<string> ValuesSelected = new List<string>();
foreach (ListItem item in ChkBoxList.Items)
{
if (item.Selected)
{
ValuesSelected.Add(item.Value);
item.Selected = false;
}
}
if (ValuesSelected.Count == 0)
{
string str = "<script>alert(\"Attention, Vous n'avez pas choisit de catégorie !\");</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", str, false);
}
//if (ValuesSelected.Count() == 1)
else
{
string nom = "";
nom = TitleMovie.Text;
string ProgramId = "";
ProgramId = HidProgramId.Text;
string NomBoutique = "";
NomBoutique = ShopName.Text;
string Label = "";
Label = LabelTab.Text;
DataTable dt = GetTable();
DataTable ds = new DataTable();
ds.Columns.Add("Label");
ds.Columns.Add("CategoryId");
ds.Columns.Add("ProgramId");
ds.Columns.Add("Title");
ds.Columns.Add("ShopName");
foreach (string ProgramIdChoose in ValuesSelected)
{
DataRow dr = dt.NewRow();
dr["ProgramId"] = ProgramId;
dr["CategoryId"] = ProgramIdChoose;
dr["Title"] = nom;
dr["ShopName"] = NomBoutique;
dr["Label"] = Label;
dt.Rows.Add(dr);
}
GridViewShops2.DataSource = dt;
GridViewShops2.DataBind();
foreach (GridViewRow Rows in GridViewShops2.Rows)
{
if (Rows.Cells[2].Text != "0")
{
Rows.BackColor = System.Drawing.Color.Aquamarine;
DataRow dr = ds.NewRow();
dr["Label"] = Rows.Cells[1].Text;
dr["CategoryId"] = Rows.Cells[2].Text;
dr["ProgramId"] = Rows.Cells[3].Text;
dr["Title"] = Rows.Cells[4].Text;
dr["ShopName"] = Rows.Cells[5].Text;
ds.Rows.Add(dr);
}
}
GridViewSend.DataSource = ds;
GridViewSend.DataBind();
foreach (GridViewRow Rows in GridViewSend.Rows)
{
Rows.BackColor = System.Drawing.Color.Lime;
}
}
ChkBoxList.ClearSelection();
UpdatePanelMagique.Update();
UpdatePanel.Update();
} |
L'aspx :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <asp:GridView ID="GridViewShops2" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="GridViewShops2_SelectedIndexChanged"
BorderColor="Gray" BorderStyle="Ridge">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="ButtonPopUp" runat="server" OnClick="ButtonPopUp_Click" CausesValidation="false"
Text="" CssClass="ButtonEdit" Width="70px" Height="22px"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Categorie Editeur" DataField="Label" />
<asp:BoundField HeaderText="CategoryId" DataField="CategoryId" />
<asp:BoundField HeaderText="ProgramId" DataField="ProgramId" />
<asp:BoundField HeaderText="Titre" DataField="Title" />
<asp:BoundField HeaderText="Nom Boutique" DataField="ShopName" />
</Columns>
</asp:GridView>
<%--<asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton>--%>
<%-------MODAL POPUP EDIT CATEGORY ------%>
<asp:Button ID="ButtonPopUp" runat="server" Style="display: none" />
<ajaxToolKit:ModalPopupExtender ID="mpeCategory" runat="server" TargetControlID="ButtonPopUp"
PopupControlID="pnlCategory" CancelControlID="btnCancelCategory" PopupDragHandleControlID="CategoryCaption"
Drag="true" BackgroundCssClass="BackGroundModal">
</ajaxToolKit:ModalPopupExtender>
<asp:Panel ID="pnlCategory" runat="server" Width="600px" BackColor="White" Height="600px"
CssClass="BackGroungModal">
<asp:UpdatePanel ID="UpdatePanelMagique" runat="server" UpdateMode="Conditional"
ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Panel ID="CategoryCaption" runat="server" Style="margin-bottom: 10px; cursor: hand;">
<div class="FontColor">
Changement de catégorie</div>
</asp:Panel>
<asp:HiddenField ID="HiddenActionType" runat="server" Value="" />
<asp:TextBox ID="hidCategoryEditIndex" runat="server" Visible="false"></asp:TextBox>
<asp:TextBox ID="HidProgramId" runat="server" Text="test" ReadOnly="true" Visible="false"></asp:TextBox>
<asp:TextBox ID="TitleMovie" runat="server" Visible="false"></asp:TextBox>
<asp:TextBox ID="ShopName" runat="server" Visible="false"></asp:TextBox>
<asp:TextBox ID="LabelTab" runat="server" Visible="false"></asp:TextBox>
<div class="divCol">
Nom
</div>
<div class="divColLast">
<asp:TextBox ID="txtCategoryLabel" runat="server" MaxLength="64" Width="250" CausesValidation="false"></asp:TextBox>
<asp:Button ID="ButtonIsAdult" runat="server" Text="IsAdult" OnClick="ButtonIsAdult_Click" />
<asp:Button ID="btnCancelCategory" runat="server" CausesValidation="false" Text="Annuler"
OnClick="btnCancelCategory_Click" />
<asp:Button ID="ButtonPopUpSave" runat="server" OnClick="ButtonPopUpSave_Click" Text="Valider" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:CheckBoxList ID="ChkBoxList" runat="server" DataTextField="Name" DataValueField="Id"
Width="250" Height="64" RepeatColumns="2" CssClass="FontColor" AutoPostBack="true">
</asp:CheckBoxList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button runat="server" ID="ClearSelection" Text="Clear" OnClick="ClearSelection_Click" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel> |
Merci d'avance pour vos réponses.
Julien