Sumedh Meshram

.Net Technical Blog

Select distinct from generic list using linq

If you have a generic list which has a multiple data like

ID  FirstName  LastName  City  State Zip

1   ABC           Z              PN   LA     25341

2   XYZ           R              UI   TG     56742

1   ABC           W             PN   LA     25341

3   UHY           R             HJ    UH    34256

3   UHY           J              HJ    UH    34256 

Now if you want to get select distinct rows on based on ID, then through Linq this can be done easily as :

 

var unique =
    from w in genericList
    group w by w.ID
    into g
        select g.FirstOrDefault();

 

This will be useful where we have the generic list with duplicate data or data with duplicate IDs.

 

Sumedh

Finding all controls in an ASP.NET Page

This can be done through enumerating all the controls in the control hierarchy:

Add a method in a common class

 

/// <summary>
/// Find the Controls inside a Page
/// </summary>
/// <param name="parent"></param>
/// <returns></returns>
public static IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
     foreach (Control child in parent.Controls)
     {
            yield return child;
            foreach (Control descendant in EnumerateControlsRecursive(child))
               yield return descendant;
      }
 }

 

Implement this method like this on the PreRender event of a page :

 

 protected override void OnPreRender(EventArgs e)
 {
      foreach (var control in RecursiveHelper.EnumerateControlsRecursive(this))
     {
                
        if (control is TextBox)
           SetTextBoxStyle(control as TextBox);

        if (control is RegularExpressionValidator)
           SetTextBoxEnterValidation(control as RegularExpressionValidator);
      }
 }

 

Then add your method seperately for assigning the common attributes to each control.

 

 private static void SetTextBoxStyle(TextBox textBox)
 {
       textBox.CssClass = "emptyMessageStyle";
 }

 

 

Hope this will help.

Sumedh

Ajax .Net 3.5 Control Toolkit MaskedEditExtender Default to PM

MaskedEditExtender can be set to default to PM.

I modified the code from http://forums.asp.net/t/1339632.aspx

to this

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SoluTest_Mask._Default" %>
 
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 
    <script type="text/javascript">
        //Set the default text to "PM"
        var mee;
        function pageLoad() {
            //Please use your MaskedEditExtender's id or behaviorId.
            mee = $find("MaskedEditExtender3");
 
            //The target textbox control
            var e = mee.get_element();
 
            //Remove the focus event handler
            if (mee._focusHandler) {
                $removeHandler(e, "focus", mee._focusHandler);
            }
            //Add a new focus event handler which inherits from the old one.
            mee._focusHandler = Function.createDelegate(mee, newFocus);
            $addHandler(e, "focus", mee._focusHandler);
        }
        function newFocus() {
            mee._onFocus();
            if ((mee._MaskType == AjaxControlToolkit.MaskedEditType.Time || mee._MaskType == AjaxControlToolkit.MaskedEditType.DateTime) && mee.get_CultureAMPMPlaceholder() != "" && mee._getClearMask() == "") {
                if (mee._AcceptAmPm) {
                    //The original code of default AM/PM text in function _onFocus() is:
                    //this.InsertAMPM(this.get_CultureAMPMPlaceholder().substring(0,1));
 
                   mee.InsertAMPM(meeTueEndCorp.get_CultureFirstLetterPM());
                   mee.setSelectionRange(0, 0);
                }
            }
        }
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <strong>Enter Time (format: <em>99:99:99</em>):</strong>
        <br />
        <asp:TextBox ID="TextBox3" runat="server" Width="130px" Height="16px" />
        <ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender3" runat="server" TargetControlID="TextBox3"
            Mask="99:99:99" MessageValidatorTip="true" OnFocusCssClass="MaskedEditFocus"
            OnInvalidCssClass="MaskedEditError" MaskType="Time" AcceptAMPM="True" ErrorTooltipEnabled="True" />
        <ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator3" runat="server" ControlExtender="MaskedEditExtender3"
            ControlToValidate="TextBox3" IsValidEmpty="False" EmptyValueMessage="Time is required"
            InvalidValueMessage="Time is invalid" Display="Dynamic" TooltipMessage="Input a time"
            EmptyValueBlurredText="*" InvalidValueBlurredMessage="*" />
        <br />
        <em><span style="font-size: 8pt">Tip: Type 'A' or 'P' to switch AM/PM</span></em>
    </div>
    </form>
</body>
</html>

Hope this Help

Sumedh

 

Cheap MLB Jerseys maillot de foot pas cher www.fotballdrakter.org