Showing posts with label C# Code. Show all posts
Showing posts with label C# Code. Show all posts

Thursday, September 27, 2012

Remove HTML tags from string in C#

Below is the sample code. The string contains HTML tags like below:


HTML Code:


.CS Code
protected void btnTest_Click(object sender, EventArgs e)
{
string strHtml = "
Hello World

Br Should not come.But what about this? Will this | will come? ) ";

Regex regex = new Regex("<[^>]*>");

string strWithOutHTMLTag = regex.Replace(strHtml, "");

lblResult.Text = strWithOutHTMLTag;
}

The output will come as:
"Hello World Br Should not come.But what about this? Will this | will come? )"

Thursday, December 9, 2010

FCK Editor not working with Update Panel

There is compatibility issue of FCK Editor with Update Panel, because both use AJAX plugin. To run it properly we need to register FCK Editor on page load.

function WebForm_OnSubmit()
{
for ( var i = 0; i < parent.frames.length; ++i ) if ( parent.frames[i].FCK ) parent.frames[i].FCK.UpdateLinkedField(); (function() { var editor = FCKeditorAPI.GetInstance('master_Content1_fckEditor1'); if (editor) editor.UpdateLinkedField(); })(); return true; }


But problem is that, if we have multiple number of FCK Editor, then we have to register all FCK Editor. To solve this issue, I discovered a simple C# code, which will find all FCK Editor present inside Update Panel & register them all automatically.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FCKEditor();
}
}
private void FCKEditor()
{
this.Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "AjaxHack", "for ( var i = 0; i < parent.frames.length; ++i ) if ( parent.frames[i].FCK ) parent.frames[i].FCK.UpdateLinkedField();"); }


Happy coding!!!

Friday, September 24, 2010

How to make FileUpload control in ASP.Net to Read-Only?

We can make the ASP.Net FileUpload control readonly by setting the ContentEditable property to false.
<asp:FileUploadID="fuFile"ContentEditable="false"runat="server"/>

 The other way of achieving it by restricting users to type in any characters i.e. return false on key press, key up and on paste event to prevent users pasting any values directly.

Refer the below code snippet that helps in doing that,
        fuFile.Attributes.Add("onkeypress", "return false;");
        fuFile.Attributes.Add("onkeyup", "return false;");
        fuFile.Attributes.Add("onpaste", "return false;");

Monday, September 20, 2010

Cookies

Here are the function to add/update, remove, get cookies.

public void SetCookie(string key , string value)
{
HttpCookie myCookie = new HttpCookie(key);
//DateTime now = DateTime.Now; // Uncomment to add expiration time
// Set the cookie value.
myCookie.Value = value;
// Set the cookie expiration date.
//myCookie.Expires = now.AddDays(1); // Uncomment to add expiration time
// Add the cookie.
System.Web.HttpContext.Current.Response.Cookies.Add(myCookie);
}
public void RemoveCookie(string key)
{
if (System.Web.HttpContext.Current.Request.Cookies[key] != null)
{
HttpCookie myCookie = new HttpCookie(key);
myCookie.Expires = DateTime.Now.AddDays(-1d);
System.Web.HttpContext.Current.Response.Cookies.Add(myCookie);
}
}
public string GetCookie(string key)
{
if (System.Web.HttpContext.Current.Request.Cookies[key] != null)
return System.Web.HttpContext.Current.Request.Cookies[key].Value;
else
return "";
}

If you want to store multiple values in a single cookie, use the following functions.

public void SetCookie(string key, string value)
{
if (!System.Web.HttpContext.Current.Response.Cookies["myCookie"].HasKeys)
{
HttpCookie cookie = new HttpCookie("myCookie");
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}

//cookie.Values.Add(key, value);
System.Web.HttpContext.Current.Response.Cookies["myCookie"][key] = value;
System.Web.HttpContext.Current.Response.Cookies.Add(System.Web.HttpContext.Current.Response.Cookies["myCookie"]);
}

public void RemoveCookie(string key)
{
if (System.Web.HttpContext.Current.Response.Cookies["myCookie"].HasKeys)
{
System.Web.HttpContext.Current.Response.Cookies["myCookie"].Values.Remove(key);
}
}
public string GetCookie(string key)
{
if (System.Web.HttpContext.Current.Request.Cookies["myCookie"] != null)
{
if (System.Web.HttpContext.Current.Request.Cookies["myCookie"].HasKeys)
{
return System.Web.HttpContext.Current.Request.Cookies["myCookie"][key];
}
else
{
return "";
}
}
else
{
return "";
}
}

Saturday, June 26, 2010

Downloading a File with a Save As Dialog in ASP.NET

How to download a file from a Web site, but instead of displaying it in the browser see it as a file that can be saved (ie. see the Save As dialog)?

string fileName = "logo.png";
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.TransmitFile(Server.MapPath("~/images/" + fileName));
Response.End();

This will cause a Open / Save As dialog box to pop up with the filename of SailBig.jpg as the default filename preset.


If at all possible though, use TransmitFile though especially if you plan on serving a file more than once. TransmitFile is very efficient because it basically offloads the file streaming to IIS including potentially causing the file to get cached in the Kernal cache (based on IIS's caching rules).

Friday, October 2, 2009

How to retrieve processorID,Motherboard serial number and MAC address of a PC in C#.net


Add using System.Management; namespace in your application (You can find it in :
Reference -> Add reference -> .NET tab -> System.management).

using System.Management;

On Page_Load Event add the following code:-

ManagementObjectSearcher MOS = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
foreach (ManagementObject getserial in MOS.Get())
{
ltrSerialNumber.Text = getserial["SerialNumber"].ToString();
// Listout all information
GridView1.DataSource = getserial.Properties;
GridView1.DataBind();
}

//Code for retrieving Processor's Identity
MOS = new ManagementObjectSearcher("Select * From Win32_processor");
foreach (ManagementObject getPID in MOS.Get())
{
ltrProcessorID.Text = getPID["ProcessorID"].ToString();
// Listout all information
GridView2.DataSource = getPID.Properties;
GridView2.DataBind();
}

//Code for retrieving Network Adapter Configuration
MOS = new ManagementObjectSearcher("Select * From Win32_NetworkAdapterConfiguration");
foreach (ManagementObject mac in MOS.Get())
{
ltrMACAddress.Text = mac["MACAddress"].ToString();
// Listout all information
GridView3.DataSource = mac.Properties;
GridView3.DataBind();
}