Friday, June 1, 2012

How to Replace the First Instance of a String in C#

Assume that you have a string and you expect that this string might have some duplicated instances.

String.Replace() is very good Function, but it will replace all the instances of the string. So to accomplish this you need to replace the first instance you find.

The next code example can be used as a helper method in your project if you are dealing with strings frequently:

public static string ReplaceFirst(string str, string term, string replace)
{
    int position = str.IndexOf(term);

    if (position < 0)
    {
        return str;
    }

    str = str.Substring(0, position) + replace + str.Substring(position + term.Length);

    return str;
}

Source: https://www.nilebits.com/blog/2011/05/how-to-replace-the-first-instance-of-a-string-in-c-net/

Tuesday, May 1, 2012

How to Programmatically create iFrame in ASP.NET

Sometimes we may need to create iFrames and use it within GridView or Repeater, and we want to pass some values to it.
This example will show you how to create iFrame Programmatically and add it to a PlaceHolder.

HtmlGenericControl myFrame = new HtmlGenericControl();

myFrame.TagName = "IFRAME";
myFrame.Attributes["src"] = "MyPagePath";
myFrame.Attributes["id"] = "myFrame1";
myFrame.Attributes["name"] = "myFrame1";
myFrame.Attributes["width"] = "500";
myFrame.Attributes["height"] = "500";
myFrame.Attributes["class"] = "frames";

myPlaceHolder.Controls.Add(myFrame);

Source: https://www.nilebits.com/blog/2011/04/how-to-programmatically-create-iframe-in-asp-net/

Monday, April 23, 2012

Operation is not valid due to the current state of the object, Exception in ASP.NET

I got this error when I tried to save a Page with lots of form fields to SQL Server Database.
The default max number of form fields that can be post is 1000.
In order to solve this error add this Line to your Web.Config File:

<appSettings>
  <add key="aspnet:MaxHttpCollectionKeys" value="10000" />
</appSettings>

Source: https://www.nilebits.com/blog/2011/03/operation-is-not-valid-due-to-the-current-state-of-the-object-exception-in-asp-net/

Thursday, March 1, 2012

How to Convert SqlDataSource to DataTable and DataView in C#

Sometimes I want to use SqlDataSource  returned data programmatically and here is how you can do this,

In this example I will assume that SqlDataSource  ID is SqlDataSource1 which is the default ID:
DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView view = SqlDataSource1.Select(args) as DataView; 
DataTable dt = view.ToTable();

Source: https://www.nilebits.com/blog/2011/02/how-to-convert-sqldatasource-to-datatable-and-dataview-in-c-net/

Wednesday, February 15, 2012

How to Submit a Form Using JavaScript

Any form is submitted when the user click on the submit button.
But you may need to submit the form programmatically using JavaScript.
JavaScript has the form object that contains the submit() method.
Use the "ID" of the form to get the form object.
Example:
If the name of the form is "Form1", JavaScript code for the submit method is:
document.forms["Form1"].submit();

Source: https://www.nilebits.com/blog/2011/01/how-to-submit-a-form-using-javascript/

Sunday, January 1, 2012

Javascript Regular Expression to match a comma delimited list

This method uses the JavaScript String's match() method to parse a comma delimited list and show its content in a Panel control.

You can try it here http://jsfiddle.net/38tXU/
function parseList(list) {
    var reg = list.match(/\w+(,\w+)*/g);
    document.getElementById('pnl').innerHTML = 'Total length:' + reg.length + '<br />';
    for (var i = 0; i < reg.length; i++) {
        document.getElementById('pnl').innerHTML += 'Token:\'' + reg[i] + '\'<br />';
    }
}
//Call parseList function
parseList('item1, item2, item3');

Source: https://www.nilebits.com/blog/2010/12/javascript-regular-expression-to-match-a-comma-delimited-list/