ASP.NET Linkbutton not getting dynamic value for commandargument value

1.5k views Asked by At
    <%
      foreach (training t in traininglist)
       {
    %>                          
   <tr>
      <td><%=t.TrainingId%></td>
      <td><%=t.TrainingName%></td>
      <td>
         <asp:LinkButton runat="server" ID="EditBtn"
         Text="Edit" OnCommand="editbtn_OnCommand" 
         CommandArgument='<%# t.TrainingId %>' CommandName="edit" />
      </td>

    </tr>
 <% } %>

where, training is the class and traininglist is List<training> defined in Page_Load() function in codebehind.

I am trying to call the

public void editbtn_OnCommand(object sender, CommandEventArgs e)
{
   String myeid = e.CommandArgument.ToString();
   ....
}

Here, myeid is not getting value but always shows <%# t.TrainingId %>

i have already tried all other options like <%: t.TrainingId %> or <%=t.TrainingId %>

1

There are 1 answers

0
HaveNoDisplayName On

The output of Tag "<%= %>" is more likely similar to use Response.Write in your code. so these tags are used to display the value to the response object.

That's why,as per my understanding, You can't used these tags to set commandargument property of controls unless you are using DataBinding. If you are using DataBinding then these tags "<%= %>" are used to set the property of controls.

Because you are here looping through each item in list on html table, my suggestion is to use GridView or Repeater and then Bind through your List Object. if you are using this way, you can get rid of unwanted formatting issues of html tables also.

Refer http://msdn.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx

If you want to use repeater then you can use these specific tags, and this should be your code(not actual code, just sample one)

<asp:Repeater id="myRepeater" runat="server" >
  <ItemTemplate>
    <div>
      <asp:LinkButton runat="server" id="EditBtn" CommandName="edit" 
        CommandArgument="<%#Container.DataItem.TrainingId %>" Text="Edit" 
        OnCommand="editbtn_OnCommand" />      
    </div>
  </ItemTemplate>
</asp:Repeater>