We help IT Professionals succeed at work.

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

goodk
goodk asked
on
1,038 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;
        }
    }
}
Comment
Watch Question

Commented:
do you want the contents of the user control returned as a string?

Author

Commented:
yes; the UC is html only -

Commented:
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

Author

Commented:

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;
        }

Commented:

public static string addMenu()
        {

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

Open in new window

Author

Commented:
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

Commented:
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.

Author

Commented:
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;
        }
    }
}

Commented:
you could try using the namespace System.Web.UI

Author

Commented:
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

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
thanks
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.