0

I have seen many resources on SO that say that I can use following syntax to pass value to CommandArguement of `LinkButton'

<%forearch(var comment in Comments){%>
<asp:LinkButton ID="del" CommandArguement='<%= comment.CommentId%>' onCommand="delete_click" Text="Delete"/>

<%}%>

But when I write this in my ascx file and click on the link the value passed to command argument is "<%=comment.CommentId%>" instead of commentId itself. Please guide what am I doing wrong?

Edit 1
based on answers and comments, I have moved to use repeater instead of foreach and plain code. Here is the code I have come up with

<asp:Repeater ID="commRepeater" SelectMethod="GetPageComments" runat="server">
        <ItemTemplate>
            <p>
                <%#Eval("Comment") %> 
                <%if(Page.User.Identity.IsAuthenticated && Page.User.Identity.GetUserId() == Eval("UserId")){ %>
                <span>
                    <asp:LinkButton  Text="Edit" runat="server" ID="EditLink" CommandArgument='<%#Eval("CommentId")%>' OnClick="Update_Comment" />&nbsp;&nbsp;
                    <asp:LinkButton  Text="Delete" runat="server" ID="DeleteLink" CommandArgument='<%#Eval("CommentId")%>' OnClientClick="if (!confirm('Are you sure you want delete?')) return false;" OnCommand="Delete_Comment"  />
                </span>
                <%} %>

            </p>
        </ItemTemplate>        </asp:Repeater>

you can see that I am trying to show the edit and delete links if user is logged in and his Id matches with user who commented but it tells me that I can on use Eval in databound controls. how would I hide/show edit/delete links conditionally within repeater

Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155

2 Answers2

0

You could simply use codebehind, for example in Page_Load:

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        del.CommandArgument = comment.CommentId;
    }
}

Maybe a better approach would be to use the Comments-collection(which seems to be a list or array of a custom class) as DataSource of a Repeater(or other web-databound control). Then you can add the LinkButtons to the Itemtemplate.

You can then either use ItemCreated or ItemDataBound events of the repeater in codebehind or inline ASP.NET tags to bind the CommandArgument.

For example:

CommandArguement='<%# DataBinder.Eval( Container.DataItem, "CommentId" ) %>'
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • there are multiple LinkButtons created in a loop how can i set command argument for each in code behind? – Muhammad Adeel Zahid Jan 08 '16 at 10:28
  • @MuhammadAdeelZahid: is that ASP.NET MVC? – Tim Schmelter Jan 08 '16 at 10:29
  • @MuhammadAdeelZahid: but `<%forearch` is not valid ASP.NET syntax. So how and where did you actually create these controls? You know that you also could use `Comments` as datasource for a repeater and add the `LinkButtons` to the `ItemTemplate`? – Tim Schmelter Jan 08 '16 at 10:30
  • can you point me to an example of repeater with `IEnumerable` plz? – Muhammad Adeel Zahid Jan 08 '16 at 10:37
  • @MuhammadAdeelZahid: just make the `Comments`(which seems to be a list or array of a custom class) the `DataSource` of the repeater and `DataBind` it. You can then either use `ItemCreated` or `ItemDataBound` events of the repeater in codebehind or inline ASP.NET tags to bind the `CommandArgument`. For example: `CommandArguement='<%# DataBinder.Eval( Container.DataItem, "CommentId" ) %>'` – Tim Schmelter Jan 08 '16 at 10:41
0

What you are doing currently is not recommended and is highly error prone. You can easily achieve this with ASP.NET Repeater control like this:-

<asp:Repeater ID="MyRepeater" runat="server">
   <ItemTemplate>
       <asp:LinkButton ID="del" CommandArguement='<%# Eval("CommentId") %>' 
                 OnCommand="del_Command" Text="Delete" runat="server" />
   </ItemTemplate>
</asp:Repeater>

In Page_Load simply bind it:-

 if (!Page.IsPostBack)
 {
      MyRepeater.DataSource = CommentsRepository();
      MyRepeater.DataBind();
 }

Or Else if you are have ASP.NET 4.5 then use strongly type Data Bound controls like this:-

<asp:Repeater ID="MyRepeater" runat="server" ItemType="MyNamespace.Comment" 
              SelectMethod="MyRepeater_GetData">
   <ItemTemplate>
       <asp:LinkButton ID="del" CommandArguement='<%# Item.CommentId %>'
                    OnCommand="del_Command" Text="Delete" runat="server" />
   </ItemTemplate>
</asp:Repeater>

And you method in code behind should be something like this(just for Demo):-

public IEnumerable<MyNamespace.Comment> MyRepeater_GetData()
{
   return new List<Comment>
   {
       new Comment { CommentId =1, Name= "foo"},
       new Comment { CommentId =2, Name= "bar"},
   };
}
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56