We help IT Professionals succeed at work.

asp.net - controling output from User Control

siac0
siac0 asked
on
526 Views
Last Modified: 2008-02-01
I have a user control i wrote that creates a very simple message board. I usually include it in my pages somethign like this:

///// start example 1 /////

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb" Inherits="iwebdemo._default"%>
<%@ Register TagPrefix="uc1" TagName="messageboard" Src="messageboard.ascx" %>

<HTML>
<body>
some content before the message board <br>blah blah blah <br>
            <form id="Form1" method="post" runat="server">
                  <uc1:messageboard id="Messageboard1" runat="server"></uc1:messageboard>

some content here after the message board, blah blah <br>blah blah
</form>
      </body>
</HTML>

////////   end example 1  /////////


Now..... the current web app i am writing looks something like this.....

///// start example 2 ////

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb" Inherits="iwebdemo._default"%>
<%@ Register TagPrefix="uc1" TagName="messageboard" Src="messageboard.ascx" %>

<HTML>
<body>
<form id="Form1" method="post" runat="server">
<%= strContent %>
</form>
</body>
</HTML>

///// end example 2 /////

the value stored in strContent in example 2 is generated in the code behind file and contains something like the following

strContent = "<table><tr><td>{messageBoardHere}</td></tr></table>"

i want to replace the text in strContent called {messageBoardHere} with an instance of the user control.....  can i do this????

Comment
Watch Question

Author

Commented:
small typo in the code i just posted.... the form tag in example 1, should come straight after the body tag :)
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks. I have almost solved what i was trying to do based on your replies. I'm not sure i explained very well "exactly" what i was trying to do.........the variable strTemplateCode contained keywords i had made up, such as {pageContent}, or {messageBoard} which had to be replaced by content either from a database, or in the case of {messageBoard} with my messageBoard user control... to accomplish what i was trying to do i had to split up the strTemplateCode variable into 2 seperate variables.

 /// in the code behind ///

       If InStr(strTemplateCode, "{messageBoard}") > 0 Then

            Dim messageBoardCode
            messageBoardCode = Split(strTemplateCode, "{messageBoard}")

            strTemplateCode = messageBoardCode(0)
            strTemplateCode2 = messageBoardCode(1)

            'load the messageBoard stuff into the placeHolder
            Dim uc As Control = Page.LoadControl("messageBoard.ascx")
            myPlaceHolder.Controls.Add(uc)
   
        End If

/// aspx page ///

<form id="Form1" method="post" runat="server">
<%= strTemplateCode %>
<asp:PlaceHolder runat="server" id="myPlaceHolder" />
<%= strTemplateCode2 %>                  
</form>

Author

Commented:
One more thing... i have some buttons on the user control, which fire onClick events. the onClick events no longer fire now that the user control is dynamically loaded...  i'll have a search and see if i can work it out :)

Author

Commented:
got it working..... just incase anyone would like to know I ended up doing the following.

// sample code from code behind

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'following solves viewstate problem, and will keep onClick events firing on dynamically loaded messageBoard

        If viewstate("userControl") = "Exists" Then
            Dim uc As Control = Page.LoadControl("messageBoard.ascx")
            myPlaceHolder.Controls.Add(uc)
           writeOutContent()
        End If

        If Not Page.IsPostBack = True Then
           writeOutContent()
        End If

    End Sub

////// writeOutContent Sub Contains the following........

        If InStr(strTemplateCode, "{messageBoard}") > 0 Then

            Dim messageBoardCode
            messageBoardCode = Split(strTemplateCode, "{messageBoard}")

            strTemplateCode = messageBoardCode(0)
            strTemplateCode2 = messageBoardCode(1)

            'load the messageBoard stuff into the placeHolder
            If viewstate("userControl") <> "Exists" Then
                Dim uc As Control = Page.LoadControl("messageBoard.ascx")
                myPlaceHolder.Controls.Add(uc)
                viewstate("userControl") = "Exists"
            End If

        End If

   
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.