Get all Checkbox controls in a Page or a Container

Posted by Unknown On Tuesday, December 30, 2014 0 comments

Following is a way to get all checkbox controls in a Panel named panelMainContent.

C#

private void GetCheckBoxControls()
{
         LoopControls(panMainContent.Controls);
}

private void LoopControls(controlCollection)
{         
        foreach (Control control in controlCollection)
        {
            if (control is CheckBox)
            {
                var menuControl = (CheckBox)control;
                if (menuControl.Checked)
                {
                     //do something
                }
            }

            if (control.Controls != null)
            {
                LoopControls(control.Controls);
            }
        }
}

ASP.NET Ajax Calendar reset value on postback Fixed

Posted by Unknown On Wednesday, December 24, 2014 0 comments
Problem: CalendarExtender value is reset on Postback.

Solution:: If you try to get selected value from Ajax Calendar Control, you will get only the default select value that you first set it. The best way to get selected datetime value is to read value from TextBox control instead.

If you want to display default today date when page load, please set it to textbox. Following is the example code:

.aspx page
 <asp:TextBox ID="txbStartDate" runat="server">
 <asp:CalendarExtender ID="calStartDate" runat="server" Format="MM/dd/yyyy" Enabled="True" TargetControlID="txbStartDate">
 </asp:CalendarExtender>
        
 <asp:Button runat="server" Text="Get Start Date" ID="butGetStartDate" />
 <asp:Label runat="server" Text="" ID="lblStartDate">

.cs page, form load
 protected void Page_Load(object sender, EventArgs e)
 {
      if (!Page.IsPostBack)
      {
           txbStartDate.Text = DateTime.Today.AddDays(-7).ToString("MM/dd/yyyy");
      }

      butGetStartDate.Click += OnGetStartDateClick;
 }

.cs page, Button Clicked
 private void OnGetStartDateClick(object sender, EventArgs e)
 {
      lblStartDate.Text = txbStartDate.Text;
 }

Bulk Export Data To Excel using Gridview

Posted by Unknown On Wednesday, December 17, 2014 0 comments

Fast, Short and Easy way to generate gridview to excel. You can also get rid of unnessary controls like check box, button, and select link etc, but you can if you want to.

c#
    private void ExportToExcel()
    {
        string attachment = "attachment; filename=statement.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView newGrid = new GridView();

        //if you want to copy old gridview column
        //DataControlFieldCollection columns = OldGridView.Columns;
        //foreach (DataControlField col in columns)
        //{
        //    newGrid.Columns.Add(col);
        //}

        //if you want to manage columns on your own
        //newGrid.Columns.Add(new BoundField { DataField = "CreateDate", HeaderText = "Created Date" });
        //newGrid.AutoGenerateColumns = false;
            
        newGrid.DataSource = SqlDataSource1.Select(DataSourceSelectArguments.Empty) as DataView;
        newGrid.DataBind();

        newGrid.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();        
    }