Showing posts with label checkbox. Show all posts
Showing posts with label checkbox. Show all posts

Saturday, 7 December 2013

Export Selected GridViewRow to Excel on button click in C#.NET

Copy & Paste this on .aspx file:

<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" CellPadding="4"
                            BackColor="#5DA63A" ForeColor="White" Width="100%" AllowPaging="false" Style="border-radius: 10px;
                            border: Solid 3px #5DA63A; text-align: left; word-wrap: break-word; word-break: break-all">
                            <AlternatingRowStyle BackColor="#F1FCCA"></AlternatingRowStyle>
                            <RowStyle BackColor="#DEDFDE" ForeColor="Black" CssClass="grid_font" />
                            <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                            <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
                            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#8EBE12" Font-Bold="False" ForeColor="#E7E7FF" />
                            <Columns>
                                <asp:TemplateField ItemStyle-Width="20px">
                                    <HeaderTemplate>
                                        <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True" 
                                            oncheckedchanged="chkAll_CheckedChanged" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="True" 
                                            oncheckedchanged="chkSelect_CheckedChanged" />
                                    </ItemTemplate>
                                    <ItemStyle Width="20px" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="EIEUID" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblid" runat="server" Text='<%#Eval("EIEUid")%>'></asp:Label>
                                        </ItemTemplate> 
                                        <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                        <ItemStyle Width="60px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblname" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
                                        </ItemTemplate> 
                                        <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                        <ItemStyle Width="150px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Details" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblemail" runat="server" Text='<%#Eval("Email")%>'></asp:Label>
                                        </ItemTemplate> 
                                        <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                        <ItemStyle Width="160px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Mobile" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblmobile" runat="server" Text='<%#Eval("Mobile")%>'></asp:Label>
                                        </ItemTemplate> 
                                        <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                        <ItemStyle Width="120px" />
                                    </asp:TemplateField>
                            </Columns>
                        </asp:GridView>



Copy & Paste this on .cs Code File:

