2009-09-11 5 views
0

En utilisant Aspnet, C# 3.5 VS2008:Définir un CommandArgument dans un gridview ou listview de bouton créé dynamiquement

Je sucessfully utilisé le code suivant dans un gridview ou listview colonne templated:

<asp:Button ID="btnShow" runat="server" Text="?" 
    TabIndex="-1" CommandName="ShowDefinition" 
    CommandArgument='<%# Eval("PKey") %>' /> 

Avec le code derrière pour obtenir un identifiant pour la ligne dans laquelle le bouton a été cliqué. :

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e) 
{ 
    if (e.CommandName == "ShowDefinition") 
     { 
     int showRecordPKey = Convert.ToInt32(e.CommandArgument); 
     } 
} 

Maintenant j'essaie de mettre dynamiquement (conditionnellement) ce bouton dans la colonne.

J'ai essayé de faire cela en utilisant un espace réservé avec le code suivant derrière:

public class BtnShow 
{ 
    public Button btnShow = new Button(); 
    PlaceHolder ph = new PlaceHolder(); 
    public BtnShow(string commandName,string displayText, PlaceHolder ph) 
    { 
     btnShow.ID = "btnShow"; 
     btnShow.Text = "?"; 
     btnShow.TabIndex = -1 ; 
     btnShow.CommandName = "ShowDefinition"; 
     btnShow.CommandArgument = "(<%# Eval('PKey') %>" ; 
     this.ph = ph; 
    } 
public void AddQuMarkToPlaceholder() 
{ 
    ph.Controls.Add(btnShow); 
} 

Le bouton est généré, mais l'argument de commande n'est pas évaluée, la chaîne « (<% # Eval ('PKey') %> » est passé comme argument de commande

Comment puis-je faire ce que je suis en train de faire

Répondre

1

en utilisant RowCommand au lieu de ItemCommand, cela a fonctionné.?

protected void ListView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     GridViewRow row = (GridViewRow) 
       (((Button) e.CommandSource).NamingContainer); 
     Label pk = (Label) row.FindControl("lblPKey"); 
     int showRecordPKey = Convert.ToInt32(pk.Text); 
    } 
Questions connexes