Solved

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

Posted on 2010-11-11
12
782 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now