checking if textbox actually exists on page...

Discussion in 'ASP .Net' started by D. Shane Fowlkes, Mar 1, 2006.

  1. (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.

    D. Shane Fowlkes, Mar 1, 2006
    1. Advertisements

  2. You can do a FindControl(ID) or just add a Try/Catch block around the IF.
    Adrian Parker, Mar 1, 2006
    1. Advertisements

  3. 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

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

    From VWD:
    Public Overridable Function FindControl(ByVal id As String) As

    Member of: System.Web.UI.Control


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


    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!
    D. Shane Fowlkes, Mar 1, 2006
  4. Try comparing with Nothing

    Adrian Parker, Mar 1, 2006
  5. 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

    I'm going to search for some real sample code in VWD.......
    D. Shane Fowlkes, Mar 1, 2006
  6. Got it. Needed quotes.

    If FindControl("txtFixedOpExp") Is Nothing Then
    D. Shane Fowlkes, Mar 1, 2006
  7. 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
    Adrian Parker, Mar 1, 2006
  8. 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"


    strResponse = "OK"

    End If

    Return strResponse

    End Function
    D. Shane Fowlkes, Mar 1, 2006
  9. D. Shane Fowlkes

    Brooke Guest

    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.

    Brooke, Mar 1, 2006
  10. Thanks. The problem was, at least with my function, was that the
    FindControl was returning a Control as a response.....not just a boolean.
    D. Shane Fowlkes, Mar 1, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.