Posts

Donate

Webbrowser GetAttribute("class") Method Not Working

When working with Webbrowser's GetAttribute() method, you often encountered an issue wherein GetAttribute("class") does not work as expected. 1 2 3 if (Element.GetAttribute( "class" ).Contains( "_textFontColor" )) { //other codes here } After investigating from MSDN docs, I found out a comment below the documentation which states that you have to use className instead of class. Well, that did the trick. :-) 1 2 3 4 5 6 7 8 9 10 11 12 13 HtmlElementCollection elements = WebBrowser1.Document.GetElementsByTagName( "A" ); foreach (HtmlElement Element in elements) { if (Element.GetAttribute( "className" ).Contains( "_textFontColor" )) { //other codes here } } HtmlElementCollection elements = WebBrowser1.Document.GetElementsByTagName( "a" ); foreach (HtmlElement Element in elements) { if (Element.GetAttribute( "className" ).Contains( "_textFontColor" ))

Bootstrap DatePicker Control In ASP.NET Web Forms

Image
Here's how to integrate Bootstrap DatePicker widget in you ASP.NET Webforms Project. First, you need to add reference to the following JS and CSS files: bootstrap.min.css , bootstrap-datepicker3.standalone.css , jquery-1.11.1.min.js , bootstrap.min.js , bootstrap-datepicker.min.js . 1 2 3 4 5 <link rel= "stylesheet" href= "Content/bootstrap.min.css" /> <link rel= "stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker3.standalone.css" /> <script type= "text/javascript" src= "Scripts/jquery-1.11.1.min.js" ></script> <script type= "text/javascript" src= "Scripts/bootstrap.min.js" ></script> <script type= "text/javascript" src= "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.1/js/bootstrap-datepicker.min.js" ></script> Create a simple registration form with B

WPF DataGrid Excel Comment Indicator

Image
Here's how to add a comment indicator to a DataGridCell similar to an Excel functionality using MultiValueConverter. See codefiles and screenshot below. XAML 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 <Window x:Class= "WPFExcelCommentIndicator.DataGridCellCommentIndicator" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src= "clr-namespace:WPFExcelCommentIndicator" Title= "DataGridCellCommentIndicator" Height= "350" Width= "500" WindowStartupLocation= "CenterScreen" > <Window.Resources> <src:DataGridCellComment x:Key= "DataGridCellComment" /> <src:StudentList x:Key= "StudentListData" /> </W

How To Convert DataTable To List Using LINQ And C#

Hello, The code below converts a DataTable object to generic List object using LINQ and C#. It assumes that the DataTable columns match with class properties. C# Code 1 2 3 4 5 6 7 8 var personEnumerable = table.AsEnumerable(); List<Person> ListPosition = new List<Person>(); ListPosition = ( from item in personEnumerable select new Person { ID = item.Field< int >( "ID" ).ToString(), Salary = item.Field< double >( "Salary" ).ToString() }).ToList(); Class 1 2 3 4 5 public class Person { public string ID { get ; set ; } public string Salary { get ; set ; } }

CookieContainer - Part Of Cookie Is Invalid In C# Cookies And WebResponse

Given the code below when scraping a website, you might encounter an error which is stated in the title of the post that is Part Of Cookie Is Invalid . 1 2 3 4 5 6 7 8 9 10 11 WebRequest request = WebRequest.Create( "http://your_url_here" ); WebResponse webResponse = request.GetResponse(); if (webResponse.Headers[ "Set-Cookie" ] != null ) { Cookie m_ccCookies = new Cookie(); CookieContainer ccContainer = new CookieContainer(); ccContainer = new CookieContainer(); ccContainer.SetCookies(webResponse.ResponseUri, webResponse.Headers[ "Set-Cookie" ]); ccContainer.Add(ccContainer.GetCookies(webResponse.ResponseUri)); } The issue could be that the url domain cookie value which is passed to SetCookies method have unrecognized characters that needs to be encoded. So, the workaround is to encode the cookie value first and then assign the appropriate encoding before saving it to the cookie container object. 1 2 ccC

