我有一个中继器,该中继器具有按照以下代码编写的表格,单击表格行时,我需要向后面的代码发送参数。 目前,我在LinkButton1上单击了它。 我也尝试过在LinkButton中包装,但无法正常工作 我该怎么做呢?
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound" OnItemCreated="Repeater1_ItemCreated">
<HeaderTemplate>
<table class="table table-striped table-bordered dttable dataTable no-footer" style="border-style: solid; border-color: rgb(252, 253, 250); text-align: center; font-weight: 700; width: 100%;" id="JsDataTable" role="grid" aria-describedby="JsDataTable_info">
<thead style="width: 100%">
<tr style="color: #fff; width: 100%;">
<td id="tdSr">Sr</td>
<td id="tdCode" style="white-space: nowrap;">
<asp:LinkButton ID="lnCode" runat="server" CommandName="CourseNo" CssClass="linkHeader">Code<i class = "fa fa-fw fa-sort" ></i></asp:LinkButton>
</td>
<td id="tdName" style="width: 50%; white-space: nowrap; text-align: left;">
<asp:LinkButton ID="lnName" runat="server" CommandName="VName" CssClass="linkHeader">Name<i class = "fa fa-fw fa-sort" ></i></asp:LinkButton>
</td>
<td id="tdType" style="width: 20%">
<asp:LinkButton ID="lnType" runat="server" CommandName="VType" CssClass="linkHeader">Type<i class = "fa fa-fw fa-sort" ></i></asp:LinkButton>
</td>
</tr>
</thead>
<tbody style="width: 100%">
</HeaderTemplate>
<ItemTemplate>
<tr id="trID" runat="server" class="box" style="height: 30px; color: #08516F; vertical-align: middle;">
<td style="color: #004457; border-right: 1px solid #004457; vertical-align: middle;" class="labelTxt">
<asp:HiddenField ID="hfAllowSubscription" runat="server" Value='<%#Eval("AllowSubscription") %>' />
<asp:Label ID="lblSR" runat="server"></asp:Label>
</td>
<td style="color: #004457; border-right: 1px solid #004457; vertical-align: middle;">
<%#Eval("CourseNo") %>
</td>
<td style="color: #004457; border-right: 1px solid #004457; text-align: left !important; vertical-align: middle;">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="PlayPrev" CommandArgument='<%#Eval("Id") %>' Text='<%#Eval("Name") %>' CssClass="name"></asp:LinkButton>
</td>
<td style="color: #004457; border-right: 1px solid #004457; vertical-align: middle;">
<asp:Label Text='<%#Eval("VType") %>' runat="server" ID="lblType" CssClass="lblType" Style="font-family: 'Segoe UI semiBold' !important;"></asp:Label>
<span id="lblsubtype" runat="server" class="subtype_color T"></span>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
<div id="repeatorEmptyRow" runat="server" style="text-align: center; color: red;">
No matching records found
</div>
</FooterTemplate>
</asp:Repeater>
答案 0 :(得分:1)
我猜您想要CommandArgument
中Repeater1_ItemCommand
的值。但是,如果您尝试将发件人强制转换为LinkButton,则该按钮将无效,因为发件人是中继器,而不是内部的按钮。
因此强制转换CommandSource并获取正确的值。
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
LinkButton lb = e.CommandSource as LinkButton;
Label1.Text = lb.CommandArgument;
}
您不能直接绑定tr
点击,因此必须使用技巧。您可以使用jQUery将LinkButton的点击设置为行本身。
<table border="1" id="Repeater1Table">
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr>
<td>Click here</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="PlayPrev" CommandArgument='<%#Eval("Id") %>' Text='<%#Eval("Name") %>' CssClass="name"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<script type="text/javascript">
$(document).ready(function () {
$("#Repeater1Table tr").each(function () {
var href = $(this).find(".name").prop("href").split(":")[1];
$(this).attr("onclick", href);
});
});
</script>