By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

checking if textbox actually exists on page...

P: n/a
(ASP.NET 2 / VB)

Question - How can I write a If statement to see if a control (textbox)
actually exists on a page? Upon page_load, a certain control may or may not
be visible on the page so I need to first check to see if it actually exists
and then if it does, get the value from it. If I try to read it or check if
its "visible" if it's not on the page then of course, it'll error.

Thanks!
Mar 1 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
You can do a FindControl(ID) or just add a Try/Catch block around the IF.
--
Adrian Parker
Ingenuity At Work Ltd

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message news:ey**************@TK2MSFTNGP09.phx.gbl...
(ASP.NET 2 / VB)

Question - How can I write a If statement to see if a control (textbox) actually exists on a page? Upon page_load, a certain
control may or may not be visible on the page so I need to first check to see if it actually exists and then if it does, get the
value from it. If I try to read it or check if its "visible" if it's not on the page then of course, it'll error.

Thanks!

Mar 1 '06 #2

P: n/a
Thanks. That'll help...but I'm stuck.

If FindControl(txtFixedOpExp) Is Null Then.....

According to the Object Browser and Help files in VWD, the FindControl will
return the name of the control or a Null. Yet, my If statement still
errors.

Error:
Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to
'String'.
From VWD:
Public Overridable Function FindControl(ByVal id As String) As
System.Web.UI.Control

Member of: System.Web.UI.Control

Summary:

Searches the current naming container for a server control with the
specified id parameter.

Parameters:

id: The identifier for the control to be found.

Return Values:

The specified control, or null if the specified control does not exist.
Any idead? Thanks again!

"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:O0**************@TK2MSFTNGP10.phx.gbl...
You can do a FindControl(ID) or just add a Try/Catch block around the IF.
--
Adrian Parker
Ingenuity At Work Ltd

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:ey**************@TK2MSFTNGP09.phx.gbl...
(ASP.NET 2 / VB)

Question - How can I write a If statement to see if a control (textbox)
actually exists on a page? Upon page_load, a certain control may or may
not be visible on the page so I need to first check to see if it actually
exists and then if it does, get the value from it. If I try to read it
or check if its "visible" if it's not on the page then of course, it'll
error.

Thanks!


Mar 1 '06 #3

P: n/a
Try comparing with Nothing

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message news:uX****************@TK2MSFTNGP11.phx.gbl...
Thanks. That'll help...but I'm stuck.

If FindControl(txtFixedOpExp) Is Null Then.....

According to the Object Browser and Help files in VWD, the FindControl will return the name of the control or a Null. Yet, my If
statement still errors.

Error:
Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to 'String'.
From VWD:
Public Overridable Function FindControl(ByVal id As String) As System.Web.UI.Control

Member of: System.Web.UI.Control

Summary:

Searches the current naming container for a server control with the specified id parameter.

Parameters:

id: The identifier for the control to be found.

Return Values:

The specified control, or null if the specified control does not exist.
Any idead? Thanks again!

"Adrian Parker" <ap******@nospam.nospam> wrote in message news:O0**************@TK2MSFTNGP10.phx.gbl...
You can do a FindControl(ID) or just add a Try/Catch block around the IF.
--
Adrian Parker
Ingenuity At Work Ltd

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message news:ey**************@TK2MSFTNGP09.phx.gbl...
(ASP.NET 2 / VB)

Question - How can I write a If statement to see if a control (textbox) actually exists on a page? Upon page_load, a certain
control may or may not be visible on the page so I need to first check to see if it actually exists and then if it does, get the
value from it. If I try to read it or check if its "visible" if it's not on the page then of course, it'll error.

Thanks!



Mar 1 '06 #4

P: n/a
I'm surprised. Same error.

My two If attempts:
If IsNothing(FindControl(txtFixedOpExp)) = True Then

If FindControl(txtFixedOpExp) Is Nothing Then

Same error:

Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to
'String'.


I'm going to search for some real sample code in VWD.......

"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Try comparing with Nothing

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:uX****************@TK2MSFTNGP11.phx.gbl...
Thanks. That'll help...but I'm stuck.

If FindControl(txtFixedOpExp) Is Null Then.....

According to the Object Browser and Help files in VWD, the FindControl
will return the name of the control or a Null. Yet, my If statement
still errors.

Error:
Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to
'String'.
From VWD:
Public Overridable Function FindControl(ByVal id As String) As
System.Web.UI.Control

Member of: System.Web.UI.Control

Summary:

Searches the current naming container for a server control with the
specified id parameter.

Parameters:

id: The identifier for the control to be found.

Return Values:

The specified control, or null if the specified control does not exist.
Any idead? Thanks again!

"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:O0**************@TK2MSFTNGP10.phx.gbl...
You can do a FindControl(ID) or just add a Try/Catch block around the
IF.
--
Adrian Parker
Ingenuity At Work Ltd

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:ey**************@TK2MSFTNGP09.phx.gbl...
(ASP.NET 2 / VB)

Question - How can I write a If statement to see if a control (textbox)
actually exists on a page? Upon page_load, a certain control may or
may not be visible on the page so I need to first check to see if it
actually exists and then if it does, get the value from it. If I try
to read it or check if its "visible" if it's not on the page then of
course, it'll error.

Thanks!



Mar 1 '06 #5

P: n/a
Got it. Needed quotes.

If FindControl("txtFixedOpExp") Is Nothing Then

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I'm surprised. Same error.

My two If attempts:
If IsNothing(FindControl(txtFixedOpExp)) = True Then

If FindControl(txtFixedOpExp) Is Nothing Then

Same error:

Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to
'String'.


I'm going to search for some real sample code in VWD.......

"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Try comparing with Nothing

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:uX****************@TK2MSFTNGP11.phx.gbl...
Thanks. That'll help...but I'm stuck.

If FindControl(txtFixedOpExp) Is Null Then.....

According to the Object Browser and Help files in VWD, the FindControl
will return the name of the control or a Null. Yet, my If statement
still errors.

Error:
Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to
'String'.
From VWD:
Public Overridable Function FindControl(ByVal id As String) As
System.Web.UI.Control

Member of: System.Web.UI.Control

Summary:

Searches the current naming container for a server control with the
specified id parameter.

Parameters:

id: The identifier for the control to be found.

Return Values:

The specified control, or null if the specified control does not exist.
Any idead? Thanks again!

"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:O0**************@TK2MSFTNGP10.phx.gbl...
You can do a FindControl(ID) or just add a Try/Catch block around the
IF.
--
Adrian Parker
Ingenuity At Work Ltd

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:ey**************@TK2MSFTNGP09.phx.gbl...
> (ASP.NET 2 / VB)
>
> Question - How can I write a If statement to see if a control
> (textbox) actually exists on a page? Upon page_load, a certain
> control may or may not be visible on the page so I need to first check
> to see if it actually exists and then if it does, get the value from
> it. If I try to read it or check if its "visible" if it's not on the
> page then of course, it'll error.
>
> Thanks!
>



Mar 1 '06 #6

P: n/a
just tried

If FindControl("fred") Is Nothing Then
End If

and it didn't blow up

Is txtFixedOpExp the ID of the control you're looking for ? if so, put it in quotes..

If FindControl("txtFixedOpExp") Is Nothing Then
End If

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl...
I'm surprised. Same error.

My two If attempts:
If IsNothing(FindControl(txtFixedOpExp)) = True Then

If FindControl(txtFixedOpExp) Is Nothing Then

Same error:

Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to 'String'.


I'm going to search for some real sample code in VWD.......

"Adrian Parker" <ap******@nospam.nospam> wrote in message news:%2***************@TK2MSFTNGP15.phx.gbl...
Try comparing with Nothing

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message news:uX****************@TK2MSFTNGP11.phx.gbl...
Thanks. That'll help...but I'm stuck.

