Monday, March 23, 2020

ASP.NET AJAX : Ways to access controls in client-side

See:
https://docs.telerik.com/devtools/aspnet-ajax/general-information/get-client-side-reference


Get Client-side Reference to a Control Object

This article shows how to get a client-side reference to a Telerik® UI for ASP.NET AJAX control and use its client-side API.
You can quickly navigate through the sections from here:
The UI for ASP.NET AJAX controls are created by implementing the IScriptControl interface and have the lifecycle of MS AJAX-based controls. Thus, they are initialized during the Sys.Application.init event and the instance of the control can be accessed during the Sys.Application.load event at the earliest. You can see how in the Important MS AJAX Events section.

Using the MS AJAX Approach

MS AJAX exposes the Sys.Application.findComponent method that enables you to get the client-side reference of an MS AJAX-based control in the client via JavaScript. You can also use its shortcut—$find()—as shown in Example 1.
Example 1: Using the $find method to get the control's client-side reference.
<telerik:RadPushButton runat="server" ID="RadButton1" Text="Button" OnClientClicked="showRadWindow" AutoPostBack="false" />

<telerik:RadWindow RenderMode="Lightweight" runat="server" ID="RadWindow1">
    <ContentTemplate>
        <p>Some content.</p>
    </ContentTemplate>
</telerik:RadWindow>

<script>
    function showRadWindow(sender, args) {
        var radWindow = $find("<%= RadWindow1.ClientID %>");
        radWindow.show();
    }
</script>

Using Plain JavaScript Methods

Typically, when an MS AJAX-based control (like the ones from the UI for ASP.NET AJAX suite) renders on the client, its client-side reference is assigned to the wrapper DOM element and exposed through the control property.
This means you can use the MS AJAX $get() or the native document.getElementById() methods and get the instance via the control property. See Example 2 and Example 3.
We recommend you use server-side scripting tags and get the dynamically created client ID of the control (<%= [ControlID].ClientID %>). If, however, you do not have access to the server-side instance of the control, see the options in the Using jQuery Selectors section.
Example 2: Using the getElementById method to get the control's client-side reference.
<telerik:RadPushButton runat="server" ID="RadButton1" Text="Button" OnClientClicked="showRadWindow" AutoPostBack="false" />

<telerik:RadWindow RenderMode="Lightweight" runat="server" ID="RadWindow1">
    <ContentTemplate>
        <p>Some content.</p>
    </ContentTemplate>
</telerik:RadWindow>

<script>
    function showRadWindow(sender, args) {
        var radWindow = document.getElementById("<%= RadWindow1.ClientID %>").control;
        radWindow.show();
    }
</script>
Example 3: Using the $get method to get the control's client-side reference.
<telerik:RadPushButton runat="server" ID="RadButton1" Text="Button" OnClientClicked="showRadWindow" AutoPostBack="false" />

<telerik:RadWindow RenderMode="Lightweight" runat="server" ID="RadWindow1">
    <ContentTemplate>
        <p>Some content.</p>
    </ContentTemplate>
</telerik:RadWindow>

<script>
    function showRadWindow(sender, args) {
        var radWindow = $get("<%= RadWindow1.ClientID %>").control;
        radWindow.show();
    }
</script>

Using jQuery Selectors

Often controls are added via user controls, content templates and other similar techniques, or you need to access the reference in a script file (where server-side scripting is not possible). In such scenarios, IDs are generated dynamically and getting the client-side reference from master or parent pages cannot be done by accessing the ClientID property of the control's server-side instance.
With the help of the included jQuery, you can use the attributeEndsWith selector to get the DOM element and access the client-side instance even by using the server-side ID only.
If the same server-side ID is used with multiple controls, jQuery will return all elements that match the same ending value. You should handle scenarios like this according to the exact requirements and scenario.
Example 4: Using the attributeEndsWith selector to get the client-side instance of a control.
<telerik:RadPushButton runat="server" ID="RadButton1" Text="Button" OnClientClicked="showRadWindow" AutoPostBack="false" />

<telerik:RadWindow RenderMode="Lightweight" runat="server" ID="RadWindow1">
    <ContentTemplate>
        <p>Some content.</p>
    </ContentTemplate>
</telerik:RadWindow>

<script>
    function showRadWindow(sender, args) {
        var radWindow = $telerik.$("[id$='RadWindow1']").get(0).control;
        radWindow.show();
    }
</script>
You can use other selectors for the ID (e.g., contains), or you can even use the CSS class where you can assign to a control or its parent to build the desired jQuery selector.

Using Telerik Methods

You can also use the $telerik.findControl() method that is exposed in the Telerik Static Client Library.
Example 5: Using the $telerik.findControl method to get the control's client-side reference.
<telerik:RadPushButton runat="server" ID="RadButton1" Text="Button" OnClientClicked="showRadWindow" AutoPostBack="false" />

<asp:Panel runat="server" ID="Panel1">
    <telerik:RadWindow RenderMode="Lightweight" runat="server" ID="RadWindow1">
        <ContentTemplate>
            <p>Some content.</p>
        </ContentTemplate>
    </telerik:RadWindow>
</asp:Panel>

<script>
    function showRadWindow(sender, args) {
        var parentElement = $get("<%= Panel1.ClientID%>");
        var radWindow = $telerik.findControl(parentElement, "<%= RadWindow1.ClientID %>");
        radWindow.show();
    }
</script>

Important MS AJAX Events

The client-side instances of the Telerik® UI for ASP.NET AJAX controls are initialized after their scripts are loaded (after Sys.Application.init). The earliest event that you can use to reference the client-side instance of a control is the Sys.Application.load event.
Example 6: Using the Sys.Application.load event to get the instance.
<telerik:RadWindow RenderMode="Lightweight" runat="server" ID="RadWindow1">
    <ContentTemplate>
        <p>Some content.</p>
    </ContentTemplate>
</telerik:RadWindow>

<script>
    Sys.Application.add_load(showRadWindow)

    function showRadWindow() {
        var radWindow = $find("<%= RadWindow1.ClientID %>");
        radWindow.show();
    }
</script>
Example 7: Using server-side techniques to interact with a control's client-side instances.
<telerik:RadWindow RenderMode="Lightweight" runat="server" ID="RadWindow1">
    <ContentTemplate>
        <p>Some content.</p>
    </ContentTemplate>
</telerik:RadWindow>
protected void Page_Load(object sender, EventArgs e)
{
    string script = "function f(){$find(\"" + RadWindow1.ClientID + "\").show(); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", script, true);
}
Using native DOM events (like window.load or jQuery's $(document).ready may not give you the expected results. Such events are usualy prior to the Sys.Application.load event and cannot guarantee that the control instances are initialized.


Friday, March 20, 2020

What happens when you close the lid on your laptop

I think this is my first hardware post.


Win10: search: power and ...
Power & Sleep Settings





Thursday, February 6, 2020

SQL Select into Comma-Delim String

Handy shortcut used to concatenate all areas into comma-delim string:

select
   distinct 
    stuff((
        select ',' + u.AREA_ID
        from myTableu
        where u.USER_ID = 'C1663'
        order by u.AREA_ID
        for xml path('')
    ),1,1,'') as userlist
from myTable
group by AREA_ID

Workable Holiday Table for SQL

--SELECT COUNT(*) FROM GetHolidayDates(0,@StartDate,@EndDate,1) AS h
--WHERE h.HolidayDate BETWEEN @StartDate AND @EndDate





--CREATE FUNCTION [dbo].[GetHolidayDates] (@HolidayKey AS int, @StartDate AS datetime, @EndDate AS DATETIME, @UsageBitMask AS INT)
--RETURNS @HolidayTable TABLE (HolidayKey int, HolidayDate DATETIME, HolidayName VARCHAR(50))

declare @HolidayKey int = 0,  @StartDate datetime = '01/01/2019', @EndDate datetime = '12/31/2019', @UsageBitMask int =1


DECLARE @HolidayTable TABLE
(
    HolidayKey int,
    HolidayDate DATETIME, HolidayName VARCHAR(50)
)


    -- strip time
    select @StartDate = convert(char(10),@StartDate,101), @EndDate = convert(char(10),@EndDate,101)
    DECLARE @Yr int, @EndYr int, @OffsetKey int
    SET @OffsetKey = isnull((SELECT OffsetKey FROM HolidayDef WHERE HolidayKey = @HolidayKey),0)
    SET @Yr = year(@StartDate) SET @EndYr = year(@EndDate)
    IF @Yr>@EndYr RETURN

    WHILE @Yr<= @EndYr
        BEGIN
--            IF @HolidayKey = 0 OR @HolidayKey = 15 OR @OffsetKey = 15 INSERT INTO @HolidayTable SELECT 15,dbo.Passover(@Yr)
--            IF @HolidayKey = 0 OR @HolidayKey = 18 OR @OffsetKey = 18 INSERT INTO @HolidayTable SELECT 18,dbo.Easter(@Yr)
--            IF @HolidayKey = 0 OR @HolidayKey = 19 OR @OffsetKey = 19 INSERT INTO @HolidayTable SELECT 19,dbo.OEaster(@Yr)
--            IF @HolidayKey = 0 OR @HolidayKey = 45 OR @OffsetKey = 45 INSERT INTO @HolidayTable SELECT 45,dbo.Chanukah(@Yr)
--            IF @HolidayKey = 0 OR @HolidayKey = 54 OR @OffsetKey = 54 INSERT INTO @HolidayTable SELECT 54,dbo.TuBishvat(@Yr)
--            IF @HolidayKey = 0 OR @HolidayKey = 55 OR @OffsetKey = 55 INSERT INTO @HolidayTable SELECT 55,dbo.YomHaAtzmaut(@Yr)
--            IF @HolidayKey = 0 OR @HolidayKey = 56 OR @OffsetKey = 56 INSERT INTO @HolidayTable SELECT 56,dbo.TishaBAv(@Yr)
            INSERT INTO @HolidayTable (HolidayKey, HolidayDate, HolidayName)
            SELECT HolidayKey, cast(str(FixedMonth) + '/' + str(FixedDay) + '/' + str(@Yr) AS datetime), HolidayName
            FROM HolidayDef
            WHERE HolidayType = 'F'
            AND (@HolidayKey = 0 OR @HolidayKey = HolidayKey)
            AND (@UsageBitMask = 0 OR @UsageBitMask & UsageBitMask = @UsageBitMask)

            INSERT INTO @HolidayTable (HolidayKey, HolidayDate, HolidayName)
            SELECT HolidayKey, cast(str(FixedMonth) + '/' + str((7 + DayOfWeek-datepart(dw,cast(str(FixedMonth) + '/01/' + str(@Yr) AS datetime)))%7 + 1) + '/' + str(@Yr) AS datetime) + (WeekOfMonth-1)*7 + Adjustment, HolidayName
            FROM HolidayDef
            WHERE HolidayType = 'M'
            AND (@HolidayKey = 0 OR @HolidayKey = HolidayKey)
            AND (@UsageBitMask = 0 OR @UsageBitMask & UsageBitMask = @UsageBitMask)

            INSERT INTO @HolidayTable (HolidayKey, HolidayDate, HolidayName)
            SELECT H1.HolidayKey, dateadd(dd,H1.Adjustment,HolidayDate), H1.HolidayName
            FROM HolidayDef H1
            JOIN HolidayDef H2
                ON (H1.OffsetKey = H2.HolidayKey)
            JOIN @HolidayTable HT
                ON (HT.HolidayKey = H1.OffsetKey AND year(HolidayDate) = @Yr)
            WHERE H1.HolidayType = 'O'
            AND (@HolidayKey = 0 OR @HolidayKey = H1.HolidayKey)
            AND (@UsageBitMask = 0 OR @UsageBitMask & H1.UsageBitMask = @UsageBitMask)

            SET @Yr = @Yr + 1
        END
        DELETE @HolidayTable WHERE HolidayDate<@StartDate OR HolidayDate>@EndDate OR HolidayKey<>@HolidayKey AND @OffsetKey<>0

        -- Adjust StateHolidays to nearest business day
        if @UsageBitMask = 1
            update @HolidayTable set
                HolidayDate = dateadd(dd,
                    case DATEPART(weekday,HolidayDate)
                        when 1 then 1
                        when 7 then -1
                        else 0
                        end
                    ,HolidayDate)
                   
--        RETURN


select * from @HolidayTable

Wednesday, January 15, 2020

T-SQL: SET ROWCOUNT 0

Why is this phrase used all over the place in stored procs?  Well, if someone has used "SET ROWCOUNT 1" or to some other number, you need to turn that off! So using "0" turns it off.  It does not set the rowcount to ZERO!!!

T-SQL: CURSOR

drop table #Practitioner;
   select userId as PractitionerId into #Practitioner from tblUsers
  where userStatus = 'Needs to be researched';


DECLARE @PractitionerId NVARCHAR(MAX)

DECLARE MY_CURSOR CURSOR
  LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT DISTINCT PractitionerId
FROM #Practitioner

OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @PractitionerId
WHILE @@FETCH_STATUS = 0
BEGIN
    --Do something with Id here
    PRINT @PractitionerId
    FETCH NEXT FROM MY_CURSOR INTO @PractitionerId
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR

T-SQL LOOPS

Aint no for...loops in T-SQL, so you can use something like:

DECLARE @cnt INT = 0;
WHILE @cnt < 10
BEGIN
   PRINT 'Inside simulated FOR LOOP';
   SET @cnt = @cnt + 1;
END;

PRINT 'Done simulated FOR LOOP';

--------------------------------------------------------------------------------
Practical example to create a reporting hierarchy for an org chart:

DECLARE @userid NVARCHAR(10) = 'A1234'
WHILE @userid is not null
    BEGIN
         select * from tblUsers where userId = @userid
         set @userid  = (select supervisorsUserId from tblUsers where userId = @userid )
    END

-------------------------------------------------------------------------------
Another example, using PRINT and multiple variables:

DECLARE @UserId NVARCHAR(10) = 'A1234',
 @FirstName NVARCHAR(max),
 @LastName NVARCHAR(max);

WHILE @UserId is not null
    BEGIN
         select @FirstName=FirstName, @LastName=LastName
         from tblUsers where UserId = @UserId
         PRINT @FirstName + ' ' + @LastName + ' reports to '
         set @UserId  = (select SupervisorsUserId from tblUsers where UserId = @UserId )
    END


=============================================
You can nest BEGIN...END's!!
=============================================

If you need to insert a "break" (like an "exit"), here's how:

BEGIN

 -- do stuff
--- would cause an infinite loop if not caught!!
IF EXISTS (select userID from tblUsers where SupervisorsUserId = @UserId)
BEGIN
     BREAK;
END



END