DataRepeater OnCommand multiple CommandArgument
Is it possible to pass several arguments inside a CommandArgument
parameter to the ItemCommand
handler for the ASP.NET Repeater?
I have a table that I render with several columns, pretty basic, I just need one linkbutton
, when clicked on it, I need to pass 3 parameters to the onclick
function, all three are the data bound parameters. There doesn't seem to be a way of doing so, aside from using CSV format(which will not work for me, since the values themselves can contain commas).
So, is this possible to pass p_field1
, p_field2
and p_field3
as per in the code below.
Thanks!
<asp:Repeater ID="rptId" runat="server" EnableViewState="true" OnItemCommand="rptId_ItemCommand">
<ItemTemplate>
<tr align="center" class='<asp:Literal ID="litRowClass" runat="server" />'>
<td><%#Eval("p_开发者_Go百科field1")%></td>
<td><%# Eval("p_field2")%></td>
<td><%# Eval("p_field3")%></td>
<td><%# Eval("p_field4")%></td>
<td><asp:LinkButton runat="server" ID="lnkBtnId" CommandArgument='<%# Eval("p_field1") %>' CommandName="testCommandName">MARK</asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:Repeater>
Unfortunately, you can't pass in multiple fields without a "CSV" style format; only a single string is allowed.
You'll have to do some string concatenation and then split it back out OnCommand. Pick some delimiter that won't be in your parameters, below I used |
.
<asp:Repeater ID="rptId" runat="server" EnableViewState="true" OnItemCommand="rptId_ItemCommand">
<ItemTemplate>
<tr align="center" class='<asp:Literal ID="litRowClass" runat="server" />'>
<td><%#Eval("p_field1")%></td>
<td><%# Eval("p_field2")%></td>
<td><%# Eval("p_field3")%></td>
<td><%# Eval("p_field4")%></td>
<td><asp:LinkButton runat="server" ID="lnkBtnId" CommandArgument='<%# Eval("p_field1") + "|" + Eval("p_field2") + "|" + Eval("p_field3") %>' CommandName="testCommandName">MARK</asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:Repeater>
You'd then need something like the following in rptId_ItemCommand
:
string[] arguments = CommandEventArgs.CommandArgument.Split('|');
CommandArgument='<%# JoinFields(Eval("p_field1"), Eval("p_field2"), Eval("p_field3")) %>'
You could use what you want as separator(f.e. the pipe |). You could easily split these values on serverside. To join these field you could use a helper-function like this:
Protected Function JoinFields(ByVal p_field1 As Object, ByVal p_field2 As Object, ByVal p_field3 As Object) As String
Return String.Join("|", New Object() {p_field1, p_field2, p_field3})
End Function
精彩评论