A personal repository of technical notes. - CSC

How to Convert an ASP.NET Control to String

Problem
Need to generate an HTML string from an ASP.NET web control created in code-behind without rendering it on the web page.

Solution
Use the control's RenderControl() method with a StringWriter to generate the HTML.
  

// Code sample of a utility method to convert a control to an HTML string

public static string RenderControlToString(Control control)

{

string renderedControl = null;

if (control != null)

{

using (StringWriter tempStringWriter = new StringWriter())

{

using (HtmlTextWriter tempHtmlTextWriter = new HtmlTextWriter(tempStringWriter))

{

control.RenderControl(tempHtmlTextWriter);

renderedControl = tempStringWriter.ToString();

tempHtmlTextWriter.Close();

}

tempStringWriter.Close();

}

}

return renderedControl;

}

// Test Utility Method

private void TestRenderControlToString()

{

HyperLink testHyperLink = new HyperLink();

testHyperLink.NavigateUrl = "/MyWebFolder/Default.aspx";

testHyperLink.Text = "Hyper Link Value";

testHyperLink.Target = "_blank";

string renderedControl = Utility.RenderControlToString(testHyperLink);

Response.Write(Server.HtmlEncode(renderedControl));

Response.Write(renderedControl);

}


References
"Control.RenderControl Method (System.Web.UI)." MSDN: Microsoft Development, MSDN Subscriptions, Resources, and More. 27 Mar. 2009
http://msdn.microsoft.com/en-us/library/system.web.ui.control.rendercontrol(VS.80).aspx.

"StringWriter Class (System.IO)." MSDN: Microsoft Development, MSDN Subscriptions, Resources, and More. 27 Mar. 2009
http://msdn.microsoft.com/en-us/library/system.io.stringwriter(VS.80).aspx.

SQL CASE Examples

-- --------------------------------------------------
-- Simple CASE function:
-- --------------------------------------------------
CASE input_expression
      WHEN when_expression THEN result_expression
      ELSE else_result_expression
END

-- --------------------------------------------------
-- Searched CASE function:
-- --------------------------------------------------
CASE
      WHEN Boolean_expression THEN result_expression
      ELSE else_result_expression
END

-- CASE with multiple values tested
CASE
      WHEN t1.Column1 IN(103,109,83) THEN 'PassedTest'
      ELSE NULL
END

-- --------------------------------------------------
-- SELECT calculation
-- --------------------------------------------------
SELECT
(2 *
      CASE @RequestedSchedule
            WHEN 'M' THEN @intMonthly
            WHEN 'Q' THEN @intQuarterly
            ELSE 'Unexpected Value in RequestedSchedule: ' + @RequestedSchedule
      END
)

-- --------------------------------------------------
-- SELECT CASE EXISTS
-- --------------------------------------------------
SELECT
      a.Column01
      ,a.Column02
      ,CASE WHEN EXISTS (SELECT z.Column05 FROM TableZ AS z WHERE zColumn06 = a.Column01)
            THEN 'Exists'
            ELSE 'Not Exists'
      END
FROM
      TableA AS a

-- --------------------------------------------------
-- Set selection mode based upon passed parameters
-- --------------------------------------------------
DECLARE
      @currentSelectionMode int
      ,@selectionModeByAAA int
      ,@selectionModeByBBB int
SELECT
      @selectionModeByAAA = 1
      ,@selectionModeByBBB = 2

SELECT @currentSelectionMode =
      CASE
            WHEN @passedValue01 IS NOT NULL THEN @selectionModeByAAA
            WHEN @passedValue02 IS NOT NULL THEN @selectionModeByBBB
            ELSE 0
      END

IF (@currentSelectionMode = 0)
BEGIN
      RAISERROR('No valid selection criteria was passed.', 11, 1)
      -- Ths first number, severity, is the user-defined severity level associated with this message.
      -- Severity levels from 0 through 18 can be specified by any user.
      -- NOTE: Must be >= 11 in order to throw an error in C#
      RETURN -1
END

WRT54G Connection To Motorola Modem

Problem
Unable to connect through to internet from Linksys WRT54G router to Motorola 2110-02-1002 DSL modem.

Solution
Had to change IP address of router from 192.168.1.1 to 192.168.2.1 even though modem IP address is 192.168.1.254

Was able to keep PPPoE on modem and did not have to enable PPPoE on the router.

References
"Setting-Up a Router with DSL Internet Service" Linksys Easy Answers (Page 1 of 121). 19 Mar. 2009
http://linksys.custhelp.com/cgi-bin/linksys.cfg/php/enduser/std_adp.php?p_faqid=3687&lid=3973237401B07.