Monday, April 14, 2008

Highlight Gridview Row when checbox is checked

First thing create Database (Company) with Table (Employee)
which has fields (Id,Name)

Second enter data in Database

Now create new website


In Default.aspx add Gridview and in Gridview add Template field
and in ItemTemplate add checkbox(cbxId)

and in Default.aspx.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;
using System.Text;

public partial class SelectGridview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

//-- for Highlight
if ((e.Row.RowType == DataControlRowType.DataRow))
{
StringBuilder script = new StringBuilder();
script.Append("if (this.checked) {");
script.Append("document.getElementById('");
script.Append(e.Row.ClientID);
script.Append("').style.backgroundColor='Blue';");
script.Append("} else {");
script.Append("document.getElementById('");
script.Append(e.Row.ClientID);
script.Append("').style.backgroundColor='';");
script.Append("}");
((CheckBox)e.Row.FindControl("cbxId")).Attributes.Add("onclick", script.ToString());

}

}
}

Send Email Using (System.Net.Mail)

First create new website project and add two pages :-
SendEmail.aspx and EmailSent.aspx

and in the page SendEmail.aspx add three TextBox (txtFrom , txtSubject , txtMessage)
and add button (Send) and Lable control
and Required Validator for txtFrom , txtSubject and also add CustomValidator control for txtMessage as we want the characters in txtMessage not more than 50 characters



In SendEmail.aspx.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;
using System.Net.Mail;

public partial class Default2 : System.Web.UI.Page
{
private bool SendMail(string from, string body,string subject)
{
//string mailServerName = "SMTP.ewebbers.com";
string mailServerName = "SMTP.ewebbers.com";
MailMessage message = new MailMessage(from, "y.ahmed@ewebbers.com",subject, body);
SmtpClient mailClient = new SmtpClient();
mailClient.Host = mailServerName;
mailClient.Send(message);
message.Dispose();

return true;
}

protected void Page_Load(object sender, EventArgs e)
{

}

protected void btn_Send_Click(object sender, EventArgs e)
{
bool x=SendMail(txtFrom.Text, txtMessage.Text,txtSubject.Text);
if (x == true)
{
Response.Redirect("~/MailSent.aspx");
}
else
{
Label1.Text = "Try again";
}
}

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (txtMessage.Text.Length > 50)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}

}
}


Note:- we can use javascript with CustomValidator to ensure that user not enter more than 50 characters in txtMessage
so we will add Javascript code in SendEmail.aspx page

// function ValidateComments(sender, args)
// {
// if (args.Value.length > 50)
// args.IsValid = false;
// else
// args.IsValid = true;
// }


and in property of CustomValidator control set ClientValidationFunction="ValidateComments"

Friday, April 4, 2008

Delete Checked in Gridview

First thing create Database (Company) with Table (Employee)
which has fields (Id,Name)

Second enter data in Database

Now create new website

in Default.aspx

write the following javascript code

//function SelectAll(id)
// {
// var frm = document.forms[0];
//
// for (i=0;i < frm.elements.length;i++)
// {
// if (frm.elements[i].type == "checkbox")
// {
// frm.elements[i].checked = document.getElementById(id).checked;
//
// }
// }
//
// }

and add Gridview and button (Delete)and lable (lblMsg)
and in Gridview add Template field and In HeaderTemplate add checkbox (cbxAll) and in ItemTemplate checkbox (cbxId)

and in Default.aspx.cs

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.Header))
{
//adding an attribut for onclick event on the check box in the hearder and passing the ClientID of the Select All checkbox
((CheckBox)e.Row.FindControl("cbxAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("cbxAll")).ClientID + "')");
}
}

protected void btn_Delete_Click(object sender, EventArgs e)
{
try
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox checkbox = (CheckBox)row.FindControl("cbxId");

if (checkbox.Checked == true)
{
// method to delete by Id

lblMsg.Text = "Selected deleted";
GridView1.DataBind();


}

}
}
catch (Exception ex)
{
lblMsg.Text = ex.Message.ToString();
}
}