简体   繁体   中英

How can i access a control within a ListView once a button has been clicked?

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    
}

In this simple case, I should consider using 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>

now, define inside a script tag the showLblDone function:

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

You can also call this function with a parameter if you want to show/hide on every click, or you can use .toggle() instead of .show().

In this case you must add a div (or a Panel) inside the ItemTemplate.

You need to hook into the listview row bind and add the information you want to have when clicked. Using this, you can add an attribute to the button that you read back on click, for example...

If you posted some actual code, I could probably help some more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM