I have been designing a code for a project, where I want to get the “ProductId” on OnClick="LinkButton1_Click" event.
Here is the code:
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4">
<ItemTemplate>
<table>
<tr><td><%#Eval("ProductId")%>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">
<img src='<%#Eval("Image")%>' style="width: 220px; height: 146px" alt="" />
</asp:LinkButton></td>
</tr>
<tr>
<td style="text-align: center">
<%#Eval("ArtName")%>
</td>
</tr>
<tr>
<td style="text-align: center">
<%#Eval("Price")%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Any suggestion please.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You have to use the itemcommand pattern.
An example at :
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemcommand%28v=vs.110%29.aspx
Basically, you can :
- dismiss the OnClick on your LinkButton
- set a CommandName (for example “LBClick”) and CommandArgument (your value) on your button
- subscribe to the DataLIst ItemCommand event
- handle your button click in the ItemCommand handler (if e.CommandName == “LBClick”, run code with e.CommandArgument as value)
Hope this will help
Method 2
You can use OnItemCommand event on your DataList
And try with CommandArgument and CommandName
void Item_Command(Object sender, DataListCommandEventArgs e)
{
if(e.CommandName ="Select")
{
var e = e.CommandArgument;
....
}
}
View :
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ProductId")%>' />
Note : Add OnItemCommand on your datalist
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0