【问题标题】:How can i access a control within a ListView once a button has been clicked?单击按钮后,如何访问 ListView 中的控件?
【发布时间】:2013-07-31 02:59:55
【问题描述】:

单击按钮(位于同一行)时,我需要访问列表视图中的标签控件...

请问有人知道怎么做吗? :(

见下文了解更多信息...

ASPX 页面:

<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" Visible="false">Your vote has been counted</asp:Label>
<asp:Button ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' OnClick="voteOnThis" />
</ItemTemplate>

代码背后:

protected void voteOnThis(object sender, EventArgs e)
{
    Button myButton = (Button)sender;
    Voting.vote(int.Parse(myButton.CommandArgument));
    // Here i would like to access the 'label' lblDone and make this Visible    
}

【问题讨论】:

    标签: c# asp.net listview


    【解决方案1】:

    在这种简单的情况下,我应该考虑使用 Javascript (JQuery)

    <asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
    <LayoutTemplate>//Etc </LayoutTemplate>
    <ItemTemplate>
    <asp:Label ID="lblDone" runat="server" style="visibility:hidden">Your vote has been counted</asp:Label>
    <asp:Button OnClientClick="showLblDone()" ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' OnClick="voteOnThis" />
    </ItemTemplate>
    

    现在,在脚本标签内定义 showLblDone 函数:

    <script>
    function showLblDone (){
    $(this).siblings('span').show();}
    </script>
    

    如果您想在每次点击时显示/隐藏,也可以使用参数调用此函数,或者您可以使用 .toggle() 代替 .show()。

    在这种情况下,您必须在 ItemTemplate 中添加一个 div(或 Panel)。

    【讨论】:

      【解决方案2】:

      您需要挂钩到列表视图行绑定并添加单击时想要的信息。使用它,您可以为单击时回读的按钮添加一个属性,例如...

      如果您发布了一些实际代码,我可能会提供更多帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-24
        • 1970-01-01
        相关资源
        最近更新 更多