Newbie: Custom Server Controls and Codebehind

W

Wardeaux

Help! I'm developing a custom server control and want to get/set properties
in my codebehind. I have the control set within the HTML page correctly and
it loads/displays correctly, but when I try to compile my aspx.vb code, it
complains that my variable name is undeclared. Is there a good article or
sample I can use to master this? I don't need Postback, but I do want the
properties avail during other controls postbacks. ANY assist is most
appreciated!
wardeaux
 
A

Alessandro Zifiglio

The web is stateless, so if you wanted properties to persist your going to
have to save it into viewstate, or a session variable. Viewstate is your
best bet coz it will save the values into the page and then retrieve them
after postback. This is how webserver controls maintain state.
In a get/set accessors scenario the following is correct. If you are unable
to compile and it says a variable is not declared this has nothing to do
with your values not being persisted but that you are using an undeclared
variable. So declare the variable before using it ;P In the following
example I have text declared as string --this is how you need to do it ;P
get/set accessors are useful if you wanted to pass values to another
page --plus there are some extra steps you need to take in the receiving
page etc
Look in the docs, its explained in detail.

Public Property Text() As String
Get
Return CType(ViewState("Text"), String)
End Get
Set
ViewState("Text") = value
End Set
End Property
 
A

Alessandro Zifiglio

oops, custom server control. My mistake. The example i just showed you is
good. I though you were using the get/set accessors in your page and that
didnt make much sense to me ;P
Please ignore where i said "get/set accessors are useful if you wanted to
pass values to another page --plus there are some extra steps you need to
take in the receiving page etc Look in the docs, its explained in detail"

Look at the link for more details on how to maintain state in your web
custom control ;)
http://msdn.microsoft.com/library/d...pguide/html/cpconmantainingstateincontrol.asp
 
A

Alessandro Zifiglio

Re-reading my post, ignore this too : "If you are unable to compile and it
says a variable is not declared this has nothing to do with your values not
being persisted but that you are using an undeclared variable. So declare
the variable before using it ;P"
 
W

Wardeaux

I appreciate your replies, but I think you missed my question...
I have a custom control. I want to set properties and get values from my
custom control in my Codebehind. I place the control on my HTML page
(MyPage.aspx). I name the control MyControl1. Now I go the codebehind
MyPage.aspx.vb and in the PageLoad sub I want to load MyControl1 with data,
but when I type:
"...
MyControl1.MyProperty = "Hello world!"
...."
I get a compile-time error stating that MyControl1 is an undeclared
variable...
How can I get a handle to the custom control object so that I can use it in
MyPage.aspx.vb?
thanks in advance!
wardeaux
 
A

Alessandro Zifiglio

Wardeaux,
Just like any webControl. If you had coded it correctly as you drag it onto
your webform, you should get the declaration for your webcontrol in your
code behind just like with any webcontrol, use that to access its
properties.

Another way is to load it dynamically, you do this like any webcontrol
again. You declare the type first

dim mycontrol1 as mycontrol = new mycontrol
'then add the control to the page, or placeholder control
placeholder1.controls.add(mycontrol1)
'then set any properties --now it will maintain state
mycontrol1.myproperty = "Hello world"

You probably know this, however make sure you have added a reference to your
customcontrol and you see it in your references node in solution
explorer --if you added your custom control your toolbox and dragged and
dropped it onto your page then the reference is already added --otherwise
add the reference
 
W

Wardeaux

Thanks for the reply! I added the control MANUALLY to the aspx page (coded
by hand the @reference and the <Prefix:Tagname> tags... that's probably why
the correct references weren't added to the aspx.vb file... thanks! :)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top