siac0
asked on
asp.net - controling output from User Control
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.v b" Inherits="iwebdemo._defaul t"%>
<%@ 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:messa geboard>
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.v b" Inherits="iwebdemo._defaul t"%>
<%@ 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>{messageBo ardHere}</ td></tr></ table>"
i want to replace the text in strContent called {messageBoardHere} with an instance of the user control..... can i do this????
///// start example 1 /////
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.v
<%@ 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:messa
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.v
<%@ 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>{messageBo
i want to replace the text in strContent called {messageBoardHere} with an instance of the user control..... can i do this????
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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("messageB oard.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>
/// 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("messageB
myPlaceHolder.Controls.Add
End If
/// aspx page ///
<form id="Form1" method="post" runat="server">
<%= strTemplateCode %>
<asp:PlaceHolder runat="server" id="myPlaceHolder" />
<%= strTemplateCode2 %>
</form>
ASKER
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 :)
ASKER
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("messageB oard.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("messageB oard.ascx" )
myPlaceHolder.Controls.Add (uc)
viewstate("userControl") = "Exists"
End If
End If
// 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("messageB
myPlaceHolder.Controls.Add
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("messageB
myPlaceHolder.Controls.Add
viewstate("userControl") = "Exists"
End If
End If
ASKER