mardi 27 février 2018

ASP.NET/C#: Retrieving listview value and passing to C# via a checkbox

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.




Aucun commentaire:

Enregistrer un commentaire