If FindControl(txtFixedOpExp) Is Null Then.....

According to the Object Browser and Help files in VWD, the FindControl will return the name of the control or a Null. Yet, my
If statement still errors.

Error:
Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to 'String'.
From VWD:
Public Overridable Function FindControl(ByVal id As String) As System.Web.UI.Control

Member of: System.Web.UI.Control

Summary:

Searches the current naming container for a server control with the specified id parameter.

Parameters:

id: The identifier for the control to be found.

Return Values:

The specified control, or null if the specified control does not exist.
Any idead? Thanks again!

"Adrian Parker" <ap******@nospam.nospam> wrote in message news:O0**************@TK2MSFTNGP10.phx.gbl...
You can do a FindControl(ID) or just add a Try/Catch block around the IF.
--
Adrian Parker
Ingenuity At Work Ltd

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message news:ey**************@TK2MSFTNGP09.phx.gbl...
> (ASP.NET 2 / VB)
>
> Question - How can I write a If statement to see if a control (textbox) actually exists on a page? Upon page_load, a certain
> control may or may not be visible on the page so I need to first check to see if it actually exists and then if it does, get
> the value from it. If I try to read it or check if its "visible" if it's not on the page then of course, it'll error.
>
> Thanks!
>



Mar 1 '06 #7

P: n/a
Thanks once again. I spoke too soon. I have a function to detect whether a
series of controls exist on a page or not. Just as a test, I'm only
checking one control. However, my function ALWAYS returns "OK". I'm
stumped because the control "txtFixedOpExp" is NOT on the page or in the
source code.....


Protected Function CheckForModes() As String

Dim strResponse As String

'If No Modes are Visible on the page then...

If IsNothing(FindControl("txtFixedOpExp")) = True Then

strResponse = "No Modes"

Else

strResponse = "OK"

End If

Return strResponse

End Function



"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:O4****************@TK2MSFTNGP11.phx.gbl...
just tried

If FindControl("fred") Is Nothing Then
End If

and it didn't blow up

Is txtFixedOpExp the ID of the control you're looking for ? if so, put it
in quotes..

If FindControl("txtFixedOpExp") Is Nothing Then
End If

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I'm surprised. Same error.

My two If attempts:
If IsNothing(FindControl(txtFixedOpExp)) = True Then

If FindControl(txtFixedOpExp) Is Nothing Then

Same error:

Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to
'String'.


I'm going to search for some real sample code in VWD.......

"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Try comparing with Nothing

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:uX****************@TK2MSFTNGP11.phx.gbl...
Thanks. That'll help...but I'm stuck.

If FindControl(txtFixedOpExp) Is Null Then.....

According to the Object Browser and Help files in VWD, the FindControl
will return the name of the control or a Null. Yet, my If statement
still errors.

Error:
Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted
to 'String'.
From VWD:
Public Overridable Function FindControl(ByVal id As String) As
System.Web.UI.Control

Member of: System.Web.UI.Control

Summary:

Searches the current naming container for a server control with the
specified id parameter.

Parameters:

id: The identifier for the control to be found.

Return Values:

The specified control, or null if the specified control does not exist.
Any idead? Thanks again!

"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:O0**************@TK2MSFTNGP10.phx.gbl...
> You can do a FindControl(ID) or just add a Try/Catch block around the
> IF.
> --
> Adrian Parker
> Ingenuity At Work Ltd
>
> "D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
> news:ey**************@TK2MSFTNGP09.phx.gbl...
>> (ASP.NET 2 / VB)
>>
>> Question - How can I write a If statement to see if a control
>> (textbox) actually exists on a page? Upon page_load, a certain
>> control may or may not be visible on the page so I need to first
>> check to see if it actually exists and then if it does, get the value
>> from it. If I try to read it or check if its "visible" if it's not
>> on the page then of course, it'll error.
>>
>> Thanks!
>>
>
>



Mar 1 '06 #8

P: n/a
C# code, but you should be able to understand.

ASP.NET form named frmASPMain that contains multiple textboxes and a button
named btnList.

protected void btnList_Click(object sender, EventArgs e) {

ListTextControls(frmASPMain.Controls, false);

}

private void ListTextControls(ControlCollection controls, bool
listNested) {

foreach(Control control in controls) {

if(control is TextBox) {

Response.Write("Text Control: " + control.ID + "->"
+ ((TextBox)control).Text + "<br />");

}

if((control.Controls != null) && listNested) {

ListTextControls(control.Controls, true);

}

}

}
You can modify the function to accomadate any type of web control.

HTH

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:OT**************@tk2msftngp13.phx.gbl...
Thanks once again. I spoke too soon. I have a function to detect whether
a series of controls exist on a page or not. Just as a test, I'm only
checking one control. However, my function ALWAYS returns "OK". I'm
stumped because the control "txtFixedOpExp" is NOT on the page or in the
source code.....


Protected Function CheckForModes() As String

Dim strResponse As String

'If No Modes are Visible on the page then...

If IsNothing(FindControl("txtFixedOpExp")) = True Then

strResponse = "No Modes"

Else

strResponse = "OK"

End If

Return strResponse

End Function



"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:O4****************@TK2MSFTNGP11.phx.gbl...
just tried

If FindControl("fred") Is Nothing Then
End If

and it didn't blow up

Is txtFixedOpExp the ID of the control you're looking for ? if so, put
it in quotes..

If FindControl("txtFixedOpExp") Is Nothing Then
End If

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I'm surprised. Same error.

My two If attempts:
If IsNothing(FindControl(txtFixedOpExp)) = True Then

If FindControl(txtFixedOpExp) Is Nothing Then

Same error:

Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to
'String'.


I'm going to search for some real sample code in VWD.......

"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Try comparing with Nothing

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:uX****************@TK2MSFTNGP11.phx.gbl...
> Thanks. That'll help...but I'm stuck.
>
> If FindControl(txtFixedOpExp) Is Null Then.....
>
> According to the Object Browser and Help files in VWD, the FindControl
> will return the name of the control or a Null. Yet, my If statement
> still errors.
>
> Error:
> Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted
> to 'String'.
>
>
> From VWD:
> Public Overridable Function FindControl(ByVal id As String) As
> System.Web.UI.Control
>
> Member of: System.Web.UI.Control
>
> Summary:
>
> Searches the current naming container for a server control with the
> specified id parameter.
>
> Parameters:
>
> id: The identifier for the control to be found.
>
> Return Values:
>
> The specified control, or null if the specified control does not
> exist.
>
>
> Any idead? Thanks again!
>
>
>
>
>
> "Adrian Parker" <ap******@nospam.nospam> wrote in message
> news:O0**************@TK2MSFTNGP10.phx.gbl...
>> You can do a FindControl(ID) or just add a Try/Catch block around the
>> IF.
>> --
>> Adrian Parker
>> Ingenuity At Work Ltd
>>
>> "D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
>> news:ey**************@TK2MSFTNGP09.phx.gbl...
>>> (ASP.NET 2 / VB)
>>>
>>> Question - How can I write a If statement to see if a control
>>> (textbox) actually exists on a page? Upon page_load, a certain
>>> control may or may not be visible on the page so I need to first
>>> check to see if it actually exists and then if it does, get the
>>> value from it. If I try to read it or check if its "visible" if
>>> it's not on the page then of course, it'll error.
>>>
>>> Thanks!
>>>
>>
>>
>
>



Mar 1 '06 #9

P: n/a
Thanks. The problem was, at least with my function, was that the
FindControl was returning a Control as a response.....not just a boolean.

