listview Checkbox click in a Row asp.net forms

Abdul Baba Syed 21 Reputation points
2021-02-10T05:24:14.833+00:00

Hi I have a listview we have 3 checkbox and total label in the each row

<asp:ListView ID="ComboMain" runat="server" GroupItemCount="1">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder" />
</LayoutTemplate>
<GroupTemplate>

                                    <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />

                            </GroupTemplate>
                            <ItemTemplate>

<td class="py-3"><%# Eval("Name") %> </td>
<td class="py-3"><asp:CheckBox ID="CheckBox1" runat="server" Visible="true" Checked="True" Text='<%# String.Format("{0:C}", Eval("Price1")) %>' AutoPostBack="True" /> </td>

<td class="py-3"><asp:CheckBox ID="CheckBox2" runat="server" Visible="true" Checked="True" Text='<%# String.Format("{0:C}", Eval("Price2")) %>' AutoPostBack="True" /> </td>
<td class="py-3"> <asp:CheckBox ID="CheckBox1" runat="server" Visible="true" Checked="True" Text='<%# String.Format("{0:C}", Eval("Price3")) %>' AutoPostBack="True" /> </td>

<td class="py-3">Total:
<asp:Label ID="total" runat="server" Text=""></asp:Label> </td>

  </ItemTemplate>
                        </asp:ListView>

if i click checkbox1, checkbox2, checkbox3, the total should come in the total label,
If I uncheck, it should total up the remaining checked one in the total label
how can i achieve this and this listview has multiple rows..
we need each row total in the total label for other rows as well.. appreciate your help

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sean Fang-MSFT 156 Reputation points
    2021-02-11T02:19:24.12+00:00

    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:
    hVZI3.gif

    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.


  2. Abdul Baba Syed 21 Reputation points
    2021-02-11T04:20:07.423+00:00

    Thanks a lot,

    Is there any way we can avoid post back on each CheckBox_CheckedChanged. so that whole page will not refresh it
    Appreciate your help.. Thanks a lot


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.