Archive

Posts Tagged ‘c#’

Truncate Strings At Complete Words without chopping words half way In C#, Asp.net

August 30, 2010 1 comment

There is always a common need to display n number of characters of a string and then….
But it regular practice we lose the characters displaying incomplete word.
For Eg. This is an example of long string
Now what happens when we use substring(..), the result will be
This is an example o…
It left the word “of” incomplete, chopping off the words in half way. To avoid below are the solutions to it.

public static string TruncateWithoutChopping(this string strText, int intLength)
{
    if (strText == null || strText.Length < intLength)
        return strText;
    int intNextSpace = strText.LastIndexOf(" ", intLength);
    return string.Format("{0}...", strText.Substring(0, (intNextSpace > 0) ? intNextSpace: strText).Trim());
}

Another Extension method is as follows:

public static string TruncateWithoutChopping (this string strText, int intLength)
  {
    if(String.IsNullOrEmpty(strText))
      throw new ArgumentNullException(s);
    var words = strText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    var sbText = new StringBuilder();

    foreach (var word in words.Where(word => (sbText.ToString().Length + word.Length) <= intLength))
    {
      sbText.Append(word + " ");
    }
    return sbText.ToString().TrimEnd(' ') + "...";
  }

Advertisements

Using Dataview RowFilter (Syntax) in Asp.net C#

February 8, 2010 1 comment

Lets say we have a datatable, we want to select rows that meet certain criteria, then we can use Dataview.Rowfilter.

For example we have a datatable having empid column, we want the row whose empid is 8.

dataView.RowFilter = "empid = 8";

Other examples are
dataView.RowFilter = "EmpName = 'Pankaj'" ;
dataView.RowFilter = "Date = #12/08/2000#" ;

Comparision
dataView.RowFilter = "Date>=" + "#" + startdate + "#" +
"And Date <=" + "#" + enddate + "#";
You can find other examples at:
http://www.csharp-examples.net/dataview-rowfilter/

Validator Enable/Disable in Asp.net using Javascript

February 3, 2010 Leave a comment

You can enable/disable validators using javascript as follows:

ValidatorEnable(document.getElementById(‘<%= rfv.ClientID %>’), false);

First argument is id of validator, second argument is to enable pass “true”, else pass “false”.

Get Current Page Name In Asp.net, C# codebehind

December 28, 2009 1 comment

Below method will give current page name in C#, asp.net Code behind:

/// <summary>
/// Pankaj lalwani
/// GetCurrentPageName
/// </summary>
/// <returns>Current Page name</returns>
public static string GetCurrentPageName()
{
string sPath = HttpContext.Current.Request.Url.AbsolutePath;
string[] strarry = sPath.Split(‘/’);
int lengh = strarry.Length;
string sRet = strarry[lengh – 1];
return sRet;
}

Calendar Extender and Textbox set to Read only in Asp.net – C#

November 30, 2009 Leave a comment

Lets say we have a textbox control and calendar extender associated with it.Now if we want to set the textbox to read only mode, we set Readonly = true

But there is a problem with it.

Setting the readonly atrribute as stated here will not cause any serverside processing to occur. If you need the value in the textbox to be available serverside then use the attribute

contentEditable="false" 

rather than read only. This only works for IE browsers however.

so the code becomes.

<asp:TextBox ID=”txtDate” contentEditable=”false” runat=”server” CssClass=”txtcss”></asp:TextBox>&nbsp;
<asp:Image ID=”imgCal” runat=”server” ImageUrl=”~/img/calendar.gif” />
<cc1:CalendarExtender ID=”CalExtDate” runat=”server”  PopupButtonID=”imgCal”
PopupPosition=”BottomLeft” Format=”dd/MM/yyyy”    TargetControlID=”txtDate”>
</cc1:CalendarExtender>

Using String.Format(“{0}”, “formatting string”}; in Asp.net

September 9, 2009 Leave a comment

Many times we want to define formats for date, currency, number etc

For example if we want date in

” 09 September 2009″  – String.Format{0:D}

” 09 September 2009 12:30″  – String.Format{0:f}

Like this various formats for date, month, year, time, currency number etc can be seen at :

http://bit.ly/cctp

Limiting the number of records to be displayed in Crystal Report

July 31, 2009 11 comments

You might be working with crystal reports, and so must have a noticed that number of records displayed in crystal report are variable, sometimes, it displays 8, sometimes 10.. and so on according to the data, it sets it accordingly.

But we can handle this scenario i.e we can specify how many records we want the crystal report to display. For this you need to follow the below instructions:

To make it show 10 records per page do the following

1. Open the report in Design View

2. Right click on the Details section and select Section Expert

3. Make sure the Details section is selected in the Section Expert dialog box. Check the box that says “New Page After”

4. Click the formula editor button to the right of the checkbox.

5. Enter the following formula

if Remainder (RecordNumber, 10) = 0 then true else false

6. Click Save and Close and then click OK.

If you run the report it should break after each 10 rows.