Solved

<uc1:WebUserControl1 ID="WebUserControl11" runat="server" /> how to make a control return a value

Posted on 2010-11-11
12
798 Views
Last Modified: 2012-05-10
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

 
    <% Response.Write(AceYP_Oct2010.App_Code.MPage.mTable());%>
    </div>
    </form>
</body>
</html>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AceYP_Oct2010.App_Code
{
    public class MPage
    {

        public static string mTable()
        {
            // return "<table bgcolor='brown' width='90%'><tr><td>testing</td></tr><table>";
            string tempstr;

            tempstr = "       <table style='width:100%;'>   <tr> <td bgcolor='#FFFF99' class='style1' colspan='3'>Page Title</td></tr><tr><td bgcolor='#33CC33' width='120'> " + addMenu() + "</td><td width='760'> main content</td> <td>                &nbsp;</td>        </tr>        <tr>            <td bgcolor='#999966' class='style2' colspan='3'>                footer</td>        </tr>    </table> ";

            return tempstr;

        }

        public static string addMenu()
        {

                    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
           
            return tempstr;
        }
    }
}
0
Comment
Question by:goodk
  • 6
  • 6
12 Comments
 
LVL 10

Expert Comment

by:hosneylk
ID: 34118092
do you want the contents of the user control returned as a string?
0
 

Author Comment

by:goodk
ID: 34119973
yes; the UC is html only -
0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34120036
you could create a control on the page and hide it. then you could do something like this..
StringWriter writer = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
WebUserControl11.RenderControl(htmlWriter);
string ctrlOutput = writer.ToString();

Open in new window

0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:goodk
ID: 34126166

I want to know how I could return the control WebUserControl11instead of tempstr? Where WebUserControl11 is a html page saved as a control.
      public static string addMenu()
        {

                    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
           
            return tempstr;
        }
0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34127703

public static string addMenu()
        {

            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            WebUserControl11.RenderControl(htmlWriter);
            return ctrlOutput = writer.ToString();
        }

Open in new window

0
 

Author Comment

by:goodk
ID: 34128031
you could create a control on the page and hide it. then you could do something like this..

How do I do that? thanks


Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'StringWriter' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

 

Line 25:                 //    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
Line 26:
Line 27:             StringWriter writer = new StringWriter();
Line 28:             HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
Line 29:             WebUserControl11.RenderControl(htmlWriter);
 

Source File: c:\AceYP_Oct2010\AceYP_Oct2010\App_Code\MPage.cs    Line: 27

0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34129025
use System.IO.StringWriter or use the namespace System.IO

you can drag and drop the control into your page so that it creates an instance of it. then you could set the style of the control to display:none.
0
 

Author Comment

by:goodk
ID: 34129174
thanks, I am getting this error now.
I do have, see attached code.  I may be still doing something wrong. help please - thanks
using System.Web;


Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'HtmlTextWriter' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

 

Line 27:
Line 28:             StringWriter writer = new StringWriter();
Line 29:             HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
Line 30:             WebUserControl11.RenderControl(htmlWriter);
Line 31:             return ctrlOutput = writer.ToString();
 

Source File: c:\AceYP_Oct2010\AceYP_Oct2010\App_Code\MPage.cs    Line: 29



Show Detailed Compiler Output:


C:\Windows\system32> "c:\Windows\Microsoft.NET\Framework\v3.5\csc.exe" /t:library /utf8output /R:"C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.IdentityModel\3.0.0.0__b77a5c561934e089\System.IdentityModel.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Data.DataSetExtensions\3.5.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.WorkflowServices\3.5.0.0__31bf3856ad364e35\System.WorkflowServices.dll" /R:"C:\Windows\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll" /R:"C:\Windows\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll" /R:"C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll" /R:"C:\Users\Owner\AppData\Local\Temp\Temporary ASP.NET Files\root\93553cfe\7728b794\assembly\dl3\8bde1c33\a09be74e_8d83cb01\AceYP_Oct2010.DLL" /R:"C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.ServiceModel.Web\3.5.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Runtime.Serialization\3.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll" /R:"C:\Windows\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /out:"C:\Users\Owner\AppData\Local\Temp\Temporary ASP.NET Files\root\93553cfe\7728b794\App_Code.zgkbt_di.dll" /D:DEBUG /debug+ /optimize- /w:4 /nowarn:1659;1699;1701 /warnaserror-  "C:\Users\Owner\AppData\Local\Temp\Temporary ASP.NET Files\root\93553cfe\7728b794\App_Code.zgkbt_di.0.cs" "C:\Users\Owner\AppData\Local\Temp\Temporary ASP.NET Files\root\93553cfe\7728b794\App_Code.zgkbt_di.1.cs" "C:\Users\Owner\AppData\Local\Temp\Temporary ASP.NET Files\root\93553cfe\7728b794\App_Code.zgkbt_di.2.cs"


Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

