简体   繁体   中英

How can I get the eval value from a list view in the code behind by the click of a button?

Basically value1 and value2 are two numbers from a database, when I click button1 I want to get the number in the code behind (c#) so I can add another number to it and the same for value2. I simply require the line needed in c# that will extract the Eval value.... Your help will be much appreciated.

<asp:Button ID="button1" runat="server" Text='Plus' OnClick="button1_Click"/> 
<asp:Label ID="value1" runat="server" Text=' <%# Eval("value1") %> ' /> 
<asp:Button ID="button2" runat="server" Text='Minus' OnClick="button2_Click" />  
<asp:Label ID="value" runat="server" Text=' <%# Eval("value2") %> '/> 

You can't use Eval() unless your in the ItemDataBound event, but you can store the value in the datakey collection and access it that way:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="Value1, Value2, Value3" ...>

And in the code-behind, you only need the row index:

var rowIndex = 0;
var value = lst.DataKeys[rowIndex]["Value1"].ToString();

You need to 'bind'

<asp:Label ID="value1" runat="server" Text=' <%# Bind("value1") %> ' />

Eval is one way and Bind is two way databinding. You may need to use Container.DataItem possibly but bind will probably do the job.

You will then be able to read the value of the Text attribute ie

var theText = value1.Text;

Additionally because the control is in a listview you may need to navigate the ListView controls collection:

theListView.FindControl("theIdOfTheControl")

HTH

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