views:

886

answers:

1

Using Aspnet, C# 3.5 in vs2008 :

I have used sucessfully the following code in a gridview or listview templated column:

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

With code behind to get an identifier for the row in which the button was clicked. :

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

Now I am trying to dynamically (conditionally) put that button in the column.

I tried doing this using a placeholder with the following code behind:

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); 
}

The button is generated but the command argument is not evaluated, the string "(<%# Eval('PKey') %>" is passed as the command argument.

How can I do what I am trying to do?

A: 

Using RowCommand instead of ItemCommand, this worked:

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); 
  }
Lill Lansey