Iterating through some controls on a webform

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DrTeeth

    Iterating through some controls on a webform

    I new to ASP.NET and got slightly confused on a probably simple little
    issue.

    I am try to iterate through some ASP radio buttons to see which of them is
    checked.

    But I get the error :

    Unable to cast object of type 'System.Web.UI. LiteralControl' to type
    'System.Web.UI. WebControls.Rad ioButton'.

    The error occurs in the line : For Each rdbChecked In Me.Controls

    Any help would be much appreciated. TIA

    Protected Sub cmdSearch_Click (ByVal sender As Object, ByVal e As
    System.EventArg s)

    Dim rdbChecked As RadioButton
    Dim sURL As String = ""
    For Each rdbChecked In Me.Controls
    If rdbChecked.Chec ked Then
    sURL = GetSearchString (rdbChecked.ID) & txtSearch.Text
    End If
    Next

    Response.Redire ct(sURL)

    End Sub

  • Mark Rae [MVP]

    #2
    Re: Iterating through some controls on a webform

    "DrTeeth" <not@here.comwr ote in message
    news:%23aQfxWf4 IHA.2064@TK2MSF TNGP02.phx.gbl. ..
    Any help would be much appreciated. TIA
    The error is caused because not every Control in the form is a RadioButton.

    So you would need something like:

    Dim objControl As Control
    For Each objControl In Me.Controls
    If objControl.GetT ype().Name = "RadioButto n" Then
    If rdbChecked.Chec ked Then
    ' whatever
    End If
    End If
    Next


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • DrTeeth

      #3
      Re: Iterating through some controls on a webform

      Ok thanks alot :)

      "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
      news:%23uDAekf4 IHA.3480@TK2MSF TNGP03.phx.gbl. ..
      "DrTeeth" <not@here.comwr ote in message
      news:%23aQfxWf4 IHA.2064@TK2MSF TNGP02.phx.gbl. ..
      >
      >Any help would be much appreciated. TIA
      >
      The error is caused because not every Control in the form is a
      RadioButton.
      >
      So you would need something like:
      >
      Dim objControl As Control
      For Each objControl In Me.Controls
      If objControl.GetT ype().Name = "RadioButto n" Then
      If rdbChecked.Chec ked Then
      ' whatever
      End If
      End If
      Next
      >
      >
      --
      Mark Rae
      ASP.NET MVP
      http://www.markrae.net

      Comment

      Working...