"Brooke" <tb******@hotmail.com> wrote in message
news:un**************@TK2MSFTNGP11.phx.gbl...
C# code, but you should be able to understand.

ASP.NET form named frmASPMain that contains multiple textboxes and a
button named btnList.

protected void btnList_Click(object sender, EventArgs e) {

ListTextControls(frmASPMain.Controls, false);

}

private void ListTextControls(ControlCollection controls, bool
listNested) {

foreach(Control control in controls) {

if(control is TextBox) {

Response.Write("Text Control: " + control.ID + "->"
+ ((TextBox)control).Text + "<br />");

}

if((control.Controls != null) && listNested) {

ListTextControls(control.Controls, true);

}

}

}
You can modify the function to accomadate any type of web control.

HTH

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:OT**************@tk2msftngp13.phx.gbl...
Thanks once again. I spoke too soon. I have a function to detect
whether a series of controls exist on a page or not. Just as a test, I'm
only checking one control. However, my function ALWAYS returns "OK".
I'm stumped because the control "txtFixedOpExp" is NOT on the page or in
the source code.....


Protected Function CheckForModes() As String

Dim strResponse As String

'If No Modes are Visible on the page then...

If IsNothing(FindControl("txtFixedOpExp")) = True Then

strResponse = "No Modes"

Else

strResponse = "OK"

End If

Return strResponse

End Function



"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:O4****************@TK2MSFTNGP11.phx.gbl...
just tried

If FindControl("fred") Is Nothing Then
End If

and it didn't blow up

Is txtFixedOpExp the ID of the control you're looking for ? if so, put
it in quotes..

If FindControl("txtFixedOpExp") Is Nothing Then
End If

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I'm surprised. Same error.

My two If attempts:
If IsNothing(FindControl(txtFixedOpExp)) = True Then

If FindControl(txtFixedOpExp) Is Nothing Then

Same error:

Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted
to 'String'.


I'm going to search for some real sample code in VWD.......

"Adrian Parker" <ap******@nospam.nospam> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
> Try comparing with Nothing
>
> "D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
> news:uX****************@TK2MSFTNGP11.phx.gbl...
>> Thanks. That'll help...but I'm stuck.
>>
>> If FindControl(txtFixedOpExp) Is Null Then.....
>>
>> According to the Object Browser and Help files in VWD, the
>> FindControl will return the name of the control or a Null. Yet, my
>> If statement still errors.
>>
>> Error:
>> Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted
>> to 'String'.
>>
>>
>> From VWD:
>> Public Overridable Function FindControl(ByVal id As String) As
>> System.Web.UI.Control
>>
>> Member of: System.Web.UI.Control
>>
>> Summary:
>>
>> Searches the current naming container for a server control with the
>> specified id parameter.
>>
>> Parameters:
>>
>> id: The identifier for the control to be found.
>>
>> Return Values:
>>
>> The specified control, or null if the specified control does not
>> exist.
>>
>>
>> Any idead? Thanks again!
>>
>>
>>
>>
>>
>> "Adrian Parker" <ap******@nospam.nospam> wrote in message
>> news:O0**************@TK2MSFTNGP10.phx.gbl...
>>> You can do a FindControl(ID) or just add a Try/Catch block around
>>> the IF.
>>> --
>>> Adrian Parker
>>> Ingenuity At Work Ltd
>>>
>>> "D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
>>> news:ey**************@TK2MSFTNGP09.phx.gbl...
>>>> (ASP.NET 2 / VB)
>>>>
>>>> Question - How can I write a If statement to see if a control
>>>> (textbox) actually exists on a page? Upon page_load, a certain
>>>> control may or may not be visible on the page so I need to first
>>>> check to see if it actually exists and then if it does, get the
>>>> value from it. If I try to read it or check if its "visible" if
>>>> it's not on the page then of course, it'll error.
>>>>
>>>> Thanks!
>>>>
>>>
>>>
>>
>>
>
>



Mar 1 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.