Table 行服务器端点击转发器?

Table row server side click in repeater?

我有一个中继器,它有 table 按照下面的代码,我需要在单击 table 行时将参数发送到后面的代码。 目前我正在点击 LinkBut​​ton1 传递它。 我也试过在 LinkBut​​ton 中包装,但它不起作用 我该怎么做呢?

<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>

我猜您想要 Repeater1_ItemCommandCommandArgument 的值。但是,如果您尝试将发件人转换为 LinkBut​​ton,这将不起作用,因为发件人是中继器,而不是内部的按钮。

因此转换 CommandSource 并获得正确的值。

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    LinkButton lb = e.CommandSource as LinkButton;
    Label1.Text = lb.CommandArgument;
}

你不能直接绑定tr点击,所以你必须使用一个技巧。您可以使用 jQUery 将 LinkBut​​ton 的点击设置为行本身。

<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>