Bonjour,
J'ai un datagrid dont je rajoute dynamiquement des TemplateColumn et dans lesquelles je positionne des textbox dans le Footer et à la fin je rajoute une colonne statique avec un bouton Add.
Malheureusement, je n'arrive pas à récupérer les valeurs écrites manuellement dans ces textbox dans la fonction ItemCommand qui est automatiquement appellée.
Voilà le code:
Le page_load():
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <asp:DataGrid ID="DataGridReference" runat="server" AllowSorting="True" AllowPaging="True" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" PageSize="20" oncancelcommand="DataGridReference_CancelCommand" ondeletecommand="DataGridReference_DeleteCommand" oneditcommand="DataGridReference_EditCommand" onsortcommand="DataGridReference_SortCommand" OnItemCommand="DataGridReference_ItemCommand" ShowFooter="True" onupdatecommand="DataGridReference_UpdateCommand" CellPadding="4"> ... <Columns> ... <asp:EditCommandColumn ButtonType="PushButton" UpdateText="Sauver" CancelText="Annuler" EditText="Editer" HeaderText=""> </asp:EditCommandColumn> <asp:TemplateColumn HeaderText=""> <FooterTemplate> <asp:Button CommandName="Insert" Text="Ajouter" ID="add_Button" Runat="Server" /> </FooterTemplate> <ItemTemplate> <asp:Button CommandName="Delete" Text="Supprimer" ID="del_Button" Runat="Server" /> </ItemTemplate> </asp:TemplateColumn> </Columns> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> </asp:DataGrid>
Le dans DataBind_DataGridReference() voici le code pour rajouter mes TemplateColumns
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataBind_DataGridReference(); } }
Voilà ensuite le code de TemplateColumn:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 ... // In a loop for TemplateColumn creation TemplateColumn packagingTemplateColumn = new TemplateColumn(); packagingTemplateColumn.HeaderText = p.Name; ITemplate viewItemTemplate = new DataGridTemplate(ListItemType.Item, p.Name, "view_PackagingPrice_" + p.Id, this.DataGridReference.DataSource as IList<Reference>); packagingTemplateColumn.ItemTemplate = viewItemTemplate; ITemplate editItemTemplate = new DataGridTemplate(ListItemType.EditItem, p.Name, "edit_PackagingPrice_" + p.Id, this.DataGridReference.DataSource as IList<Reference>); packagingTemplateColumn.EditItemTemplate = editItemTemplate; ITemplate addItemTemplate = new DataGridTemplate(ListItemType.Footer, p.Name, "add_PackagingPrice_" + p.Id, null); packagingTemplateColumn.FooterTemplate = addItemTemplate; this.DataGridReference.Columns.AddAt(this.DataGridReference.Columns.Count - 2, packagingTemplateColumn); ...
Et pour finir là où je ne vois pas ce que j'ai rentré dans mes textbox
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class DataGridTemplate : ITemplate { ListItemType templateType; string columnName; IList<Reference> dtBind; string controlId; public DataGridTemplate(ListItemType type, string colname, string controlId, IList<Reference> source) { templateType = type; columnName = colname; dtBind = source; this.controlId = controlId; } public void InstantiateIn(System.Web.UI.Control container) { switch (templateType) { case ListItemType.Header: Literal lc = new Literal(); lc.Text = "<B>" + columnName + "</B>"; container.Controls.Add(lc); break; case ListItemType.Item: Label l = new Label(); l.ID = controlId; l.DataBinding += new EventHandler(viewBind); container.Controls.Add(l); break; case ListItemType.EditItem: TextBox tb = new TextBox(); tb.ID = controlId; tb.DataBinding += new EventHandler(editBind); container.Controls.Add(tb); break; case ListItemType.Footer: TextBox tb2 = new TextBox(); tb2.ID = controlId; tb2.DataBinding += new EventHandler(addBind); tb2.Text = ""; container.Controls.Add(tb2); break; } } public void editBind(object sender, System.EventArgs e) { TextBox tb = sender as TextBox; DataGridItem container = tb.NamingContainer as DataGridItem; string packagingId = controlId.Substring(controlId.LastIndexOf("_") + 1); PackagingPrice pp = (container.DataItem as Reference).getPackagingPrice(new Guid(packagingId)); tb.Text = (pp != null) ? ("" + pp.Price) : ""; } public void addBind(object sender, System.EventArgs e) { TextBox tb2 = sender as TextBox; tb2.Text = ""; } public void viewBind(object sender, System.EventArgs e) { Label l = sender as Label; DataGridItem container = l.NamingContainer as DataGridItem; string packagingId = controlId.Substring(controlId.LastIndexOf("_") + 1); PackagingPrice pp = (container.DataItem as Reference).getPackagingPrice(new Guid(packagingId)); l.Text = (pp != null) ? ("" + pp.Price) : ""; } }
à ce stade, je rentre dans le if, mais je ne récupère aucune valeur des textbox du footer.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 protected void DataGridReference_ItemCommand(object source, DataGridCommandEventArgs e) { string test = ""; test += ""; if (e.CommandName == "Insert") { ...
Il me manque une étape (transmission des valeurs) mais je ne sais pas où ni comment le placer dans le code.
Votre aide me serait très précieuse car je galère dessus depuis ce matin.
Pourriez vous m'aider, svp ?
Merci !
Partager