繁体   English   中英

如何将commandArgument动态添加到LinkBut​​ton

[英]How can I add commandArgument dynamically to a LinkButton

我需要为面板列表中的每个项目添加一个“下载”按钮。 每个下载按钮应将唯一的ID传递给事件处理程序。

新按钮的问题:ID = DownloadPopup_btn_1是它没有将请求发送给带有我想要的参数的事件处理程序。

我尝试使用<%#Eval()%>方法,但是它以空字符串或纯文本“ item.PromotionId”的形式发送参数(取决于是否使用“”)。

我无法使用<%= item.PromotionId%>,因为未定义“ item”。

 <ul id="draggablePanelList" class="list-group">
            <% var popupIndex = 0;

                foreach (var item in OldGameSettings.PromotionalDataList)
                {
                    popupIndex++;
            %>

            <li class="list-group-item sortable-item first">

                <asp:LinkButton ID="DownloadPopup_btn_1" 
                                runat="server" 
                                CssClass="btn btn-default pull-right btn-xs"    
                                OnCommand="DownloadPopup_Click"
                                CommandArgument='<%#Eval(item.PromotionId)%>'>
                    <span aria-hidden="true" class="glyphicon glyphicon-download-alt"></span>
                </asp:LinkButton>

                <span style="margin-right: 15px;" class="pull-right">
                    <i class="glyphicon glyphicon-chevron-up" style="cursor: pointer; cursor: hand;" onclick="sendToTopPriority(this)" id="<%=item.PromotionId %>"></i>
                    <i class="glyphicon glyphicon-chevron-down" style="cursor: pointer; cursor: hand;" onclick="sendToBottomPriority(this)" id="<%=item.PromotionId %>"></i>
                    <i class="glyphicon glyphicon-chevron-right" style="cursor: pointer; cursor: hand;" onclick="sendToPriority(this)" id="<%=item.PromotionId %>"></i>
                </span>
            </li>
            <% } %>
        </ul>

预期:单击面板列表中的N'ish下载按钮,该按钮将使用CommandArgument == Button [N] .CommandArgument触发事件处理程序。

实际:单击面板列表中的N'ish下载按钮,该按钮使用CommandArgument ==触发事件处理程序,而不是我想要的...

PS我已经在代码的最后部分添加了代码,以证明item.PromotionId可在其他元素中使用。

您可以尝试使用中继器控件

<ul id="draggablePanelList" class="list-group">    
<asp:Repeater runat="server" ID="rptOutter"  >
        <ItemTemplate>

                <li class="list-group-item sortable-item first">

                    <asp:LinkButton ID="DownloadPopup_btn_1" 
                                    runat="server" 
                                    CssClass="btn btn-default pull-right btn-xs"    
                                    OnCommand="DownloadPopup_Click"
                                    CommandArgument='<%#Eval("PromotionId")%>'>
                        <span aria-hidden="true" class="glyphicon glyphicon-download-alt"></span>
                    </asp:LinkButton>

                    <span style="margin-right: 15px;" class="pull-right">
                        <i class="glyphicon glyphicon-chevron-up" style="cursor: pointer; cursor: hand;" onclick="sendToTopPriority(this)" id='<%#Eval("PromotionId")%>'></i>
                        <i class="glyphicon glyphicon-chevron-down" style="cursor: pointer; cursor: hand;" onclick="sendToBottomPriority(this)" id='<%#Eval("PromotionId")%>'></i>
                        <i class="glyphicon glyphicon-chevron-right" style="cursor: pointer; cursor: hand;" onclick="sendToPriority(this)" id='<%#Eval("PromotionId")%>'></i>
                    </span>
                </li>
        </ItemTemplate>
    </asp:Repeater>
</ul>

不要忘记将中继器数据源分配给您的OldGameSettings.PromotionalDataList

背后的代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        rptOutter.DataSource = OldGameSettings.PromotionalDataList;
        rptOutter.DataBind();
    }

}
 CommandArgument='<%#Eval(item.PromotionId)%>'>

这行必须是

 CommandArgument='<%#Eval("item.PromotionId")%>'>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM