OnItemCommand ne recupère pas de valeur
Bonjour,
J'utilise un gridview dans un repeater.
Dans ce gridview, j'ai également une dropdownlist.
Je voudrais que ma DDL contienne 3 items (fait/en cours/a faire), et que par défaut, il mette la valeur que je trouve dans ma requete sql qui rempli le gridview.
Par la suite, lorsque l'on change d'index, il faudrait enregistrer le changement dans ma bdd également, et pour cela je pensais utiliser un itemcommand
Le probleme : non seulement ma ddl ne prend pas la valeur de ma requete sql, et en plus de cela, l'itemcommand n'est pas appelé, sauf si je met un bouton à chacune des lignes (ce qui va rendre l'utilisation très contraignante...). Et lorsqu'il est appelé, le FindControl ne marche pas (impossible de récupérer ma DDL) :cry:
voici le code 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
| <asp:Repeater ID="RepeatFond" DataSourceID="sqlRepeater" runat="server" OnItemDataBound="RepeatFond_ItemDataBound" OnItemCommand="ddl_change">
<ItemTemplate>
<b>Fondamental : <%# Eval("libelle_fondamentaux")%></b><br />
<asp:GridView ID="GridViewFond" runat="server" AutoGenerateColumns="false" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="#5C5C5C" GridLines="Vertical" style="text-align:center">
<RowStyle BackColor="#F7F7DE" />
<Columns>
<asp:BoundField DataField="fondamentaux" HeaderText="Action" HeaderStyle-Width="700" />
<asp:BoundField DataField="idvalidation" HeaderText="Qui" />
<asp:TemplateField HeaderText="Avancement">
<ItemTemplate>
<asp:DropDownList ID="ddl_avancement" AutoPostBack="true" runat="server" DataTextField="fondamentaux" DataValueField="id_fondamentaux">
<asp:ListItem>Fait</asp:ListItem>
<asp:ListItem>En cours</asp:ListItem>
<asp:ListItem>A faire</asp:ListItem>
</asp:DropDownList>
<asp:Button CommandArgument='<%# DataBinder.Eval(Container.DataItem, "id_fondamentaux") %>' ID="Button1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
</ItemTemplate>
</asp:Repeater>
<asp:sqldatasource id="sqlRepeater" runat="server" /> |
puis le code cs :
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
| protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
repeaterBind();
}
}
private void repeaterBind()
{
sqlRepeater.ConnectionString = ConfigurationManager.ConnectionStrings["sqlServer"].ConnectionString;
string req = "Select [libelle_fondamentaux] as libelle_fondamentaux, [id_categorie_fondamentaux] as id_categorie_fondamentaux from [BSC_Categorie_Fondamentaux] Order By [id_categorie_fondamentaux]";
sqlRepeater.SelectCommand = req;
RepeatFond.DataSource = null;
RepeatFond.DataBind();
}
public void RepeatFond_ItemDataBound(object Sender, RepeaterItemEventArgs e)
{
string toto = "";
// Si il s'agit d'un élément Item ou AlternatingItem.
if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddl_avancement");
// Tu récupère la valeur sélectionnée (le texte ou la valeur associée)
// ddl.SelectedItem.Text
// ou ddl.SelectedItem.Value
if (ddl != null && ddl.SelectedItem != null)
toto = ddl.SelectedItem.Value;
// Récupération id de la categ.
int idFond = int.Parse(((DataRowView)e.Item.DataItem)["id_categorie_fondamentaux"].ToString());
// Stock le GridView1 dans une variable
GridView myGridView = (GridView)e.Item.FindControl("GridViewFond");
// Construction du GridView
myGridView.DataSource = dsListeFondamentaux(idFond);
myGridView.DataBind();
}
}
private DataSet dsListeFondamentaux(int idFond)
{
// Création d'une instance de connexion
SqlConnection oConnexion = new SqlConnection();
oConnexion.ConnectionString = ConfigurationManager.ConnectionStrings["sqlServer"].ConnectionString;
// Création d'un DataSet
DataSet myDataset = new DataSet();
SqlDataAdapter dtr = new SqlDataAdapter("Select [BSC_Fondamentaux].[id_fondamentaux] as id_fondamentaux, [BSC_Fondamentaux].[libelle_fondamentaux] as fondamentaux, [BSC_Type_Validation].[libelle_validation] as validation, [BSC_Type_Validation].[id_type_validation] as idvalidation from [BSC_Fondamentaux], [BSC_Etat_Fondamentaux], [BSC_Type_Validation], [BSC_Categorie_Fondamentaux] Where [BSC_Etat_Fondamentaux].[id_type_validation]=[BSC_Type_Validation].[id_type_validation] And [BSC_Etat_Fondamentaux].[id_fondamentaux]=[BSC_Fondamentaux].[id_fondamentaux] And [BSC_Fondamentaux].[id_categorie_fondamentaux]=[BSC_Categorie_Fondamentaux].[id_categorie_fondamentaux] And [BSC_Categorie_Fondamentaux].[id_categorie_fondamentaux] = '" + idFond + "' Order By [BSC_Fondamentaux].[id_fondamentaux]", oConnexion);
dtr.Fill(myDataset);
// Retourne le DataSet
return myDataset;
}
public void ddl_change(object source, RepeaterCommandEventArgs e)
{
string status;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddl_avancement");
// Tu récupère la valeur sélectionnée (le texte ou la valeur associée)
if (ddl != null && ddl.SelectedItem != null)
status = ddl.SelectedItem.Text;
// Faire des traitements avec la variable récupérée
}
foreach (RepeaterItem Item in RepeatFond.Items)
{
if (((DropDownList)Item.FindControl("ddl_avancement")).Text != "")
{
// traitement
}
}
} |
Si quelqu'un a une idée, je suis preneur ! :ccool: