Skip to main content

Posts

Showing posts from August, 2012

Adding a Control into a String Dynamically

You suppose you are wanted to add a string in a page that the string has to have a hyper link in it to link an especial page for any reason. 
So for doing this job you can do it in this way:


privatestring buildMessage(string message, int reportId) {StringBuilder sb = newStringBuilder();         sb.Append(message);         sb.Append("<br>");         HyperLink hp = newHyperLink();         hp.NavigateUrl = "~/index.aspx?reportId=" + reportId;         hp.ID = "hpl";         hp.Text = "Link Report";         hp.Style.Add(HtmlTextWriterStyle.FontWeight, "Bold");         using (StringWriter sw = newStringWriter(sb)) {             using (HtmlTextWriter tw = newHtmlTextWriter(sw)) {                 hp.RenderControl(tw);             }         } sb.Append("Some other text");return sb.ToString();     }Now you can you this function in different way. For example you can add a Panel Control to your page. Then call this functio…

Distinct in a List of Objects

If you have a list of objects and in the list there are repetitive elements, If you want to delete duplicate elements, you may be want to use Linq Distinct() method. But the result may be not your desire because it does not work properly.
For getting the best result in your work, it is better to use the following method for doing distinct in your list:

1: var listOfObject = items 2: .GroupBy(l => l.PropertyToCompare) //Name of you propery   3: .Select(l => l.First());