Monday 29 December 2014

How to select checkbox and display values in textboxes and when checkbox not selected dont show value

 <tr>
                                <td>
                                    Select Center:
                                </td>
                                <td>
                                    <asp:TextBox ID="txt_city" runat="server" BackColor="White" CssClass="textbox" placeholder="Select Centre"
                                        ReadOnly="true" Width="300px"></asp:TextBox>
                                    <cc1:PopupControlExtender ID="PopupContExtcity" runat="server" Enabled="True" ExtenderControlID=""
                                        OffsetY="18" PopupControlID="pnl_city" TargetControlID="txt_city">
                                    </cc1:PopupControlExtender>
                                    <asp:Panel ID="pnl_city" runat="server" BackColor="White" BorderColor="#B3B1AD" BorderWidth="1px"
                                        Direction="LeftToRight" Height="116px" ScrollBars="Auto" Style="display: none"
                                        Width="300px">
                                        <asp:CheckBox ID="chk_city_all" runat="server" AutoPostBack="false" Text="All" />
                                        <asp:CheckBoxList ID="chklistCity" runat="server" AutoPostBack="false">
                                        </asp:CheckBoxList>
                                    </asp:Panel>
                                </td>
                            </tr>

=======================================================
<script language="javascript" type="text/javascript">
        var ModalProgress = '<%= ModalProgress.ClientID %>';

        function EndRequestHandler(sender, args) {
            if (args.get_error() == undefined) {
                chklistCity();
                ChkListEnterBy();
            }
        }

        $(function () {
            chklistCity();
        });

        function chklistCity() {
            $("[id*=chk_city_all]").bind("click", function () {
                if ($(this).is(":checked")) {
                    $("[id*=chklistCity] input").attr("checked", "checked");

                } else {
                    $("[id*=chklistCity] input").removeAttr("checked");
                }
                updatecity();
            });
            $("[id*=chklistCity] input").bind("click", function () {
                if ($("[id*=chklistCity] input:checked").length == $("[id*=chklistCity] input").length) {
                    $("[id*=chk_city_all]").attr("checked", "checked");

                } else {
                    $("[id*=chk_city_all]").removeAttr("checked");

                }
                updatecity();
            });

            function updatecity() {
                var txt_city = "";
                $("[id*=chklistCity] input:checked").each(function () {
                    txt_city += $(this).siblings("label").html() + ",";
                });
                //console.log(txtSubCity);
                $("#<%= txt_city.ClientID %>").val(txt_city);
            }
        }

       
    </script>
==============================================================
private void Bind_Center_CheckBox()
    {
        if (con.State != ConnectionState.Open)
        {
            con.Open();
        }
        //chklistCity.DataSource = clsc.Get_DataReader("Select SC_CODE,SC_DESC from ID_citymst order by SC_DESC", con);
        //chklistCity.DataValueField = "SC_CODE";
        //chklistCity.DataTextField = "SC_DESC";
        chklistCity.DataSource = clsc.Get_DataReader("Select cc_centerno code,cc_name descr from id_check_up_centers where cc_active_yn='Y' order by CC_NAME", con);
        chklistCity.DataValueField = "code";
        chklistCity.DataTextField = "descr";
        chklistCity.DataBind();
        chk_city_all.Checked = true;
        string strname = "";
        foreach (ListItem item in chklistCity.Items)
        {
            item.Selected = true;
            strname = strname + item.Text + ",";
        }
        txt_city.Text = strname;

        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "jscript1", "chklistCity();", true);
    }

Friday 5 December 2014

export gridview to excel in asp.net c#

public void ExportGridviewToExcel(GridView gvnmae, string strFileName)
    {
        try
        {
            HttpContext context = HttpContext.Current;
            string strText = "";

            context.Response.Clear();
            context.Response.Buffer = true;
            context.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName + ".csv");
            context.Response.Charset = "";
            context.Response.ContentType = "application/text";

            StringBuilder sb = new StringBuilder();
            for (int k = 0; k <= gv.HeaderRow.Cells.Count - 1; k++)
            {
                if (gv.HeaderRow.Cells[k].Visible == true & gv.HeaderRow.Cells[k].CssClass != "hiddenColumn")
                {
                    //add separator
                    sb.Append(gv.HeaderRow.Cells[k].Text + ',');
                }
            }
            //append new line          
            sb.Append(System.Environment.NewLine);
            for (int i = 0; i <= gv.Rows.Count - 1; i++)
            {
                for (int k = 0; k <= gv.Rows[i].Cells.Count - 1; k++)
                {
                    if (gv.Rows[i].Cells[k].Visible == true & gv.Rows[i].Cells[k].CssClass != "hiddenColumn")
                    {
                        //add separator

                        strText = "";
                        strText = (gv.Rows[i].Cells[k].Text.Contains(",") ? "\"" + gv.Rows[i].Cells[k].Text + "\"" : gv.Rows[i].Cells[k].Text);
                        strText = strText.Replace("&nbsp;", "");

                        //sb.Append(gv.Rows(i).Cells(k).Text + ","c)
                        sb.Append(strText + ',');
                    }
                }
                //append new line
                sb.Append(System.Environment.NewLine);
            }
            context.Response.Output.Write(sb.ToString());
            context.Response.Flush();
            context.Response.End();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Getting database values into checkboxlist in templatefield in gridview

<asp:GridView ID="gvccedetails" runat="server" AutoGenerateColumns="false" CssClass="table">
                        <AlternatingRowStyle CssClass="success" />
                        <HeaderStyle CssClass="warning" />
                        <SelectedRowStyle CssClass="active" />
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
                                        OnCommand="btnSelect_Click" />
                                </ItemTemplate>
                            </asp:TemplateField>                          
                            <asp:BoundField DataField="cceid" HeaderText="CCE ID" />
                            <asp:BoundField DataField="ccename" HeaderText="CCE Name" />
                            <asp:BoundField DataField="ccecontact" HeaderText="CCE Contact" />
                            <asp:BoundField DataField="cceemail" HeaderText="CCE Email" />
                            <asp:BoundField DataField="ccesharedyn" HeaderText="Shared Resources Y/N" />
                            <asp:TemplateField HeaderText="Working Days">
                                <ItemTemplate>
                                    <asp:CheckBoxList ID="cbl_cceworkingDays" RepeatDirection="Horizontal" runat="server"
                                        EnableViewState="true">
                                        <asp:ListItem Text="Mon" Value="Mon"></asp:ListItem>
                                        <asp:ListItem Text="Tue" Value="Tue"></asp:ListItem>
                                        <asp:ListItem Text="Wed" Value="Wed"></asp:ListItem>
                                        <asp:ListItem Text="Thu" Value="Thu"></asp:ListItem>
                                        <asp:ListItem Text="Fri" Value="Fri"></asp:ListItem>
                                        <asp:ListItem Text="Sat" Value="Sat"></asp:ListItem>
                                        <asp:ListItem Text="Sun" Value="Sun"></asp:ListItem>
                                    </asp:CheckBoxList>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="ccecentreid" HeaderText="" HeaderStyle-CssClass="hiddenColumn"
                                ItemStyle-CssClass="hiddenColumn" />
                        </Columns>
                    </asp:GridView>







private void fillGrid()
    {
        try
        {
            string[] items = null;

            strSql = " select distinct CCE_ID,CCE_WORKINGDAYS,cce_centreno, em_first_name ||' '|| em_middle_name ||' '|| em_last_name empname," +
                     " EM_EMAIL, to_char( EM_DS_CELL_NO) EM_DS_CELL_NO, EM_EMPCODE,DM_ID,dm_desc " +
                     " from id_cce_details,ID_EMPMST,id_deptmst " +
                     " where cce_centreno='" + hdnCenterID.Value + "' and EM_DEPT_ID = DM_ID and EM_EMPCODE=CCE_ID ";

            if (con.State != ConnectionState.Open) con.Open();
            DataSet ds = cls.Get_DataSet(strSql, con);

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow[] drs = ds.Tables[0].Select("cce_centreno='" + hdnCenterID.Value + "' and CCE_ID='" + ds.Tables[0].Rows[i]["CCE_ID"].ToString() + "' ");

                    if (drs.Length > 0)
                    {
                        strWorkDays = "";
                        drGrid = dtGrid.NewRow();
                        drGrid["ccecentreid"] = drs[0]["CCE_CENTRENO"].ToString();
                        drGrid["cceid"] = drs[0]["CCE_ID"].ToString();
                        drGrid["ccename"] = drs[0]["empname"].ToString();
                        drGrid["ccecontact"] = drs[0]["EM_DS_CELL_NO"].ToString();
                        drGrid["cceemail"] = drs[0]["EM_EMAIL"].ToString();
                        drGrid["ccedepartment"] = drs[0]["dm_desc"].ToString();
                        for (int j = 0; j < drs.Length; j++)
                        {
                            strWorkDays += drs[j]["CCE_WORKINGDAYS"].ToString() + ",";
                            i++;
                        }
                        drGrid["cceworkingdays"] = strWorkDays;
                        dtGrid.Rows.Add(drGrid);
                    }
                }
                ViewState["dtGrid"] = dtGrid;
                dtGridmodified = dtGrid.DefaultView.ToTable(true);
                gvccedetails.DataSource = dtGridmodified;
                gvccedetails.DataBind();

                for (int j = 0; j < gvccedetails.Rows.Count; j++)
                {
                    CheckBoxList c = (CheckBoxList)gvccedetails.Rows[j].FindControl("cbl_cceworkingDays");
                    if (c != null)
                    {
                        items = null;
                        items = dtGrid.Rows[j]["cceworkingdays"].ToString().Split(',');
                        for (int i = 0; i < c.Items.Count; i++)
                        {
                            if (items.Contains(c.Items[i].Value))
                            {
                                c.Items[i].Selected = true;
                            }
                        }
                    }
                }
            }
            else lblError.Text = "No record found";
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }