Showing posts with label Asp.net/C# Information. Show all posts
Showing posts with label Asp.net/C# Information. Show all posts

Monday, 2 December 2024

How to Create a Generic Method to Get the Enum Description in C#

        public static SelectList GetSelectListForEnumWithDescription(Type enumType)
        {
            var list = new List<Object>();
            Array enumValues = Enum.GetValues(enumType);
         
            foreach (Enum enumValue in enumValues)
            {
                list.Add(new
                              {
                                 ID = (int)Enum.Parse(enumType, enumValue.ToString()),
                                 Value = GetEnumDescription(enumValue)});
                               }
              return new SelectList(list, "ID", "Value");
        }

        public static string GetEnumDescription(Enum value)
        {

            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false);

            if (attributes != null && attributes.Length > 0)

                return attributes[0].Description;

            else return value.ToString();
        }
    }

Monday, 22 April 2013

Reading a CSV file with Linq


A simple single line of code  to read the CSV file in Linq:

rawFile = (from line in File.ReadAllLines(fileName).AsParallel()
            select line.Split(',')).ToList();

Happy Coding!

Monday, 5 November 2012

Issue with the sorting the telerik grid for Asp.net MVC

While working on telerik grid , I found a peculiar issue in Internet Explorer(IE) browsers. The issue is that on the column bound HeaderTemplate if you are providing a link for sorting purpose and if you used a label inside the link then the link will not work in IE but will work for all other browsers.


Solution: So, while providing the link text for sorting the grid on that column, just be care full that u must not used any label for that link. See below:

The code below will  not work:
column.Bound(m => m.Name).Template(
        @<text>
    <a target="_blank" href="@item.Url">@item.Name</a>
    </text>) .HeaderTemplate(
        @<text><a href="javascript:void(0)" class="t-link"> <label>Name<label> </a>  ---> This will not work
    </text>);



The code below will work:
 column.Bound(m => m.Name).Template(
        @<text>
    <a target="_blank" href="@item.Url">@item.Name</a>
    </text>) .HeaderTemplate(
        @<text><a href="javascript:void(0)" class="t-link"> Name </a>  ---> This will work.
    </text>);



Enjoy!.......

Friday, 26 October 2012

How to Minimize Long Title or Overflowed text with less text followed with ellipsis using Jquery or CSS


While working on a project I got a requirement  of showing a limited amount of text followed with ellipsis(...)  in  a telerik grid, to do it I thought I will use the CSS property  text-overflow: ellipsis; but unable to succeed. I thought a lot and at last done it with CSS.

What I had done with CSS:
So in order to get the text as "Hello Hai how Are..." I used the following CSS

#divContainer td:first-child + td {height: 45px;width:280px;max-width: 280px;overflow: hidden;
text-overflow: ellipsis; display:block;white-space: nowrap;}

Here divContainer is the Container and td:first-child + td  : refers to the second column of a table. InOrder to make it work you need to specify the width,max-width(for IE), white-space and Overflow:hidden and text-overflow: ellipsis;

The output is:
 


 *Note: Here We get the desired result with single line only but I need multiple line with  ellipsis, so to do it I followed JQuery.


What I had done with JQuery:

In order to fix this issue I just write below the two line to get multiple lines followed with ellipsis(...)

$("#divContainer td:first-child + td").each(function() {
            $(this).text($(this).text().substr(0,75)+"...");
        });


The Output is:

Monday, 12 March 2012

Auto Refresh .aspx page - ASP.NET

Method 1: Response.AddHeader

To Refresh Web page After every 15 Seconds You can add following code,
Response.AddHeader("Refresh", "15");

Yahoo Cricket Sites, Stock Exchange sites use similar logic :)

Method 2: In body Tag, window.setTimeout

Here 1000 = 1 Second...
<body onload="window.setTimeout('window.location.reload()',1000);">


Method 3: In Meta Tag

Theres also a meta tag that you can define on the head, but not sure wheter it refreshes even if the content has not finished loading or if it starts counting when the head section loaded. Code would be something like that:
<meta content="600" http-equiv="refresh">

Method 4: Timer Control : Microsoft ASP.NET 2.0 AJAX Extensions server control

In following example, the timer interval is set to 10 seconds
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>Timer Example Page</title>

  <script runat="server">
      protected void Page_Load(object sender, EventArgs e)
      {
          OriginalTime.Text = DateTime.Now.ToLongTimeString();
      }

      protected void Timer1_Tick(object sender, EventArgs e)
      {
          StockPrice.Text = GetStockPrice();
          TimeOfPrice.Text = DateTime.Now.ToLongTimeString();
      }

      private string GetStockPrice()
      {
          double randomStockPrice = 50 + new Random().NextDouble();
          return randomStockPrice.ToString("C");
      }
  </script>

</head>
<body>
  <form id="form1" runat="server">
      <asp:ScriptManager ID="ScriptManager1" runat="server" />
      <asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="10000" />
      <asp:UpdatePanel ID="StockPricePanel" runat="server" UpdateMode="Conditional">
          <triggers>
              <asp:AsyncPostBackTrigger ControlID="Timer1" />
          </triggers>
          <contenttemplate>
              Stock price is <asp:Label id="StockPrice" runat="server"/>
              <br/>as of
              <asp:Label id="TimeOfPrice" runat="server"/>
          </contenttemplate>
      </asp:UpdatePanel>
      <div>
          Page originally created at
          <asp:Label ID="OriginalTime" runat="server"></asp:Label>
      </div>
  </form>
</body>
</html>

How to Read The Codes from the dll files

One can browse, analyze, decompile and debug .NET code when ever one get a DLL file. The Mostly used one to read the content  is the  .NET Reflector, however you must be knowing that it’s not free now. But don't Loose your heart you can even use  dotPeek, an awesome decompiler, which was been developed by JetBrains, the maker of Re-sharper,Eclipse and more developer productivity tools.  
You can check out what its latest build offer here and can download it from here.