dataUtility du = new dataUtility();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindGridData();
        }

        /// This Method is used to bind gridview
        private void BindGridData()
        {
            try
            {
                DataTable dtnew = new DataTable();
                DataTable dt = new DataTable();
                dt = du.executeStoteProc("SP_CLIENT 5,'" + Session["Name"] + "','',''");
               
                    if (dt.Rows.Count > 0)
                    {
                        gvDetails.DataSource = dt;
                        gvDetails.DataBind();

                    }
                    else
                    {
                        dt.Rows.Add(dt.NewRow());
                        gvDetails.DataSource = dt;
                        gvDetails.DataBind();
                        int columncount = gvDetails.Rows[0].Cells.Count;
                        gvDetails.Rows[0].Cells.Clear();
                        gvDetails.Rows[0].Cells.Add(new TableCell());
                        gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
                        gvDetails.Rows[0].Cells[0].Text = "No Records Found";
                    }
                }

            }
            catch (Exception ex)
            {

            }
        }

        /// enable gridview control to be rendered
        public override void VerifyRenderingInServerForm(Control control)
        {
            /*Verifies that the control is rendered */
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ExportToExcell();
        }

        /// export gridview to excel
        private void ExportToExcell()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Details");
            dt.Columns.Add("Mobile");
            foreach (GridViewRow row in gvDetails.Rows)
            {
                CheckBox chkCalls = (CheckBox)row.FindControl("chkSelect");
                if (chkCalls.Checked == true)
                {
                    int i = row.RowIndex;
                    Label lblname = (Label)gvDetails.Rows[i].FindControl("lblname");
                    Label lblemail = (Label)gvDetails.Rows[i].FindControl("lblemail");
                    Label lblmobile = (Label)gvDetails.Rows[i].FindControl("lblmobile");

                    DataRow dr = dt.NewRow();
                    dr["Name"] = Convert.ToString(lblname.Text);
                    dr["Details"] = Convert.ToString(lblemail.Text);
                    dr["Mobile"] = Convert.ToString(lblmobile.Text);
                    dt.Rows.Add(dr);
                }
            }
            GridView gdvExportxls = new GridView();
            gdvExportxls.DataSource = dt;
            gdvExportxls.DataBind();
            gdvExportxls.HeaderStyle.Font.Bold = true;
            gdvExportxls.HeaderStyle.ForeColor = Color.White;
            gdvExportxls.HeaderStyle.BackColor = Color.Green;
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", string.Format("attachment;filename=ReferrerDetails.xls", "selectedrows"));
            Response.Charset = "";
            StringWriter stringwriter = new StringWriter();
            HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);
            gdvExportxls.RenderControl(htmlwriter);
            Response.Write(stringwriter.ToString().Replace("<div>", " ").Replace("</div>", " "));
            Response.End();

        }

        /// select/unselect all row using header checkbox
        protected void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkH = (CheckBox)gvDetails.HeaderRow.FindControl("chkAll");
                if (chkH.Checked == true)
                {
                    foreach (GridViewRow gv in gvDetails.Rows)
                    {
                        CheckBox chk = (CheckBox)gv.FindControl("chkSelect");
                        chk.Checked = true;
                    }
                }
                else
                {
                    foreach (GridViewRow gv in gvDetails.Rows)
                    {
                        CheckBox chk = (CheckBox)gv.FindControl("chkSelect");
                        chk.Checked = false;
                    }

                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        /// select/unselect single row using row checkbox
        protected void chkSelect_CheckedChanged(object sender, EventArgs e)
        {
            bool isFound = false;
            CheckBox cbxall = (CheckBox)gvDetails.HeaderRow.FindControl("chkAll");
            foreach (GridViewRow gvr in gvDetails.Rows)
            {
                CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
                if (chkSelect.Checked == false)
                {
                    isFound = true;
                    break;
                }

            }

            if (isFound)
                cbxall.Checked = false;
            else
                cbxall.Checked = true;

        }

Thursday, 5 December 2013

How To Style A Checkbox With CSS

Method 1:

/* in style tag */

.onoffswitch {
    position: relative; width: 65px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #999999; border-radius: 20px;
}
.onoffswitch-inner {
    width: 200%; margin-left: -100%;
    -moz-transition: margin 0.3s ease-in 0s; -webkit-transition: margin 0.3s ease-in 0s;
    -o-transition: margin 0.3s ease-in 0s; transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    float: left; width: 50%; height: 25px; padding: 0; line-height: 25px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "ON";
    padding-left: 10px;
    background-color: #FFC42F; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "OFF";
    padding-right: 10px;
    background-color: #EEEEEE; color: #999999;
    text-align: right;
}
.onoffswitch-switch {
    width: 15px; margin: 5px;
    background: #FFFFFF;
    border: 2px solid #999999; border-radius: 20px;
    position: absolute; top: 0; bottom: 0; right: 36px;
    -moz-transition: all 0.3s ease-in 0s; -webkit-transition: all 0.3s ease-in 0s;
    -o-transition: all 0.3s ease-in 0s; transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}

/* html tag */

<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
    <label class="onoffswitch-label" for="myonoffswitch">
        <div class="onoffswitch-inner"></div>
        <div class="onoffswitch-switch"></div>
    </label>
</div>




Method 2:

/* in style tag */

.slideThree {
width: 80px;
height: 26px;
background: #333;
margin: 20px auto;

-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
position: relative;

-webkit-box-shadow: inset 0px 1px 1px rgba(0,0,0,0.5), 0px 1px 0px rgba(255,255,255,0.2);
-moz-box-shadow: inset 0px 1px 1px rgba(0,0,0,0.5), 0px 1px 0px rgba(255,255,255,0.2);
box-shadow: inset 0px 1px 1px rgba(0,0,0,0.5), 0px 1px 0px rgba(255,255,255,0.2);
}

.slideThree:after {
content: 'OFF';
font: 12px/26px Arial, sans-serif;
color: #000;
position: absolute;
right: 10px;
z-index: 0;
font-weight: bold;
text-shadow: 1px 1px 0px rgba(255,255,255,.15);
}

.slideThree:before {
content: 'ON';
font: 12px/26px Arial, sans-serif;
color: #00bf00;
position: absolute;
left: 10px;
z-index: 0;
font-weight: bold;
}

.slideThree label {
display: block;
width: 34px;
height: 20px;

-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;

-webkit-transition: all .4s ease;
-moz-transition: all .4s ease;
-o-transition: all .4s ease;
-ms-transition: all .4s ease;
transition: all .4s ease;
cursor: pointer;
position: absolute;
top: 3px;
left: 3px;
z-index: 1;

-webkit-box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.3);
-moz-box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.3);
box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.3);
background: #fcfff4;

