how to access PlaceHolder from codeBehind (class - .dll file) ???? PLEASE HELP

  • Thread starter gonzal kamikadze
  • Start date
G

gonzal kamikadze

Hi

It is possible to access a placeHolder in a aspx page from a class (compiled
code)?
(coding in VB)


--- ASPX page ---
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1"
debug="true" %>
<%@ Import Namespace="myNameSpace" %>
<script runat="server">
Sub page_Load()
Dim myClass As New testClass
myClass.testSub
End Sub
</script>
<html>
<title>test page</title>
<body>
<form ID="form1" runat="server">
<asp:placeHolder ID="plhTest" runat=server" />
</form>
</body>
</html>



--- page.vb ---
Imports system
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Namespace myNameSpace
Public Class testClass
Inherits Page
Public plhTest as PlaceHolder

Sub testSub()
Dim myLabel As New Label
myLabel.Text = " - TEST LABEL - "
plhTest.Controls.Add(myLabel)
End Sub
End Class
End Namespace



I'm getting all the time an error message like this:
System.NullReferenceException: Object reference not set to an instance of an
object.

does anybody know the solution to the above problem??? thanks

regards
 
N

nikki

Sure it's possible. You are declaring it, but I don't see FindControl
or a check that it exists anywhere...
You need to do FindControl.

Relevant snippets from my C# app...
Control placeholder = this.FindControl("legacyPlaceholder");
....
placeholder.Controls.Add(h);
 
C

clintonG

FindControl is late bound and imposes performance penalties.
An early bound approach would use a public property.

That allows the objects to be initialized by the Page class at runtime which
makes it easy for us to simply refer to merhods or properties using dotted
grammar from Page_Load or wherever.

I'm just about mastering this myself and only use FindControl at design-time
and then try to refactor the code manually. Next on the learning curve is to
learn how to use Visual Studio to refactor the code which is a feature I'm
fairly certain Visual Studio supports. At least for C#.


<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
 
P

Patrice

For now my guess is that the variable is not considered as being a match for
the control markup (try perhaps "protected" instead of "public").
 
N

nikki

clintonG said:
FindControl is late bound and imposes performance penalties.

Really?
What kind?
Under what situations is it a bad thing to use?
Any articles you could point me to?

Thx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top