Thursday, July 3, 2008

Event and Delegates -Capture Master Page Events in Content Pages

In This article, I explain how to use event and delegate to capture Master page event at content page.
Case :The master page (MasterPage.master.cs) Content drop down list .When The dropdown selected index change the selected value must be display in content page labels.
How to Do:
1. we develop master page as usual .Then top of code behind page of the master page, we have to declare Delegate and also have to declare public event in the class which type of previous delegate .Next step is declaring virtual method that used to handle the event ,Then we have to call the above method at the event of controller(drop down list ).
2.Develop content page and put following code at the top of .aspx page
3. In the code behind file of content page we have to write method capture to the master page event and in the page load event we have to add event handaler to call .
MasterPage.master.cs
This is Master Page with content Drop down list

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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>Master test</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 158px; height: 414px; background-color: gray">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="150px">
<asp:ListItem Value="a"></asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
</div>
<div style="left: 168px; width: 638px; position: absolute; top: 17px; height: 410px">
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>

class MasterPage
Then Put following codes in page behined file of the master page

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//declare delegates for Master Page Events
public delegate void mstDelegate(object sender, System.EventArgs e);
public partial class MasterPage : System.Web.UI.MasterPage
{

private string slectedValue;
public string SlectedValue
{
get { return slectedValue; }
set { slectedValue = value; }
}
// declare Public Events type of above delegate
public event mstDelegate DropDownEvent;
protected void Page_Load(object sender, EventArgs e)
{

}
// impliment virtual method to handel above event
protected virtual void OnDropDownClick(EventArgs e)
{
if (DropDownEvent != null)
{
//Invokes the delegates.
DropDownEvent(this, e);
}
}
// DropDownList1_SelectedIndexChanged event
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SlectedValue = DropDownList1.SelectedValue;
OnDropDownClick(e);
}

}

Create Test page
testevent.aspx

<%@ MasterType virtualPath="~/MasterPage.master"%>
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="testevent.aspx.cs" Inherits="testevent" Title="test Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="lblOutput" runat="server" Text="Label" Width="244px"></asp:Label>
</asp:Content>
Code behind file of test page
Testevent.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class testevent : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
// create instant of Master event
Master.DropDownEvent += new mstDelegate(mstDDLClick);

}
/// method for catch Master Page menu click event
void mstDDLClick(object sender, EventArgs e)
{
lblOutput.Text = Master.SlectedValue;
}

}

Monday, June 2, 2008

Set a Date Format In GridView Using ASP.NET

Following code explain How to format date in ASP.Net Gride view


< asp :GridView ID=“GridView1″ runat=“server” >
< columns >
< asp :BoundField DataField=“CreationDate”
DataFormatString=“{0:dd-MM-yyyy}”
HtmlEncode=“false”
HeaderText=“CreationDate” />
< /columns >
< /asp >

Monday, April 21, 2008

Passing Parameters to Crystal Reports

It is seam to be passing parameters to crystal reports (without prompting Enter to parameter values) programmatically some what confusing. So in this article I’ll explain Step by step “how to pass parameters to crystal reports”. We need to import following namespace.
using CrystalDecisions.CrystalReports.Engine;

Then following code pass the parameter value to crystal reports.

protected void Page_Load(object sender, EventArgs e){
this.CrystalReportViewer1.ParameterFieldInfo.Clear();
ParameterDiscreteValue desValue1= new ParameterDiscreteValue();
desValue1.Value = "Testing";// set value for parameter
ParameterField paraFld = new ParameterField();
paraFld.Name = "User";// this is parameter name
paraFld.CurrentValues.Add(desValue1);
paraFld.HasCurrentValue = true;
this.CrystalReportViewer1.ParameterFieldInfo.Add(paraFld);
this.CrystalReportViewer1.ShowFirstPage();
}

If we have more than two parameters it is bit difficult .So we can write common Method to passé parameters to crystal report as follow.

Public Void SetParameter(string ParaName ,string ParaValue)
{
ParameterDiscreteValue desValue= new ParameterDiscreteValue();
desValue. Value= ParaValue;
ParameterField paraFld = new ParameterField();
paraFld.Name = ParaName;
paraFld.CurrentValues.Add(desValue);
paraFld.HasCurrentValue = true;
CrystalReportViewer1.ParameterFieldInfo.Add(paraFld);

}

By using above method we can pass any number of parameters very easily.
As following (Think, ReportName and Createdby are two parameters which we defined in crystal reports) Then

protected void Page_Load(object sender, EventArgs e)
{
this.CrystalReportViewer1.ParameterFieldInfo.Clear();
SetParameter(“ReportName”,”My First Report”);// SetParameter(“Createdby”,”Dinesh”);
this.CrystalReportViewer1.ShowFirstPage();

}

}

Saturday, January 12, 2008

Log4j

Inserting log statements into your code is a low-tech method for debugging it. It may also be the only way because debuggers are not always available or applicable. This is often the case for distributed applications.

With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Logging behavior can be controlled by editing a configuration file, without touching the application binary.
so first we need to download log 4j jar file and put it into lib directory .
http://logging.apache.org/log4j/1.2/download.html"

Configure Log4j with Xml file
To configure log4j only we need to copy following log4j.xml file to src folder of project. And edit the location which the log message should be write as
< param name="File" value="../webapps/myporject/info.log > in the log4j.xml file .

log4j.xml

< ? xml version="1.0" encoding="UTF-8" ?>
< !DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
< log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j / ' >

< appender name="STDOUT" class="org.apache.log4j.ConsoleAppender" >
< layout class="org.apache.log4j.PatternLayout">
< param name="ConversionPattern" value="%p-- %c %d{ISO8601}-- %m%n" / >
< /layout>
< /appender>

< appender name="INFO" class="org.apache.log4j.RollingFileAppender">
< param name="File" value="../webapps/myporject/info.log"/> // the location and file name
< param name="Append" value="true" / >
< param name="MaxFileSize" value="5000KB" / >
< param name="MaxBackupIndex" value="2"/>
< layout class="org.apache.log4j.PatternLayout">
< param name="ConversionPattern" value=" %p --%c-- %d{ISO8601} -- %m%n" / >
< /layout>
< filter class="org.apache.log4j.varia.LevelRangeFilter" >
< param name="LevelMin" value="INFO" / >
< /filter>
< /appender>

< appender name="DEBUG" class="org.apache.log4j.RollingFileAppender >
< param name="File" value=" value="../webapps/myporject/logs/debug.log"/ >
< param name="Append" value="true"/>
< param name="MaxFileSize" value="5000KB"/ >
< param name="MaxBackupIndex" value="2"/ >
< layout class="org.apache.log4j.PatternLayout" >
< param name="ConversionPattern" value=" %p--%c %d{ISO8601} -- %m%n"/ >
< /layout>
< filter class="org.apache.log4j.varia.LevelRangeFilter" >
< param name="LevelMin" value="DEBUG" / >
< /filter>
< /appender>

< appender name="FATAL" class="org.apache.log4j.RollingFileAppender" >
< param name="File" value=" value="../webapps/myporject/logs/fatal.log"/ >
< param name="Append" value="true"/ >
< param name="MaxFileSize" value="5000KB"/ >
< param name="MaxBackupIndex" value="2"/ >
< layout class="org.apache.log4j.PatternLayout">
< param name="ConversionPattern" value=" %p--%c %d{ISO8601} -- %m%n"/ >
< /layout>
< filter class="org.apache.log4j.varia.LevelRangeFilter" >
< param name="LevelMin" value="FATAL" / >
< /filter>
< /appender>

< appender name="WARN" class="org.apache.log4j.RollingFileAppender" >
< param name="File" value="../webapps/myporject/logs/warn.log"/ >
< param name="Append" value="true"/ >
< param name="MaxFileSize" value="5000KB"/ >
< param name="MaxBackupIndex" value="2"/>
< layout class="org.apache.log4j.PatternLayout" >
< param name="ConversionPattern" value=" %p--%c %d{ISO8601} -- %m%n"/ >
< /layout>
< filter class="org.apache.log4j.varia.LevelRangeFilter" >
< param name="LevelMin" value="WARN" />
< /filter>
< /appender>

< appender name="ERROR" class="org.apache.log4j.RollingFileAppender" >
< param name="File" value="../webapps/myporject/logs/error.log"/ >
< param name="Append" value="true"/ >
< param name="MaxFileSize" value="5000KB"/ >
< param name="MaxBackupIndex" value="2"/ >
< layout class="org.apache.log4j.PatternLayout">
< param name="ConversionPattern" value="%p--%c %d{ISO8601} -- %m%n"/ >
< /layout>
< filter class="org.apache.log4j.varia.LevelRangeFilter">
< param name="LevelMin" value="ERROR" / >
< /filter>
< /appender>

< root>
< priority value="ALL"/>
< appender-ref ref="DEBUG"/>
< appender-ref ref="FATAL"/>
< appender-ref ref="INFO"/>
< appender-ref ref="WARN"/>
< appender-ref ref="ERROR"/>
< /root>

< /log4j:configuration>



Put log messages in the Class
After put above file to src folder in the project.Then we can use log4j as following code in the progrmme.

import org.apache.log4j.Logger;

class testlog()
{
Logger log=new Logger(testlog.class);
log.Infor("<>>>");// this simply print in specify
//locationwebapps/myporject/info.log
}