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