0

I've seen various solutions to this on here, but for some reason it isn't working for me. Basically on the client side I'm generating Listview elements from a database and want to retrieve the value generated in the "catlbl" label value and pass to C# to add to an "int total variable", which already has a value in it when the page is generated. Here is my ASP.NET code:

<asp:ListView ID="elecMod_listview" runat="server" DataSourceID="elecModule_sqlDataSource">
     <ItemTemplate>
          <tr>
             <td>
                <asp:Hyperlink ID="elecModuleDesc_hyperlink" runat="server" CssClass="pull-left" Text='<%# string.Concat(Eval("code"), " - ", Eval("name"))%>' NavigateURL='<%# "student_module_info.aspx?searchquery=" + Eval("code")%>' />
             </td>
             <td>
                 <asp:Label ID ="cat_lbl" runat="server" Text='<%# Eval("catPoints") %>'></asp:Label>
             </td>
             <td>
                <asp:Label ID="semester_lbl" runat="server" Text='<%# Eval("semesterNum") %>'></asp:Label>
             </td>
             <td>
                <asp:CheckBox ID="elective_checkbox" runat="server" CssClass="pull-left" OnCheckedChanged="elective_checkbox_CheckedChanged" AutoPostBack="true"/>
             </td>
           </tr>
       </ItemTemplate>
</asp:ListView>

Here is my label code:

<div class="row mt">
   <div class="col-sm-12 col-md-12 col-lg-12">
      Total CAT Points Selected: <asp:Label ID="catTotal_lbl" runat="server" Text="" />
   </div>
</div>

And now here is my C# code:

//This value will be changed upon Page Load via other parts in the code and is displayed in the "catTotal_lbl" 
int total = 0; 
protected void elective_checkbox_CheckedChanged(object sender, EventArgs e)
{
    foreach (ListViewDataItem row in elecMod_listview.Items)
    {
        CheckBox catCheck = (CheckBox)row.FindControl("elective_checkbox");
        if(catCheck.Checked)
        {
            int elecCat = Convert.ToInt32(((Label)row.FindControl("cat_lbl")).Text);

            total += elecCat;
        }
    }

}

When I check the box, nothing happens. I basically want the value from "catPoints" added to the "catTotal_lbl" whenever I check the checkbox(s) and then stick that value in a database via C#. Been racking my brains on this for awhile! I'm still a newb so apologies if I broke any protocol.

EDIT: I was able to make it work by adding this to the C#:

 protected void elective_checkbox_CheckedChanged(object sender, EventArgs e)
{

    foreach (ListViewDataItem row in elecMod_listview.Items)
    {
        CheckBox catCheck = (CheckBox)row.FindControl("elective_checkbox");
        if(catCheck.Checked)
        {
            int elecCat = Convert.ToInt32(((Label)row.FindControl("cat_lbl")).Text);

            total += elecCat;
            catTotal_lbl.Text = total.ToString();
        }
    }

}

But I would like to update the catTotal_lbl Label upon checking the checkbox in an UpdatePanel in AJAX. Is this feasible? Basically when I check the checkbox now, it reloads the whole page but it DOES change the label to the correct amount of CAT points now. I don't want it to reload the entire page, though.

DSchwartz
  • 1
  • 1
  • What do you mean by nothing happens? Have you tried debugging the method elective_checkbox_CheckedChanged to see if you are getting the value in "elecCat" variable? If you want to show the value in the label you need to assign the value to lablel – Shetty Feb 27 '18 at 15:01
  • Sorry, I should have expanded on that, By nothing happening, I mean that the total in the catTotal_lbl isn't changing. I want to be able to click on the corresponding checkbox and add the value of "catPoints" to the "catTotal_lbl". "catTotal_lbl" is being generated by "int total" already in other parts of the code so basically I want the value from "catPoints" to be added to what's already in "int total", Also when I tried debugging it, it's not breaking when I check the checkbox. – DSchwartz Feb 27 '18 at 15:24

0 Answers0