Hi @Abdul Baba Syed ,
You should use "OnCheckedChanged" event so that the total value will be changed whenever the check box is checked/unchecked.
The key is to use "NamingContainer" to fetch the ListViewItem from the changed checkbox.
More details, you could refer to below codes.
aspx (based on your codes):
Add below event handler in the ListView for DataBind:
- [OnItemDataBound="ComboMain_ItemDataBound"]
Add OnCheckedChanged event handler on each control of the ListViewItem.
For example,
<asp:CheckBox ID="CheckBox1"...... OnCheckedChanged="CheckBox_CheckedChanged" />
<asp:CheckBox ID="CheckBox2"...... OnCheckedChanged="CheckBox_CheckedChanged" />
<asp:CheckBox ID="CheckBox3"...... OnCheckedChanged="CheckBox_CheckedChanged" />
Code behind with simulation data:
// Simulation of the data
private static DataTable _listviewDT;
public static DataTable ListviewDT
{
get
{
if (_listviewDT is null)
{
_listviewDT = new DataTable();
_listviewDT.Columns.Add("Name", typeof(string));
_listviewDT.Columns.Add("Price1", typeof(int));
_listviewDT.Columns.Add("Price2", typeof(int));
_listviewDT.Columns.Add("Price3", typeof(int));
_listviewDT.Rows.Add("name1",1000,2000,3000);
}
return _listviewDT;
}
set
{
_listviewDT = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindListView();
}
}
protected void BindListView()
{
ComboMain.DataSource = ListviewDT;
ComboMain.DataBind();
}
// Bind Item and Sum up for the total value
protected void ComboMain_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if(e.Item.ItemType == ListViewItemType.DataItem)
{
CheckBox CheckBox1 = (CheckBox) e.Item.FindControl("CheckBox1");
CheckBox CheckBox2 = (CheckBox)e.Item.FindControl("CheckBox2");
CheckBox CheckBox3 = (CheckBox)e.Item.FindControl("CheckBox3");
Label total = (Label)e.Item.FindControl("total");
int price1 = CheckBox1.Checked ? int.Parse(CheckBox1.Text,NumberStyles.Currency) : 0;
int price2 = CheckBox2.Checked ? int.Parse(CheckBox2.Text, NumberStyles.Currency) : 0;
int price3 = CheckBox3.Checked ? int.Parse(CheckBox3.Text, NumberStyles.Currency) : 0;
total.Text = String.Format("{0:C}", price1 + price2 + price3);
}
}
// Change the total value when the check box is checked/unchecked
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
ListViewItem item = (ListViewItem) cb.NamingContainer;
CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
CheckBox CheckBox2 = (CheckBox)item.FindControl("CheckBox2");
CheckBox CheckBox3 = (CheckBox)item.FindControl("CheckBox3");
Label total = (Label)item.FindControl("total");
int price1 = CheckBox1.Checked ? int.Parse(CheckBox1.Text, NumberStyles.Currency) : 0;
int price2 = CheckBox2.Checked ? int.Parse(CheckBox2.Text, NumberStyles.Currency) : 0;
int price3 = CheckBox3.Checked ? int.Parse(CheckBox3.Text, NumberStyles.Currency) : 0;
total.Text = String.Format("{0:C}", price1 + price2 + price3);
}
Demo:
Hope helps.
Best regards,
Sean
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.