Thursday, February 9, 2012

Search Google using keyword in Asp.net C#

string link = "http://www.google.com/search?q=" + "CognetHRO";
Response.Redirect(link);

Create a Message Box in ASP.NET using C#

public void MsgBox1(string msg, Page refP)
{
Label lbl = new Label();
string lb = "window.alert('" + msg + "')";
ScriptManager.RegisterClientScriptBlock(refP, this.GetType(), "UniqueKey", lb, true);
refP.Controls.Add(lbl);
}

Function Call:

MsgBox1("Your Message", this);

Server side Confirmation code Programmatically in C#

Write this code at server side where you want to display confirmation box

Server side Confirmation code :


var sbk = new StringBuilder();
sbk.AppendFormat("var foo = window.confirm('Data Already Exists.Do you want to replace?');\n");
sbk.Append("if (foo)\n");
sbk.Append("__doPostBack('MyConfirmationPostBackEventTarget', foo);\n");
ClientScript.RegisterStartupScript(GetType(), "MyScriptKey", sbk.ToString(), true);
ClientScript.GetPostBackEventReference(this, string.Empty);


In page load write this code


ClientScript.GetPostBackEventReference(this, string.Empty);
if (IsPostBack)
{
string eventTarget = Request["__EVENTTARGET"] ?? string.Empty;
string eventArgument = Request["__EVENTARGUMENT"] ?? string.Empty;

switch (eventTarget)
{
case "MyConfirmationPostBackEventTarget":
if (Convert.ToBoolean(eventArgument))
{
AfterUserConfirmationHandler(tablename);
}
break;
}
}

Set Tab Indexes to Gridview Controls Programmatically in C#

private void SetTabIndexes_SutaRates()
{
short currentTabIndex = currentTabIndexSUTA;

foreach (GridViewRow gvr in grdCurrentCost.Rows)
{
TextBox inputField1 = (TextBox)gvr.FindControl("txtCurrentSutaRate");
inputField1.TabIndex = ++currentTabIndex;

DropDownList dropDown1 = (DropDownList)gvr.FindControl("ddlCompanyLst");
dropDown1.TabIndex = ++currentTabIndex;

TextBox inputField2 = (TextBox)gvr.FindControl("txtSutaBillRate");
inputField2.TabIndex = ++currentTabIndex;
}


}

Function Call in OnDataBound Event of Gridview

protected void grdCurrentCost_DataBound(object sender, EventArgs e)
{
SetTabIndexes_SutaRates();
}

Export Chart to Pdf in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.DataVisualization.Charting;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Color = System.Drawing.Color;
using Font = System.Drawing.Font;

public partial class Chart2Pdf : System.Web.UI.Page
{
Chart chart = new Chart();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["dbstring"].ToString());

protected void Page_Load(object sender, EventArgs e)
{

}

public void LoadchartControl()
{
DataSet ds = new DataSet();
SqlDataAdapter da=new SqlDataAdapter("select * from TestEmployee",con);
da.Fill(ds);

this.chart.Width = 500;
this.chart.Height = 500;

this.chart.Legends.Add("Legend1");
this.chart.Legends[0].Enabled = true;
this.chart.Legends[0].Docking = Docking.Bottom;
this.chart.Legends[0].Alignment = System.Drawing.StringAlignment.Center;
chart.Titles.Add("Sales By Employee");
chart.Titles[0].Font = new Font("Arial", 16f);

chart.ChartAreas.Add("");
chart.ChartAreas[0].AxisX.Title = "Employee";
chart.ChartAreas[0].AxisY.Title = "Sales";
chart.ChartAreas[0].AxisY.Interval = 100;
chart.ChartAreas[0].AxisX.TitleFont = new Font("Arial", 12f);
chart.ChartAreas[0].AxisY.TitleFont = new Font("Arial", 12f);
chart.ChartAreas[0].AxisX.LabelStyle.Font = new Font("Arial", 10f);
chart.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
chart.ChartAreas[0].BackColor = Color.White;
chart.ChartAreas[0].Area3DStyle.Enable3D = true;
chart.ChartAreas[0].Area3DStyle.Inclination = 50;

chart.Series.Add("");
chart.Series[0].ChartType = SeriesChartType.Column;
this.chart.Series[0].BorderWidth = 1;
this.chart.Series[0].BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
this.chart.Series[0].XValueMember = "Employee";
this.chart.Series[0].YValueMembers = "Sales";
chart.DataSource = ds;
chart.DataBind();

}

void ExportToPdf()
{
//Load the chart control as you normally do whenever you load it.

LoadchartControl();

chart.SaveImage(Server.MapPath( "~/chartImages/chart.png"), ChartImageFormat.Png);
Document document = new Document(PageSize.LETTER, 72, 72, 82, 72);
MemoryStream msReport = new MemoryStream();

try
{

PdfWriter writer = PdfWriter.GetInstance(document, msReport);
document.AddAuthor("Test");
document.AddSubject("Export to PDF");
document.Open();
Chunk c = new Chunk("Export chart to PDF", FontFactory.GetFont("VERDANA", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
iTextSharp.text.Image hImage;
hImage = iTextSharp.text.Image.GetInstance(MapPath("~/chartImages/chart.png"));

float NewWidth = 500;
float MaxHeight = 400;

if (hImage.Width <= NewWidth) { NewWidth = hImage.Width; } float NewHeight = hImage.Height * NewWidth / hImage.Width; if (NewHeight > MaxHeight)
{
NewWidth = hImage.Width * MaxHeight / hImage.Height;
NewHeight = MaxHeight;
}

float ratio = hImage.Width / hImage.Height;
hImage.ScaleAbsolute(NewWidth, NewHeight);
document.Add(p);
document.Add(hImage);
document.Close();

Response.AddHeader("Content-type", "application/pdf");
Response.AddHeader("Content-Disposition", "attachment; filename=chart.pdf");
Response.OutputStream.Write(msReport.GetBuffer(), 0, msReport.GetBuffer().Length);

}
catch (System.Threading.ThreadAbortException ex)
{
throw new Exception("Error occured: " + ex);
}

}

protected void btnExport_Click(object sender, EventArgs e)
{
ExportToPdf();
}

}