Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Saturday, February 25, 2012

page load event with working example in c#

I have google a lot for working example of page events in C# but i was not able to find code in c# but found in vb. then i decided to write my own example in c# , so that it may help all my .net friends.
Here are two pages code one is .aspx and other one is .aspx.cs :

Default.aspx : 
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
 <asp:PlaceHolder runat="server" ID="LabelPlaceHolder" >
     <%--<asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>--%>
 </asp:PlaceHolder>
    <br />
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>
</asp:Content>
and  the code behind file for this page is  :
CODE Behind File :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
   
    void Page_Error(object sender, EventArgs e)
    {
        //This is for getting error from page and handing them accordingly
    }
    void Page_PreInit(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            Label lblMessage = new Label();
            lblMessage.ID = "lblMessage";
            lblMessage.Width = 500;
            lblMessage.Text = "Dynamically created control.";
            ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("MainContent");
            PlaceHolder LabelPlaceHolder = (PlaceHolder)ContentPlaceHolder1.FindControl("LabelPlaceHolder");
            LabelPlaceHolder.Controls.Add(lblMessage);
        }
    }
    void Page_Init(object sender, EventArgs e)
    {

        ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("MainContent");
        PlaceHolder LabelPlaceHolder = (PlaceHolder)ContentPlaceHolder1.FindControl("LabelPlaceHolder");
        Label lblMessage = (Label)LabelPlaceHolder.FindControl("lblMessage");
        lblMessage.ForeColor = System.Drawing.Color.Black;
        Trace.Write("Page_Init " + DateTime.Now.ToString());
    }
    void Page_InitComplete(object sender, EventArgs e)
    {
        ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("MainContent");
        PlaceHolder LabelPlaceHolder = (PlaceHolder)ContentPlaceHolder1.FindControl("LabelPlaceHolder");
        Label lblMessage = (Label)LabelPlaceHolder.FindControl("lblMessage");
        lblMessage.ToolTip = "Tooltip added during InitComplete event.";
        ViewState["Author"] = "Robert S. Robbins";
        System.Diagnostics.Trace.WriteLine("Page_InitComplete " + DateTime.Now.ToString());
    }
    void Page_PreLoad(object sender, EventArgs e)
    {
        System.Diagnostics.Trace.WriteLine(ViewState.Values.Count, "ViewState Count");
        System.Diagnostics.Trace.WriteLine(Request.ServerVariables["SCRIPT_NAME"], "Script Name");
        System.Diagnostics.Trace.WriteLine("Page_PreLoad " + DateTime.Now.ToString());
    }
    void Page_Load(object sender, EventArgs e)
    {
        SqlConnection cn;

        SqlCommand objSqlCommand = new SqlCommand();

        cn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Northwind"].ToString();

        cn.Open();

        objSqlCommand.Connection = cn;

        objSqlCommand.CommandText = "SELECT ProductID, ProductName, UnitPrice FROM Products ORDER BY ProductName";

        DataSet objDataSet = new DataSet();

        SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter();

        objSqlDataAdapter.SelectCommand = objSqlCommand;

        objSqlDataAdapter.Fill(objDataSet);

        GridView1.DataSource = objDataSet;

        GridView1.DataBind();

        cn.Close();

        System.Diagnostics.Trace.WriteLine("Page_Load " + DateTime.Now.ToString());
    }
    void Page_LoadComplete(object sender, EventArgs e)
    {
        System.Diagnostics.Trace.WriteLine(GridView1.Rows[5].Cells[01].Text, "Row 6, Cell 2");
        System.Diagnostics.Trace.WriteLine("Page_LoadComplete " + DateTime.Now.ToString());
    }
    void Page_PreRender(object sender, EventArgs e)
    {
        ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("MainContent");
        PlaceHolder LabelPlaceHolder = (PlaceHolder)ContentPlaceHolder1.FindControl("LabelPlaceHolder");
        
        GridView GridView1= (GridView)ContentPlaceHolder1.FindControl("GridView1");

        GridViewRow objGridViewRow;

        objGridViewRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

        TableCell objTableCell= new TableCell();

        LiteralControl literal= new LiteralControl();

        literal.Text = "Northwind Products";

        objTableCell.Controls.Add(literal);

        objTableCell.ColumnSpan = 3;

        objTableCell.HorizontalAlign = HorizontalAlign.Center;

        objTableCell.ForeColor = System.Drawing.Color.White;

        objTableCell.BackColor = System.Drawing.Color.Black;

        objGridViewRow.Cells.Add(objTableCell);

        GridView1.Controls[0].Controls.AddAt(0, objGridViewRow);

        System.Diagnostics.Trace.WriteLine("Page_PreRender " +DateTime.Now();
    }
    void Page_PreRenderComplete(object sender, EventArgs e)
    {
        ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("MainContent");
        PlaceHolder LabelPlaceHolder = (PlaceHolder)ContentPlaceHolder1.FindControl("LabelPlaceHolder");
        GridView GridView1 = (GridView)ContentPlaceHolder1.FindControl("GridView1");
        GridView1.HeaderRow.Cells[0].Text = "Product ID";
        GridView1.HeaderRow.Cells[1].Text = "Product Name";
        GridView1.HeaderRow.Cells[2].Text = "Unit Price";
    }
    void Page_SaveStateComplete(object sender, EventArgs e)
    {
        ViewState["Date"] = DateTime.Now.ToString();
        System.Diagnostics.Trace.WriteLine("Page_SaveStateComplete " + DateTime.Now.ToString());
    }
    void Page_Unload(object sender, EventArgs e)
    {
        System.Diagnostics.Trace.WriteLine(ViewState.Values.Count, "ViewState Count");

    }
   
   
}

-----------------------------------------------------------------------------------------
I have used this code in vs2010.

Try using breakpoin with every event . and then debug .
By using Debugger . you can check which event fires first and what can we do with each event..

HOPE THIS HELPS  :)

Thanks

Monday, January 30, 2012

Creating Exe in C# and Deploying sql server with exe

http://balanagaraj.wordpress.com/2007/05/29/create-exe-or-setup-file-in-net-windows-application/

Friday, October 21, 2011

Send email using asp

Create an aspx page and Paste this code on the page and change the required credential on the page..


<%@ Language=VBScript %>
<% Option Explicit %>

<%

Dim strFrom
Dim strPassword
Dim strTo
Dim strSubject
Dim strRedirect
Dim strBody
Dim Field
Dim strSubmit
Dim strServer
Dim strThankyou

'=======================================
'---------------------------------------
'        Inside HTML File IMPUT
'---------------------------------------
'<form name = "mail" action="mail.asp" method="POST">
'---------------------------------------
'=======================================
'---------------------------------------
'        Information to Edit
'---------------------------------------
strFrom = "email@domain.com"
strTo = "email@domain.com"
'strTo = "email@domain.com"
strServer = "mail.domain.com"
strThankyou = "thankyou.htm"
'---------------------------------------
' POP/SMTP Password
'---------------------------------------
strPassword = "******"
'=======================================

'strSubject = Request("Subject")
'strSubject = Request("Title of the Paper Presentation")
strRedirect = Request("Redirect")

If Request.QueryString <> "" Then

    'For Each Field in Request.QueryString
        If LCase(Field) <> "Email" And LCase(Field) <> "to" And _
        LCase(Field)<> "subject" And LCase(Field) <> "redirect" And _
        Lcase(Field)<> "submit" Then
            'strBody = strBody & Field & ": " & Request(Field) & chr(13)+ chr(10)
            strbody=strbody&"Name                            :"&Trim(Request("firstname"))&"  "&chr(13)+chr(10)
            strbody=strbody&"Address                    :"&Trim(Request("address"))&"  "&chr(13)+chr(10)
            strbody=strbody&"E-mail                           :"&Trim(Request("email"))&"  "&chr(13)+chr(10)
            strbody=strbody&"Mobile No           :"&Trim(Request("phone"))&"  "&chr(13)+chr(10)
            strbody=strbody&"Zip / Pin code :"&Trim(Request("zip"))&"  "&chr(13)+chr(10)
            strbody=strbody&"Feedback             :"&Trim(Request("feedback"))&"  "&chr(13)+chr(10)
           
        End If
    'Next
   
Else
   

    'For Each Field in Request.Form
       
        If LCase(Field) <> "Email" And LCase(Field) <> "to" And _
        LCase(Field)<> "subject" And LCase(Field) <> "redirect" And _
        Lcase(Field)<> "submit" Then
            'strBody = strBody & Field & ": " & Request(Field) & chr(13)+ chr(10)
            strbody=strbody&"Name                            :"&Trim(Request("firstname"))&"  "&chr(13)+chr(10)
            strbody=strbody&"Address                    :"&Trim(Request("address"))&"  "&chr(13)+chr(10)
            strbody=strbody&"E-mail                           :"&Trim(Request("email"))&"  "&chr(13)+chr(10)
            strbody=strbody&"Mobile No           :"&Trim(Request("phone"))&"  "&chr(13)+chr(10)
            strbody=strbody&"Zip / Pin code  :"&Trim(Request("zip"))&"  "&chr(13)+chr(10)
            strbody=strbody&"Feedback             :"&Trim(Request("feedback"))&"  "&chr(13)+chr(10)
           
           
        End If
       
    'Next


End If

SendMail strFrom,strTo,strSubject,strBody,strServer,strThankyou,strPassword

%>


<%

Sub SendMail(strFrom,strTo,strSubject,strBody,strServer,strThankyou,strPassword)

On Error Resume Next

Dim Mailer
Set Mailer = server.createobject("CDO.Message")

Mailer.From = strFrom
Mailer.To = strTo
Mailer.TextBody = strBody
Mailer.Subject = "Contact Information"
'Mailer.Subject = request("sub")
With Mailer.Configuration
.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'------------------------------------------------------------------------------
' For SMTP Authentication Modify ('.Fields) to (.Fields) for three fields Below
'------------------------------------------------------------------------------
'.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = strFrom
'.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strPassword
'------------------------------------------------------------------------------
.Fields.Update
End With

Mailer.Send
Set Mailer = Nothing

If Err.number<>0 Then
    Response.Write Err.Description
    Response.End
End If

Response.Redirect str Thankyou

End Sub

%>