jeudi 30 juin 2016

asp.net fail get value from gridview checkbox

I have a GridView table which the data source is from the database. The first column of the GridView is a checkbox, the checkbox user can select checkboxes. I'm not sure where I did wrong,my code cannot get the checkbox I had ticked.

 <div id="UserFrom" class="form-horizontal" runat="server">
    <h4>Add Training</h4>
    <hr />
    <div class="alert alert-info" style="display: none;">
        <button data-dismiss="alert" class="close" type="button">×</button>
    </div>
    <div class="form-group">
        <label for="inputCode" class="col-sm-2 control-label">Training Code</label>
        <div class="col-sm-3">
            <asp:DropDownList ID="ddlRole" runat="server" CssClass="form-control" ValidationGroup="G1" required></asp:DropDownList>
        </div>
    </div>
    <div class="col-md-6">
        <asp:GridView ID="GrdRole" runat="server" CssClass="table table-striped table-bordered table-hover" EmptyDataText="No Records Found" DataKeyNames="RoleID"
            AllowPaging="true" AutoGenerateColumns="false" AutoGenerateDeleteButton="false" RowStyle-HorizontalAlign="Left" OnPageIndexChanging="GrdRole_PageIndexChanging"
            HeaderStyle-HorizontalAlign="Center" GridLines="None" PageSize="10" RowStyle-CssClass="gradeX" AlternatingRowStyle-CssClass="gradeA">
        <columns>
           <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chkCtrl" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Name" HeaderText="Name"  />
        <asp:BoundField DataField="EmployeeNo" HeaderText="EmployeeNo"  />
        </columns>
            <PagerStyle HorizontalAlign="Right" CssClass="pagination-ys" />
        </asp:GridView>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-9">
            <td>
                <asp:Button ID="submit_button" Text="Check" CssClass="btn btn-success" runat="server" OnClick="checkOuput" />
            </td>
        </div>
    </div>
</div>e

my c#

 protected void checkOuput(object sender, EventArgs e)
{
    string data = "";
    foreach (GridViewRow row in GrdRole.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
            if (chkRow.Checked)
            {
                string EmployeeNo = row.Cells[2].Text;
                data = data + EmployeeNo + " ,  " ;
            }
        }
    }
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + data + "');", true);
}

I'm not sure where I did wrong when I click on the check button the popup window is empty.please guide me thank you.




Aucun commentaire:

Enregistrer un commentaire