[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2010-11-11
12
Medium Priority
?
846 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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