Click to See Complete Forum and Search --> : How do i reference asp.net controls in vb??


Martin_SBT
July 24th, 2006, 03:00 PM
Please be patient with me as i am new to asp.net! :)

I am sure this is an easy question for you guys...

i am using VS 2003 asp.net 1.1 with VB.net

i created my controls in Design view and aligned them the way i wanted them to be which was not obvious at all by the way! :ehh:

now that i need to write some code in my WebForm1.aspx.vb referencing some of the properties of the controls created in design view, i found out that i could not reference the Controls by their Control ID directly!!

How do i access the properties of the controls i created in design view???

thanks for any help

regards

Martin

mcmcom
July 24th, 2006, 03:30 PM
you CAN reference them by their ID's.

this.myControlName.theProperty = whatever;

hth,
mcm

Alsvha
July 24th, 2006, 03:33 PM
That depends on how you have added them to your page.
If you have simply "pulled" them into the design view you need to create an instance in the code-behind by using "findControl" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuicontrolclassfindcontroltopic.asp) method.

For instance: Dim myControlInstance as MyControl = me.FindControl("IdOfMyControl")
then you'll have an object to call the properties on.

Alternative, you can load controls in from code behind and place them into a placeholder on the page:

Dim myControlInstance as MyControl = LoadControl("PathToMyControl")
myPlaceHolder.Controls.Add(myControlInstance )

Here you'll then also get a reference to your control to allow properties to be called.

Martin_SBT
July 24th, 2006, 04:22 PM
if i look at the id of my control in HTML view it is:

"EmployeeNumberTextBox"

Now as you suggested i added the following declaration on top of my webform:

Dim employeeNumberbox As Infragistics.WebUI.WebDataInput.WebTextEdit = Me.FindControl("EmployeeNumberTextBox")

and later in my code i try to access the following property:

employeeNumberbox.text = "test"

and i get the following error when running my project:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.


What am i doing wrong???

mcmcom
July 24th, 2006, 04:24 PM
in the code behind do you have any Protected Members listed right near the top of your code?

it should have all the fields you placed on the form they should look like:

protected System.Web.UI.Webcontrols.TextBox EmployeeNumberTextBox;

if thats there you can just go

this.EmployeeNumberTextBox (and intellisense should show you it in a little popup while your typing) Does this happen? can you type this and then a "." and see a list of all properties / methods you can use? Can you see your controls in there?

Martin_SBT
July 24th, 2006, 04:33 PM
hi mcmcom

when you say "Code Behind" i guess it means my "WebForm1.aspx.vb" ??
(sorry for the goofy question... :blush: )

If so then NO, there is no declaration for the control. And i also tried that too. I tried adding manually these declarations but get the same error when running my project...

I am sure i am missing someting... but what

If you guys can help me figure that out, i will be the happiest men!!!

mcmcom
July 24th, 2006, 04:37 PM
if you add controls at design time, then switch to html view and back to design, they should automatically be added by Visual Studio.

just try one for a test.

Take a control, a textbox, you mentioned EmployeeNumberTextBox.

First off make sure the control has its Runat property set to "server" (ie: <mycontrol id="id" runat="Server" /> <-- that has to be there.

then in the .vb file for that webform1 put

protected System.Web.UI.WebControls.TextBox EmployeeNumberTextBox;


then in the say Page_Load event type

this.EmployeeNumberTextBox.Text = "Something" ;

see if that works.

hth,
mcm

Martin_SBT
July 24th, 2006, 04:55 PM
nope! still the same error!

mcmcom
July 24th, 2006, 04:57 PM
can you put

this + "." and see your controls in the Intellisense box?

are your controls "protected", "private" or "public" ?

Martin_SBT
July 24th, 2006, 05:25 PM
no i cant see thme in intellisense. And there are no declaration for my controls in the WebForm1.aspx.vb

and if i add a declaration, lets say:

Protected blah bla bla

i can sure now access the text property (and i see it in intellisense popup) but when i run my project i keep getting the error i mentionned above

i am lost

Martin_SBT
July 24th, 2006, 07:33 PM
ok i found what it was!

the textbox was a child control of the TabStrip control so instead of

mycontrol as control = me.findcontrol("ID")

i had to use:
mycontrol as control = TabStrip1.findcontrol("ID")

it now works!
Thanks guys for the great help! It is really appreciated!

Martin