Showing posts with label C#.Net. Show all posts
Showing posts with label C#.Net. Show all posts

Tuesday, 17 October 2017

How to trigger button click event when Enter key pressed in Textbox?

In this post, I will explain How to trigger button click event when Enter key pressed in Textbox?.

To trigger the Button click event when user press Enter Key in Textbox, I have used JavaScript function call with Textbox "onkeypress" event. There is no server-side keypress event for an ASP.Net Textbox. When the user presses any key, JavaScript function check whether it is an Enter key with the help of char code. If pressed key is Enter key then JavaScript function triggers an Asp.Net Button click event. Please go through below code for details.

HTML Code

<%@ Page Language = "C#" AutoEventWireup = "true" CodeBehind = "TriggerButtonClickEvent.aspx.cs" Inherits = "TestApplication.TriggerButtonClickEvent"  %>

<!DOCTYPE html>

<html>
<head>
    <title> How to trigger button click event when Enter key pressed in textbox? </title>

    <script type="text/javascript" language="javascript">

        function TriggerButtonClick(obj, event) {
            var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
            if (keyCode == 13) {
                document.getElementById(obj).click();
                return false;
            }
            else {
                return true;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">

        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

        <asp:Button ID="btnSubmit" runat="server" OnClick = "Button1_Click" />
    </form>
</body>
</html>

C#.Net Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestApplication
{
    public partial class TriggerButtonClickEvent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtName.Attributes.Add("onkeypress", "return TriggerButtonClick('" + btnSubmit.ClientID + "', event)");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Response.Write("Button click event triggered!");
        }
    }
}




Sunday, 16 April 2017

Export Gridview data to Excel in ASP.Net

In this post, I will explain how to export gridview to Excel in Asp.Net?

There often is a need in a project's reporting module to show records of a GridView in an Excel sheet. In ASP.Net we can easily export data of gridview data to an excel file. I will explain how to export data of gridview to an Excel sheet with an example.

In this example, we will bind gridview on the page load event and will export it to excel on "Export to Excel" button click.

ASPX Page Code(HTML Code)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ExportToExcel.aspx.vb" Inherits="ExportToExcel" %>
<!DOCTYPE html>
<html lang="">
<head runat="server">
    <title>Gridview Export to Excel in ASP.Net</title>
    <style>
        body {
            font-family: Georgia;
            font-size: small;
        }
        .GridViewStyle {
            border-right: 2px solid #A7A6AA;
            border-bottom: 2px solid #A7A6AA;
            border-left: 2px solid white;
            border-top: 2px solid white;
            padding: 4px;
        }
            .GridViewStyle a {
               
            }

        .GridViewHeaderStyle th {
            border-left: 1px solid #EBE9ED;
            border-right: 1px solid #EBE9ED;
        }

        .GridViewHeaderStyle {
            font-weight: bold;
        }

        .GridViewFooterStyle {
            font-weight: bold;
          }
        .GridViewRowStyle {
        }
        .GridViewAlternatingRowStyle {
        }

            .GridViewRowStyle td, .GridViewAlternatingRowStyle td {
                border: 1px solid #EBE9ED;
            }

        .GridViewSelectedRowStyle {
            font-weight: bold;
        }

        .GridViewPagerStyle {
           
        }

            .GridViewPagerStyle table /* to center the paging links*/ {
                margin: 0 auto 0 auto;
            }
    </style>
</head>
<body>
    <form id="form1" runat="server" style="vertical-align: top;">
        <h2>Gridview Export to Excel in ASP.Net</h2>
        <div>
            <asp:GridView ID="gridViewEmp" runat="server" AutoGenerateColumns="false">
                <FooterStyle CssClass="GridViewFooterStyle" />
                <RowStyle CssClass="GridViewRowStyle" />
                <SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
                <PagerStyle CssClass="GridViewPagerStyle" />
                <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
                <HeaderStyle CssClass="GridViewHeaderStyle" />
                <Columns>
                    <asp:BoundField DataField="Code" HeaderText="Code" />
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:BoundField DataField="City" HeaderText="City" />
                    <asp:BoundField DataField="Designation" HeaderText="Designation" />
                    <asp:BoundField DataField="Department" HeaderText="Department" />
                </Columns>
            </asp:GridView>
        </div>
        <br />
        <asp:Button ID="btnExport" runat="server" Text="Export To Excel" />
    </form>
</body>
</html>

VB.Net Code

Imports System.Data
Imports System.IO

Partial Class ExportToExcel
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Dim dt As New DataTable()
            dt.Columns.AddRange(New DataColumn(4) {
           New DataColumn("Code"),
         New DataColumn("Name"),
         New DataColumn("City"),
         New DataColumn("Designation"),
         New DataColumn("Department")
         })

            dt.Rows.Add("E0001", "Sachin", "Nagpur", "Software Developer", "IT")
            dt.Rows.Add("E0002", "Harshal", "Mumbai", "Recruiter", "HR")
            dt.Rows.Add("E0003", "Nilesh", "Pune", "Accountant", "Finance")
            dt.Rows.Add("E0004", "Shrikant", "Jalgaon", "Sales Manager", "Marketing")
            gridViewEmp.DataSource = dt
            gridViewEmp.DataBind()
        End If
    End Sub

    Protected Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
        Try
            Dim FileName As String
            FileName = "Employee List"

            Response.Clear()
            Response.Buffer = True
            Response.AddHeader("content-disposition", "attachment;filename=" & FileName + ".xls")
            Response.Charset = ""
            Response.ContentType = ContentType
            Dim sw As New StringWriter()
            Dim HW As New HtmlTextWriter(sw)
            gridViewEmp.RenderControl(HW)
            Dim ExportString As String
            ExportString = "<html><head><style type='text/css'>"
            ExportString = ExportString & "</style></head><body>"
            ExportString = ExportString & "<h2><Center>" & FileName & "</Center></h2><br>"
            ExportString = ExportString & sw.ToString()
            ExportString = ExportString & "</body></html>"

            Response.Write(ExportString)
            Response.Flush()
            Response.Close()
            Response.End()
        Catch ex As Exception
        End Try
    End Sub
End Class