Link to home
Start Free TrialLog in
Avatar of ukinet
ukinet

asked on

Access Textbox within user control from web form

Hi,

I have a web form which loads a user control which contains a textbox (the load is within the aspx page itself, rather than programmatically being loaded).  The control loads fine, but I have thereafter tried to access the textbox from the web form via code behind but am unable to do so.

I have:

1. Created a reference to the control via code-behind (public [namespace].cnt ccnt),
2. Created a new instance (ccnt = new [namespace].cnt()), and
3. Have tried to access the textbox using the code TextBox tbx= (TextBox)ccnt.FindControl("tbx");, as well as
4. Assign a value thereafter (tbx.Text = "This is a test");

but I receive an "Object reference not set to an instance of an object."

Does anyone know what I'm doing wrong?

Thanks,

John
Avatar of jk_vr
jk_vr

Hello,
Do you want to set the textbox value on page load event of the webform?
Avatar of ukinet

ASKER

Hi,

Yes that's what I'm basically trying to do, but on the page load  of the web form I will be calling another function which then places this value within the user control,

Thanks,

John
In this case u can load the control dynamically
try ths

public [namespace].cnt ccnt = (cnt)loadcontrol("usercontrol.ascx")

//do anything with ccnt

//use placeholder or add directly the control to webform  
Have u tried

public [namespace].cnt ccnt = (cnt)FindControl("Id of usercontrol")
Avatar of ukinet

ASKER

I have and it returns [namespace].ccnt denotes a 'field' where a 'class' was expected
SOLUTION
Avatar of jk_vr
jk_vr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the clean solution is jatinderalagh's
Avatar of ukinet

ASKER

Hi,

Thanks for that, the code now works fine, Thanks Jatinder,

Thanks also jk_vr for your time,

John