有没有办法将文本从列表视图控制器传递到OnClick子例程?例如,列表视图中有一个标签,我在列表视图中附加了一个按钮,并为其附加了一个OnClick子例程。现在我想将Label中的文本传递给OnClick子例程。我希望这是有道理的。下面是我的代码:
VB.Net代码:
Protected Sub AmButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim url As String = CType(Me.AmListViewDetails.FindControl("merchantLink"), Label).Text
Dim sb As New StringBuilder()
sb.Append("<script type = 'text/javascript'>"_
sb.Append("window.open('")
sb.Append(url)
sb.Append("');")
sb.Append("</script>")
ClientScript.RegisterStartupScript(Me.GetType(), "script", sb.ToString())
End Sub
ASP.Net:
<asp:ListView runtat="server" ID="AmListViewDetails" ...>
<ItemTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<asp:Label ID="merchantLink" runat="server" Text='<%Eval("Link")%>' />
</td>
</tr>
<tr runat="server">
<td runat="server">
<asp:Button ID="AmBtn" runat="server" Text="Checkout" OnClick="AmButtonClick"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
答案 0 :(得分:2)
向按钮添加CommandArgument
属性。这将是传递给事件处理程序的值。您不需要OnClick
属性。
<asp:Button ID="AmBtn" runat="server" Text="Checkout" CommandArgument="???" />
您的事件处理程序将是
Sub AmListViewDetails_ItemCommand(object sender, ListViewCommandEventArgs e)
Dim valuePassedFromListView = Cstr(e.CommandArgument)
End Sub