background: -webkit-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
background: -moz-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
background: -o-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
background: -ms-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
background: linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfff4', endColorstr='#b3bead',GradientType=0 );
}

.slideThree input[type=checkbox]:checked + label {
left: 43px;
}


/* html tag */

<div class="slideThree">
<input type="checkbox" value="None" id="slideThree" name="check" />
<label for="slideThree"></label>
</div>



Saturday, 5 October 2013

GridView Select all row using Header Checkbox or select one row using checkbox in C#.NET

Copy and Paste this code to .aspx Page :

<asp:GridView ID="GridView1" runat="server" Width="100%" BackColor="White" BorderColor="White"
                                BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None"
                                AutoGenerateColumns="False" Style="font-size: 10pt">
                                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                                <PagerStyle BackColor="#669900" ForeColor="White" Font-Bold="true" />
                                <HeaderStyle BackColor="#669900" Font-Bold="True" ForeColor="#E7E7FF" />
                                <Columns>
                                    <asp:TemplateField HeaderText="Course" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="id" runat="server" Text='<%#Eval("Row_Id")%>' Visible="false"></asp:Label>
                                            <asp:Label ID="lblgrade" runat="server" Text='<%#Eval("Quali1")%>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="35px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Grade" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblsubgrade" runat="server" Text='<%#Eval("Quali2")%>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="45px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Roll No" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblroll" runat="server" Text='<%#Eval("RollNo")%>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="80px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="180px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Father's Name" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblfname" runat="server" Text='<%#Eval("FName")%>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="180px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lbldate" runat="server" Text=""></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="70px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="cbxap" runat="server" AutoPostBack="true" OnCheckedChanged="cbxap_CheckedChanged" />
                                        </ItemTemplate>
                                        <HeaderTemplate>
                                            A/P<asp:CheckBox ID="cbxapall" runat="server" AutoPostBack="true" OnCheckedChanged="cbxapall_CheckedChanged" />
                                        </HeaderTemplate>
                                        <ItemStyle Width="50px" />
                                    </asp:TemplateField>
                                </Columns>

                            </asp:GridView>


and .cs Page Code is given:

Bind GridView:
private void bindgridview()
        {
            try
            {
                DataTable dt = app.GetDataTable("select * from tb_Regstd_mst where Center='" + ddlcenter.SelectedItem.Text + "' and Quali1='" + ddlgrade.SelectedItem.Text + "' and Quali2='" + ddlsubgrade.SelectedItem.Text + "' order by RollNo Asc");
                GridView1.DataSource = dt;
                GridView1.DataBind();
                foreach (GridViewRow gv in GridView1.Rows)
                {
                    Label date = (Label)gv.FindControl("lbldate");
                    date.Text = DateTime.Now.ToShortDateString();
                }
                if (GridView1.Rows.Count <= 0)
                {
                    btnattd.Visible = false;
                    lblmsg.Text = "No Student Records Found in this Category.";
                }
                else
                {
                    lblmsg.Text = "";
                    btnattd.Visible = true;
                }
            }
            catch (Exception)
            {

            }

        }


On Header Checkbox checkchanged Event :
protected void cbxapall_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkH = (CheckBox)GridView1.HeaderRow.FindControl("cbxapall");
                if (chkH.Checked == true)
                {
                    foreach (GridViewRow gv in GridView1.Rows)
                    {
                        CheckBox chk = (CheckBox)gv.FindControl("cbxap");
                        chk.Checked = true;
                    }
                }
                else
                {
                    foreach (GridViewRow gv in GridView1.Rows)
                    {
                        CheckBox chk = (CheckBox)gv.FindControl("cbxap");
                        chk.Checked = false;
                    }

                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }


Checkchanged event of each row :
protected void cbxap_CheckedChanged(object sender, EventArgs e)
        {
            bool isFound = false;
            CheckBox cbxall = (CheckBox)GridView1.HeaderRow.FindControl("cbxapall");
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                CheckBox chkSelect = gvr.FindControl("cbxap") as CheckBox;
                if (chkSelect.Checked == false)
                {
                    isFound = true;
                    break;
                }

            }

            if (isFound)
                cbxall.Checked = false;
            else
                cbxall.Checked = true;

        }