mardi 25 juillet 2017

Checkbox is only getting value from one checkbox. I need to get value for multiple checkboxes when selected

I'm having some issues trying to get the Checkbox to receive multiple values when checked. I'm using a dynamic GridView. When the Checkbox is checked and the Start button is clicked the services are suppose to start. Only one service is starting and the value for the other services that are Checked are reading false. I know I'm doing something wrong. Thanks in advance!

                <asp:Panel ID="Panel1" runat="server">
                <asp:GridView ID="GridView1" runat="server" Style="font-size: 15pt" AutoGenerateColumns="False">

                    <Columns>
                        <asp:BoundField DataField="ServiceName" HeaderText="Service Name" />
                        <asp:BoundField DataField="Description" HeaderText="Description" />
                        <asp:BoundField DataField="Status" HeaderText="Status" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="ChkStatus"
                                    Enabled="True" runat="server"  />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </asp:Panel>


        protected void strBtn_Click(object sender, EventArgs e)
    {
        var dt = new DataTable();
        dt.Columns.Add("ServiceName", typeof(string));
        dt.Columns.Add("Description", typeof(string));
        dt.Columns.Add("Status", typeof(string));

        string include = txtbox_Inclusion.Text;
        string exclude = txtbox_Exclusion.Text;

        ServiceController[] services = ServiceController.GetServices();

        foreach (ServiceController service in services)
        {
            if (service.ServiceName.StartsWith(include) && !service.ServiceName.Contains(exclude))
            {

                var dro = dt.NewRow();
                dro["ServiceName"] = service.ServiceName;
                dro["Description"] = service.DisplayName;
                dro["Status"] = service.Status;
                dt.Rows.Add(dro);

                for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
                {
                    CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("ChkStatus"); //find the CheckBox
                    if (cb != null)
                    {
                        if (cb.Checked)
                        {
                            service.Start();
                            service.WaitForStatus(ServiceControllerStatus.Running);
                            dro["Status"] = service.Status;

                        }
                        else if (cb.Checked && service.Status == ServiceControllerStatus.Running)
                        {
                            cb.Enabled = false;
                        }
                    }
                }

                GridView1.DataSource = dt;
                GridView1.DataBind();

            }
        }


    }




Aucun commentaire:

Enregistrer un commentaire