单击一个按钮后,如何访问ListView中的控件(ASP.NET c#)?

时间:2009-11-26 12:56:18

标签: c# asp.net listview

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

有人知道怎么做吗? :(

请参阅下文,了解更多信息......

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 

}

2 个答案:

答案 0 :(得分:2)

试试这样。

Label lb = e.Item.FindControl("lblDone") as Label;    
b.Visible = false;    
lb.Text = "text goes here";

答案 1 :(得分:1)

@Saar's code应该可以工作,但您可能需要更改事件处理程序来处理ListView上的ItemCommand事件,而不是按钮的Click事件:

<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource"
        OnItemCommand="ListView1_ItemCommand">
    <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") %>' />
    </ItemTemplate>
    ...
</asp:ListView>

然后您的事件处理程序将如下所示:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e) {
    // @Saar's code
}