c:\AceYP_Oct2010\AceYP_Oct2010\App_Code\MPage.cs(29,13): error CS0246: The type or namespace name 'HtmlTextWriter' could not be found (are you missing a using directive or an assembly reference?)
c:\AceYP_Oct2010\AceYP_Oct2010\App_Code\MPage.cs(29,45): error CS0246: The type or namespace name 'HtmlTextWriter' could not be found (are you missing a using directive or an assembly reference?)
c:\AceYP_Oct2010\AceYP_Oct2010\App_Code\MPage.cs(30,13): error CS0103: The name 'WebUserControl11' does not exist in the current context
c:\AceYP_Oct2010\AceYP_Oct2010\App_Code\MPage.cs(31,20): error CS0103: The name 'ctrlOutput' does not exist in the current context

 



Show Complete Compilation Source:


Line 1:    using System;
Line 2:    using System.Collections.Generic;
Line 3:    using System.Linq;
Line 4:    using System.Web;
Line 5:    using System.IO;
Line 6:    
Line 7:    namespace AceYP_Oct2010.App_Code
Line 8:    {
Line 9:        public class MPage
Line 10:       {
Line 11:  
Line 12:           public static string mTable()
Line 13:           {
Line 14:               // return "<table bgcolor='brown' width='90%'><tr><td>testing</td></tr><table>";
Line 15:               string tempstr;
Line 16:  
Line 17:               tempstr = "       <table style='width:100%;'>   <tr> <td bgcolor='#FFFF99' class='style1' colspan='3'>Page Title</td></tr><tr><td bgcolor='#33CC33' width='120'> " + addMenu() + "</td><td width='760'> main content</td> <td>                &nbsp;</td>        </tr>        <tr>            <td bgcolor='#999966' class='style2' colspan='3'>                footer</td>        </tr>    </table> ";
Line 18:  
Line 19:               return tempstr;
Line 20:  
Line 21:           }
Line 22:  
Line 23:           public static string addMenu()
Line 24:           {
Line 25:  
Line 26:                   //    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
Line 27:  
Line 28:               StringWriter writer = new StringWriter();
Line 29:               HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
Line 30:               WebUserControl11.RenderControl(htmlWriter);
Line 31:               return ctrlOutput = writer.ToString();
Line 32:  
Line 33:  
Line 34:              
Line 35:              // return tempstr;
Line 36:           }
Line 37:       }
Line 38:   }
Line 39:  
 




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.4206; ASP.NET Version:2.0.50727.4209

I looked this up
http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.aspx

my code is following,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace AceYP_Oct2010.App_Code
{
    public class MPage
    {

        public static string mTable()
        {
            // return "<table bgcolor='brown' width='90%'><tr><td>testing</td></tr><table>";
            string tempstr;

            tempstr = "       <table style='width:100%;'>   <tr> <td bgcolor='#FFFF99' class='style1' colspan='3'>Page Title</td></tr><tr><td bgcolor='#33CC33' width='120'> " + addMenu() + "</td><td width='760'> main content</td> <td>                &nbsp;</td>        </tr>        <tr>            <td bgcolor='#999966' class='style2' colspan='3'>                footer</td>        </tr>    </table> ";

            return tempstr;

        }

        public static string addMenu()
        {

                //    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />

            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            WebUserControl11.RenderControl(htmlWriter);
            return ctrlOutput = writer.ToString();


           
           // return tempstr;
        }
    }
}
0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34129745
you could try using the namespace System.Web.UI
0
 

Author Comment

by:goodk
ID: 34133531
just cannot make it work!!

All I want to know is how to include an html page control into html page - not drag and drop but programitically.

Just if stupid microsoft would allow INCLUDE files.  Or some way to attach a working html page into another html page? but Programitically.

Drag and Drop of control file works. Why I cannot make the same thing works programitically??

I am all confuse. please help - thanks

0
 
LVL 10

Accepted Solution

by:
hosneylk earned 500 total points
ID: 34133545
well if thats what you want to do you can do this
Control WebUserControl1 = LoadControl("controlpath\WebUserControl.ascx");
Controls.Add(WebUserControl1);

Open in new window

0
 

Author Closing Comment

by:goodk
ID: 34233965
thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question