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

时间:2019-01-01 13:36:49

标签: c# asp.net webforms

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

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

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

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

Sub test()
    Dim EMPLID As String
    Dim rngDB As Range, Rng As Range, rngU As Range
    Dim Ws As Worksheet
    Dim strAddress As String

    EMPLID = Application.InputBox("Enter Your Employee Number", "Employee Number")


    Set Ws = Sheets("Tracking Data")
    With Ws
        Set rngDB = .Range("e1", .Range("e" & Rows.Count).End(xlUp))
    End With
    With rngDB
        .EntireRow.Hidden = False
        Set Rng = .Find(What:=EMPLID, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)

        If Not Rng Is Nothing Then
            strAddress = Rng.Address
            Do
                If rngU Is Nothing Then
                    Set rngU = Rng

                Else
                    Set rngU = Union(rngU, Rng)
                End If
                Set Rng = .FindNext(Rng)
            Loop While Rng.Address <> strAddress

        End If
    End With
    If rngU Is Nothing Then
        MsgBox "Nothing found"
    Else
        rngDB.EntireRow.Hidden = True
        rngU.EntireRow.Hidden = False
    End If

End Sub

预期:单击面板列表中的N'ish下载按钮,该按钮将使用CommandArgument == Button [N] .CommandArgument

触发事件处理程序。

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

P.S。我在代码中添加了最后一部分,以证明item.PromotionId可在其他元素中使用。

2 个答案:

答案 0 :(得分:2)

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

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

}

答案 1 :(得分:0)

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

此行必须是

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