views:

145

answers:

2

I need to access a label control in a listview when i've clicked a button (that is on the same row)...

Does anyone know how to do this please? :(

See below for more of an insight...

ASPX Page:

<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>

Code Behind:

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  A: 

Try like this.

Label lb = e.Item.FindControl("lblDone") as Label;    
b.Visible = false;    
lb.Text = "text goes here";
Saar
Yep... but that is my Question... that i can't access... this is why i'm asking it again... :(
Stephen
No good i'm afraid... I'm getting this error:'System.EventArgs' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)
Stephen
+1  A: 

@Saar's code should work but you might need to change your event handler to handle the ItemCommand event on the ListView, rather the the Click event of the button:

<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>

Then your event handler will look something like this:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e) {
    // @Saar's code
}
Ian Oxley
@Ian: thanks for completing. :)
Saar
Thats perfect cheers to you both!
Stephen