Showing posts with label Asp.Net Tutorials. Show all posts
Showing posts with label Asp.Net Tutorials. Show all posts

Asp.net show alert message from code behind using javascript

Asp.net show alert message from code behind using javascript

In this article we will learn how to call JavaScript function and show JavaScript alert message from code behind using asp.net.
To display JavaScript alert message from code behind we can do by using ScriptManager.RegisterStartupScript() method. This method will allow us to show alert message direclty from code behind or we can call javascript function from code behind in asp.net.

Declaration of ScriptManager.RegisterStartupScript() metod:

ScriptManager.RegisterStartupScript(Control control, Type type,string key, string script,
bool addScriptTags)


Here I will explain how to show JavaScript alert message in code behind and how to call JavaScript function from code behind in asp.net. For that first create new website and write following code in aspx page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Call Javascript functions and show alert message from code behind file in asp.net Page</title>
<script type="text/javascript">
function Showalert() {
alert('Call JavaScript function from codebehind');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Show alert" onclick="btnSubmit_Click" />
<asp:Button ID="btnClick" runat="server" Text="Call JavaScript Function"
onclick="btnClick_Click" />
</div>
</form>
</body>
</html>

using System;
using System.Web.UI;



After that write the following code in code behind

C# code

protected void Page_Load(object sender, EventArgs e)
{
}
//This button click event is used to show alert message directly
protected void btnSubmit_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this,GetType(),"showalert","alert('Only alert Message');",true);
}
// This button click event is used to show alert message from JavaScript function
protected void btnClick_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);
}




Export data from sqlserver database to excel in asp.net c#

Export data from sqlserver database to excel in asp.net c#
In this article we will learn how to export data directly from sql server database table to excel,
below is the database table which contain some data
I will export this data into excel file to excel by using C#.net coding as below:
1.Add a new file TabletoExcel.aspx in the solution explorer
Copy and paste below code in TabletoExcel.aspx page
01
02
03
04
05
06
07
08
09
10
11
12
13
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnGenaerateToExcel" runat="server" onclick="btnGenerateToExcel_Click" Text="Button" />
</div>
</form>
</body>
Copy and paste below code in TabletoExcel.cs  page:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class TableToExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGenerateToExcel_Click(object sender, EventArgs e)
{
PopulatExcelEntry();
}
protected void PopulatExcelEntry()
{
SqlConnection Connection = new SqlConnection("Server=Munesh-PC;Database=Griddata;Uid=sa;Pwd=123");
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter("select * from Tbl_Mst_Employee", Connection);
try
{
adapter.Fill(ds);
ExportToExcel(ds);
}
catch (Exception ex)
{
Connection.Close();
}
}
public static void ExportToExcel(System.ComponentModel.MarshalByValueComponent DataSource)
{
try
{
System.IO.StringWriter objStringWriter = new System.IO.StringWriter();
System.Web.UI.WebControls.DataGrid tempDataGrid = new System.Web.UI.WebControls.DataGrid();
System.Web.UI.HtmlTextWriter objHtmlTextWriter = new System.Web.UI.HtmlTextWriter(objStringWriter);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=TableToExcel.xls");
tempDataGrid.DataSource = DataSource;
tempDataGrid.DataBind();
tempDataGrid.HeaderStyle.Font.Bold = true;
tempDataGrid.RenderControl(objHtmlTextWriter);
DataSource.Dispose();
HttpContext.Current.Response.Write(objStringWriter.ToString());
HttpContext.Current.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex)
{
throw ex;
}
}
}
Run you application and click on button
Click on Ok and you will get your required output as: