Thursday, February 17, 2011

Sorting / Paging on Asp.Net GridView without a DataSourceControl DataSource

If we set AllowPaging="true" or AllowSorting="true" on a GridView control without using a DataSourceControl DataSource (i.e. SqlDataSource, ObjectDataSource),
we will run into the following errors:

When changing the page on the GridView control:

"The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled."

When clicking a column name to sort the column on the GridView control:

"The GridView 'GridViewID' fired event Sorting which wasn't handled."

As a result of not setting the DataSourceID property of the GridView to a DataSourceControl DataSource, we have to add event handlers for sorting and paging.


private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;

switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;

case SortDirection.Descending:
newSortDirection = "DESC";
break;
}

return newSortDirection;
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = gridView.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

gridView.DataSource = dataView;
gridView.DataBind();
}
}

How to read the binding data record of the current row in Asp.Net GridView


You can get the binding data record using following code example:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string name= DataBinder.Eval(e.Row.DataItem, "StudentName") as string;
if (name!= null && name.Length > 0)
{

}
}
}

Enable/Disable DIV tag and its inner controls using Javascript

JavaScript Code:

function toggleAlert() {
toggleDisabled(document.getElementById("content"));
}
function toggleDisabled(el) {
try {
el.disabled = el.disabled ? false : true;
}
catch(E){
}
if (el.childNodes && el.childNodes.length > 0) {
for (var x = 0; x < el.childNodes.length; x++) {
toggleDisabled(el.childNodes[x]);
}
}
}


Html Code:

<div id="content">
<table>
<tr>
<td><input type="text" name="foo" /></td>
</tr>
<tr>
<td>
<select name="bar">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
</td>
</tr>
</table>
</div>
<input type="checkbox" value="toggleAlert()" onclick="toggleAlert()" />

Monday, October 19, 2009

Sending email in asp.net 3.5

Sending email is a very important in web application for remind anything to user, newsletter ,invitation or reset password etc.In .Net 1.1 we used System.Web.Mail.SmtpMail for sending emails , which is now obsolete. Now in asp.net 2.0 or higher version we can use System.Net.Mail.Smtpmail to send mail.


Code for sending email in asp.net 3.5

SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("from@site.com","Nameofsendingperson");
message.From = fromAddress; //here you can set address
message.To.Add("to@abc.com"); //here you can add multiple to
message.Subject = "Subject Goes Here"; //subject of email
message.Body =@"Body Text";
smtpClient.Send(message);
}
catch (Exception ex)
{
//throw exception here you can write code to handle exception here
}


For adding attachment with your mail you need to write following code

message.Attachments.Add(New System.Net.Mail.Attachment(Filename))

Here Filename will be the physical path along with the file name. You can attach multiple files with the mail.


If you want to sent a email with authentication then you have to write following code.

System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
//This object stores the authentication values
System.Net.NetworkCredential basicCrenntial =
new System.Net.NetworkCredential("username", "password");
mailClient.Host = "Host";
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicCrenntial;
mailClient.Send(message);

Saturday, October 17, 2009

Three major points in WCF


1) Address --- Specifies the location of the service which will be like http://Myserver/MyService.Clients will use this location to communicate with our service.

2) Contract --- Specifies the interface between client and the server. It’s a simple interface with some attribute.

3) Binding --- Specifies how the two parties will communicate in term of transport and encoding and protocols.

WCF

Windows Communication Foundation is a set of .NET technologies for building and running connected systems. It is a new breed of communications infrastructure built around the Web services architecture.



Windows Communication Foundation is Microsoft's unified programming model for building service-oriented applications with managed code. It extends the .NET Framework to enable developers to build secure and reliable transacted Web services that integrate across platforms and interoperate with existing investments. Windows Communication Foundation combines and extends the capabilities of existing Microsoft distributed systems technologies, including Enterprise Services, System.Messaging, Microsoft .NET Remoting, ASMX, and WSE to deliver a unified development experience across multiple axes, including distance (cross-process, cross-machine, cross-subnet, cross-intranet, cross-Internet), topologies (farms, fire-walled, content-routed, dynamic), hosts (ASP.NET, EXE, Windows Presentation Foundation, Windows Forms, NT Service, COM+), protocols (TCP, HTTP, cross-process, custom), and security models (SAML, Kerberos, X509, username/password, custom).

Friday, October 16, 2009

Timer on Windows Service in Visual Studio 2008


We do not use the timer System.Windows.Forms.Timer that is in the Toolbox during adding a timer to the Windows Service. This doesn’t work for Windows Services. We will have to use the System.Timers.Timer instead of System.Windows.Forms.Timer. And the event we will use Timer1_Elapsed instead of Timer1_Tick.