Simple question

G

Guest

Dear all,

I have build a simple Web user control which contains 2 labels named Lab1
and Lab2.

I defined property for that control to change the .Text property of both of
them.
Then I drag my user control on an empty form whcih contains only my created
user control.

Then I set the text property of those control through my Web form code as :

dim myControl as new USerContrl
myControl.SetText ="Test value"

This code is place in my webForm Page_PreRender, but when it runs my user
control labels text property has not change, it contain default value.

What I am doing wrong ?
Is it the properway to do or should I use a Control Libary ?

Thanks for your help
regards
serge
 
K

Karl Seguin

if you are dragging the user control on your webform, it should look
something like this, correct?

<Tag:USerContrlid="myControl" runat="Server" />

in your code, you should declare your control ala:

Protected myControl as USerContrl

and then you can do

myControl.SetText = "Test Value"


the way you are doing it, you are creating a new instance of UserContrl via
new UserCtrl (and setting it's values). Two problems with this (a) you can't
load user controls via the New keyword, you need to use Page.LoadControl
and (b) you are never adding this user control to your page. All of which
doesn't matter, 'cuz you already have a user control on the page...

Karl
 
G

Guest

Ok, I have done what you mentioned but then at the time of setting the property

myControl.SetText ="test" I get error :

"Object reference not set to an instance of an object."

I have declare Protected ctlReel As myControl
Then on my WeForm Page_PreRender i have the code :

myControl.SetText = "Test Value"

If I place a break point and watch for myControl object, value is set to
nothing

When draging the control, it adds the following :
<%@ Register TagPrefix="uc1" TagName="myControl" Src="myControl.ascx" %>
<uc1:myControl id="myControl1" runat="server"></uc1:myControl>

What dd I miss ?
 
K

Karl Seguin

If the id in the aspx is myControl1 than the variable name should be
myControl1

everything should match out

<uc1:myControl id="myControl1" runat="server"></uc1:myControl>
myControl = control name/class name
myControl1 = instance identifier

should have the following declaration:

protected myControl1 as myControl
myControl1 = varialbe name = instance identifier
myControl = control name/class name

you can then use myControl1.Whatever = Whatever

Karl
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top