Breaking News

Editors Picks

Friday, May 25, 2012

Merge GridView Header column in ASP.NET


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestwebPage.aspx.cs" Inherits="TestwebPage" %>

<%@ Register Assembly="Obout.Ajax.UI" Namespace="Obout.Ajax.UI.TreeView" TagPrefix="obout" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> gridview merge column headers title>
    <script type="text/javascript">
     function Filter(Obj)
     {
        var grid=document.getElementById('GridView');
        var terms = Obj.value;
        var cellNr = "0";//grid colunm cellindex in which you want to search
        var ele;
        for (var r = 1; r < grid.rows.length; r++)
        {
          ele = grid.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g, "");
            if (ele.toLowerCase().indexOf(terms) >= 0)
                grid.rows[r].style.display = '';
            else grid.rows[r].style.display = 'none';
        }
     };   
  
    script>

head>
<body>
    <form id="form1" runat="server">   
<asp:GridView ID="gridView" runat="server" OnRowCreated="gridView_RowCreated">asp:GridView>   
    form>
body>
html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Obout.Ajax.UI.TreeView;
using System.Data.SqlClient;
using System.Net.Mail;
using System.IO;
using System.Collections.Generic;


public partial class TestwebPage : System.Web.UI.Page
{
    public enum AnalysisType { sss, ssss };
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }

    protected void gridView_PreRender(object sender, EventArgs e)
    {
        MergeRows(gridView);
    } 

    void BindGridView()
    {
        DataSet dataSet = new DataSet();
        gridView.DataSource = dataSet;
        gridView.DataBind();
    }

   protected void gridView_ RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //custom header.
GridView gvHeader = (GridView)sender;
GridViewRow gvHeaderRow = new GridViewRow(0, 0,    DataControlRowType.Header,DataControlRowState.Insert);
TableCell tcell = new TableCell();

// First column
tcell.Text = "First Column1";
tcell.ColumnSpan = 2;
tcell.BackColor = System.Drawing.Color.DarkGray;
gvHeaderRow.Cells.Add(tcell);

// Second  column
tcell = new TableCell();
tcell.Text = "Second Column2";
tcell.ColumnSpan = 2;
gvHeaderRow.Cells.Add(tcell);

gvHeader.Controls[0].Controls.AddAt(0, gvHeaderRow);

        }
    }}




Read more ...

How to merge cells with equal values in a GridView in Asp.net


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestwebPage.aspx.cs" Inherits="TestwebPage" %> 

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>merge row in grid viewtitle>
    <script type="text/javascript">
     function Filter(Obj)
     {
        var grid=document.getElementById('GridView');
        var terms = Obj.value;
        var cellNr = "0";//grid colunm cellindex in which you want to search
        var ele;
        for (var r = 1; r < grid.rows.length; r++)
        {
          ele = grid.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g, "");
            if (ele.toLowerCase().indexOf(terms) >= 0)
                grid.rows[r].style.display = '';
            else grid.rows[r].style.display = 'none';
        }
     };   
  
    script>

head>
<body>
    <form id="form1" runat="server">   
    <asp:GridView ID="gridView" runat=server CellPadding="2" OnPreRender="gridView_PreRender">asp:GridView>
   
    form>
body>
html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net.Mail;
using System.IO;
using System.Collections.Generic;


public partial class TestwebPage : System.Web.UI.Page
{
    public enum AnalysisType { sss, ssss };
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }

    protected void gridView_PreRender(object sender, EventArgs e)
    {
        MergeRows(gridView);
    } 

    void BindGridView()
    {
        DataSet dataSet = new DataSet();
        gridView.DataSource = dataSet;
        gridView.DataBind();
    }

    public static void MergeRows(GridView gridView)
    {
     for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
      {
         GridViewRow row = gridView.Rows[rowIndex];
         GridViewRow previousRow = gridView.Rows[rowIndex + 1];

       for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
       {
         if (row.Cells[cellIndex].Text == previousRow.Cells[cellIndex].Text)
         {
          row.Cells[cellIndex].RowSpan = previousRow.Cells[cellIndex].RowSpan < 2 ? 2 : previousRow.Cells[cellIndex].RowSpan + 1;
          previousRow.Cells[cellIndex].Visible = false;
         }
        }
      }
    }
} 
Read more ...

Wednesday, May 9, 2012

send email from asp.net with dynamic contents.


Mailcontant.html page

<table border="1" style="font-family:Arial; border-color:Black; font-  size:11px;" cellspacing="0" cellpadding="2">
            <tr>
                <td width="120">ID :td>
                <td width="190">#id#td>
            tr>
            <tr>
                <td>Request Date :td>
                <td>#enquirydate#td>
            tr>           
            <tr>
                <td>Customer Name :td>
                <td>#customername#td>
            tr>
            <tr>
                <td>Customer Address1 :td>
                <td>#custaddress1#td>
            tr>
            <tr>
                <td>Customer Address2 :td>
                <td>#custaddress2#td>
            tr>
            <tr>
                <td>State :td>
                <td>#state#td>
            tr>
            <tr>
                <td>City :td>
                <td>#city#td>
            tr>
           
            <tr>
                <td>Remarks :td>
                <td>#remarks#td>
            tr>           

        table>


public static void sendmail(string ID,string mailbody)
{
string mailbodytext = "";
string mailID = string.Empty;
StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath("Mailcontant.html"));
sr = File.OpenText(HttpContext.Current.Server.MapPath("Mailcontant.html "));
mailbodytext = sr.ReadToEnd();
sr.Close();
string squery = string.Empty;
squery = "select * from Detail where ID='" + ID + "'";
DataTable  dr = DataAccess.getDataTable(squery);




if (dr.Rows.Count > 0)
{
mailbodytext = mailbodytext.Replace("#id#", ID);
mailbodytext = mailbodytext.Replace("#enquirydate#", Convert.ToDateTime(dr.Rows[0]["Date"]).ToString("dd/MM/yyyy"));
mailbodytext = mailbodytext.Replace("#customername#", dr.Rows[0]["Cust_Name"].ToString());
mailbodytext = mailbodytext.Replace("#custaddress1#", dr.Rows[0]["Cust_Address1"].ToString());
mailbodytext = mailbodytext.Replace("#custaddress2#", dr.Rows[0]["Cust_Address2"].ToString());
mailbodytext = mailbodytext.Replace("#state#", dr.Rows[0]["State"].ToString());
mailbodytext = mailbodytext.Replace("#city#",
dr.Rows[0]["City"].ToString());
mailbodytext = mailbodytext.Replace("#remarks#", dr.Rows[0]["Remarks"].ToString());
}

SmtpClient smtp = new SmtpClient();
MailMessage msg = new MailMessage();
smtp.EnableSsl = true;
msg.IsBodyHtml = true;
msg.To.Add(new MailAddress(ConfigurationManager.AppSettings["EmailTo"].ToString()));
msg.Subject = "Request - " + FeasibilityID;
msg.Body = mailbody + "
"
+ mailbodytext;
smtp.EnableSsl = true;
object state = msg;
smtp.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtp.Send(msg);
}
private static void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
        MailMessage mail = e.UserState as MailMessage;
        if (!e.Cancelled && e.Error != null)
        {
            message.Text = "Mail sent successfully";
        }
        else
        {
            message.Text = "Error occured";
        }
 }




Add smtp server setting inwebconfig file
<system.net>
<mailSettings>
<smtp from="work@gmail.com" deliveryMethod="Network">
<network host="smtp.gmail.com" port="587"
userName="work@gmail.com" password="12345"/>
smtp>
mailSettings>
system.net>
Read more ...

Contact Us

Name

Email *

Message *