checking if textbox actually exists on page...

  • Thread starter Thread starter D. Shane Fowlkes
  • Start date Start date
D

D. Shane Fowlkes

(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!
 
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!
 
Try comparing with Nothing

D. Shane Fowlkes said:
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!
 
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.......
 
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
 
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
 
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
 
Thanks. The problem was, at least with my function, was that the
FindControl was returning a Control as a response.....not just a boolean.
 
Back
Top