Get Total Hours And Minutes From Total Minutes Using TimeSpan Class In C# And VB.NET

Image
Good evening! Given total number of minutes as input and you need to extract the total number of hours and remaining minutes out from the input, you need to use the TimeSpan class specifically FromMinutes() method. Based from the docs ,the FromMinutes method returns a TimeSpan object that represents a specified number of minutes, where the specification is accurate to the nearest millisecond. C# Code 1 2 3 4 TimeSpan span = TimeSpan.FromMinutes(1506); var hours = ( int ) span.TotalHours; var minutes = span.Minutes; Console.WriteLine(hours + ":" + minutes.ToString( "D2" )); VB.NET Code 1 2 3 4 Dim span As TimeSpan = TimeSpan.FromMinutes(1506) Dim hours = CInt (span.TotalHours) Dim minutes = span.Minutes Console.WriteLine(hours + ":" + minutes.ToString( "D2" )) Output

ASP.NET MVC ListBoxFor() With Optgroup Tag Support

Image
Here's a simple ListBoxFor helper that supports optgroup tag. See versions for C# and VB.NET. C# Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public static class HtmlExtensions { public static IHtmlString ListBoxFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Dictionary< string , IEnumerable<SelectListItem>> selectList, object htmlAttributes = null ) { var select = new TagBuilder( "select" ); select .Attributes.Add( "name" , ExpressionHelper.GetExpressionText(expression)); if (htmlAttributes != null ) { RouteValueDictionary routeValues = new RouteValueDictionary(htmlAttributes); if (!routeValues.ContainsKey(( "size" ).ToLower(

Failed to load resource: the server responded with a status of 404 (WebResource.axd)

I downloaded a GridView custom control with cool features on search and filtering that was developed on Visual Studio 2010 and ASP.NET 4.0. After playing around with the control, I decided to migrate the files to Visual Studio 2012 ASP.NET 4.5. Upon running the ASP.NET Project, the resource to be embedded on the GridView control in which case a JavaScript file was not recognized/found. And thus, returned a 404 status. After doing some research, I came up with the solution below. Steps to fix the issue: 1. Set Build Action of the Resource/JavaScript file to Embedded Resource . In my project, the file to be embedded is EnhancedGridView.js 2. Add WebResourceAttribute to the GridView custom control to embedded a JavaScript file as Resource in an assembly. Make sure that the namespace of the file is correct. 1 2 3 4 5 [assembly: WebResource("GridViewFilter.EnhancedGridView.js", "text/javascript")] public partial class EnhancedGridView : GridView { //.....

Remove Formatting Of Last Selected Text In A WPF RichTextBox Control

Image
Here's how to remove the formatting of last selected text in a WPF RichTextBox control from it's SelectionChanged event. C# Code 1 2 3 4 5 6 private void rtbEditor_SelectionChanged( object sender, RoutedEventArgs e) { //Clear previous selections. TextRange textRange = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd); textRange.ClearAllProperties(); } Output

Create XmlElement Using XmlSerializer In C#.NET

Image
Given the XML data below, the product properties are generated below the products node. What I want is to structure the product properties inside an element Product while serializing. <?xml version="1.0" encoding="utf-8"?> <Products xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "http://www.w3.org/2001/XMLSchema" > <Code>12345</Code> <Name>Samsung Galaxy</Name> <Model>Galaxy</Model> <Manufacturer>Samsung Ltd</Manufacturer> <OperatingSystem>Jelly Bean</OperatingSystem> <Distributor>Junrex</Distributor> <Version>7.0</Version> </Products> To achieve that, I have modified the model classes by separating the properties into another class and in the Products class I created a property of type List with XML Attribute Product . public class Products { [XmlElement("Product")] publ

Donate