Tag - C++

How to convert DataRow to DataRowView

Converting DataRow to DataRowView is not possible, but we can go around this by using DefaultView property of the Data Row Table. The following code example demonstrate how to get DataRowView out of DataRow: DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Name", typeof(string)); for (int i = 0; i < 10; i++) { DataRow tempDR = dt.NewRow(); tempDR["ID"] = (i...

How to remove HTML tags from string in C#

I will show you three different methods to remove HTML tags from string in C#: 1. By using Regex: public static string RemoveHTMLTags(string html) { return Regex.Replace(html, "<.*?>", string.Empty); } 2. By using Compiled Regex for better performance: static Regex htmlRegex = new Regex("<.*?>", RegexOptions.Compiled); public static string RemoveHTMLTagsCompiled(string html) { return htmlRegex.Replace(html, string.Empty); } 3. By using Char Array for...

How to Access controls on Master Pages from Content Pages in ASP .NET

We will assume that we have a Label control called "Label1" on your Master Page, you might want to change the text of that Label.To do that write this code in your content page: Label lbl = new Label(); lbl = (Label)Master.FindControl("Label1"); lbl.Text = "Text"; Of course, this is all assuming the label is OUTSIDE the ContentPlaceHolder

Cross Page Postback in ASP.NET

This is a new feature in ASP .NET 2.0. The IButtonControl Interface contains a new property called PostBackUrl which points to the page to which the current page will postback, Button, ImageButton and LinkButton implements this interface and exposes the cross page postback functionality. When user clicks the button in the current page will postback...

Export to excel in ASP.NET using C#

In this article we are going to show two examples of exporting to excel. First one is how to export grid view control to excel and the second one is how to export data table to excel. 1-Export Grid View control to Excel: HtmlForm htmlForm = new HtmlForm(); string fileName = "attachment; filename=Reports.xls"; Response.ClearContent(); Response.AddHeader("content-disposition", fileName); Response.ContentType = "application/ms-excel"; StringWriter strw...

Send email using C#

We are going to use Gmail outgoing mail server in our code here to demonstrate how you can send an email from your application using C# SmtpClient oSmtpClient = new SmtpClient(); oSmtpClient.Host = "smtp.gmail.com"; //The Outgoing mail server oSmtpClient.Credentials = new NetworkCredential("Your Email", "Your password"); oSmtpClient.Port = 587; oSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; oSmtpClient.EnableSsl = true; MailMessage msg = new MailMessage("Email From", "Email...

Captcha image in C#.NET

What Captcha stand for? Completely Automated Public Turing test to tell Computers and Humans Apart. The Captcha technology help you to make sure your site is reasonably secure against automated attacks. Write the following code in a class named Captcha: public class Captcha { //make the captcha image for text public Bitmap MakeCaptchaImage(string txt, int width, int hight,...

Make previous calendar dates not selectable in ASP.NET

In order to make all dates before the current date, not able to be selected, in the onDayRender event for your Calendar: if (e.Day.Date < DateTime.Today) { e.Day.IsSelectable = false; } To make it more obvious to the end user, also add: e.Cell.BackColor = Drawing.Color.GhostWhite; e.Cell.ForeColor = Drawing.Color.Gainsboro;

Get the referring page on Page_Load event in ASP.NET

When a page loads, in order to get the name of the page that sent you there, all you need to use is: Request.UrlReferrer.ToString(); You can create a global variable to hold it: string sReferrer = ""; Then, in the Page_Load event, assign it: if (!Page.IsPostback) { sReferrer = Request.UrlReferrer.ToString(); } Or, you can put it in ViewState at...