How to pass Eval () parameter to Link Button Control?

ASP declaration:

<asp:LinkButton ID="LinkButton1" runat="server" Text="edit item" onclick="'AddItem.aspx?catid=<%# Eval("CollectionID")%>'"></asp:LinkButton> 

I get an error: Server tag is badly formed.

what's the problem with a LinkButton bite? Thank you in advance.

+4
source share
2 answers
 <asp:LinkButton ID="LinkButton1" runat="server" Text="edit item" onclick='AddItem.aspx?catid=<%# Eval("CollectionID")%>' /> 

I removed the extraneous quotes around the attribute value for OnClick .

However, OnClick expects a delegate, not a URL. Either use the hyperlink or switch to the event handler.

 <a href='AddItem.aspx?catid=<%# Eval("CollectionID")%>'>edit item</a> 

This article shows how to pass an argument to an event handler from a link button. Instead of using OnClick you can use OnCommand and set the CommandArgument property.

In the markup

  <asp:LinkButton id="lnkEdit" Text="Edit Item" CommandArgument='<%# Eval("CollectionID")%>' OnCommand="lnkEdit_Command" runat="server"/> 

In Codebehind

 protected void lnkEdit_Command( object sender, CommandEventArgs e ) { // evaluate e.CommandArgument and do something with it } 

I prefer to use the URL against the command's event handler whenever possible, as it eliminates the relatively expensive postback.

+7
source

You can pass the parameter this way.

 <asp:LinkButton ID="LinkButton1" runat="server" Text="edit item" PostBackUrl='AddItem.aspx?catid=<%#Eval("CollectionID")%>&catname=<%#Eval("CollectionName")%>' /> 
+2
source

Source: https://habr.com/ru/post/1393465/


All Articles