【问题标题】:How can I add commandArgument dynamically to a LinkButton如何将 commandArgument 动态添加到 LinkBut​​ton
【发布时间】:2019-01-02 12:39:04
【问题描述】:

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

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

我尝试使用 方法,但它以空字符串或纯文本“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 ==不是我想要的...

附:我在代码中添加了最后一部分以证明 item.PromotionId 在不同的元素中有效。

【问题讨论】:

  • CommandArgument == not what I want... CommandArgument 到底得到了什么?
  • 你可以试试&lt;%=item.PromotionId %&gt;而不是'&lt;%#Eval(item.PromotionId)%&gt;
  • @ChetanRanpariya 我得到一个空字符串或 的纯文本。我尝试了您的建议,但在这种情况下项目未知。
  • @EtianChamay,您是否在代码或其 100% 内联 C# 代码中使用 Page_Load 等服务器端事件?

标签: c# asp.net webforms


【解决方案1】:

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

<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();
    }

}

【讨论】:

    【解决方案2】:
     CommandArgument='<%#Eval(item.PromotionId)%>'>
    

    这一行必须是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-27
      • 2015-08-29
      • 2012-10-13
      • 2018-10-20
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多