Home > OS >  Need to clear all textboxes inside several group in C#(Control.control not being recognised in my ve
Need to clear all textboxes inside several group in C#(Control.control not being recognised in my ve

Time:05-17

I am working on a website where I have created a form page and added a button "Clear All" to allow clearing all textboxes in one go. I tried the contol.controls solution as given in enter image description here

As noted, you COULD write a single custom routine - but then you would be re-writing that over and over.

So, while I tag all controls with "f" to load/save data automatic from the database (wrote those routines one time too!!!), a clear routine, would look like this:

Public Sub fclear(F As HtmlGenericControl)

    For Each c As System.Web.UI.Control In F.Controls
        Select Case c.GetType
            Case GetType(TextBox)
                Dim ctlC As TextBox = c
                If Not ctlC.Attributes("f") Is Nothing Then
                    ctlC.Text = ""
                End If
            Case GetType(Label)
                Dim ctlC As Label = c
                If Not ctlC.Attributes("f") Is Nothing Then
                    ctlC.Text = ""
                End If

            Case GetType(DropDownList)
                Dim ctlC As DropDownList = c
                If Not ctlC.Attributes("f") Is Nothing Then
                    ctlC.Text = ""
                    ctlC.SelectedItem.Text = ""
                    ctlC.SelectedItem.Value = ""
                End If
            Case GetType(CheckBox)
                Dim ctlC As CheckBox = c
                If Not ctlC.Attributes("f") Is Nothing Then
                    ctlC.Checked = False
                End If
            Case GetType(RadioButtonList)
                Dim ctlC As RadioButtonList = c
                If Not ctlC.Attributes("f") Is Nothing Then
                    ctlC.SelectedValue = -1
                End If
        End Select
    Next

End Sub

So, we pass the "div" with the group of controls. Since each type of control needs DIFFERENT code to clear, then of course over time, you can add more controls. Say you might add a listbox to the above (I don't have that yet).

And note how for a check box control, or drop down, the code to clear is goinng to be diffrent.

Also, as I pointed out, in above, I have a custom "f" tag. that simple allows me to determine what database column.

So, I have a routine (almost identical to above), and to load up the div with one data record, I have this code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        Dim rstData As DataTable
        rstData = MyRst("SELECT * FROM tblHotelsA WHERE ID = 5")
        fLoader(EditRecord, rstData.Rows(0))
    End If

End Sub

and I now get this:

enter image description here

to be really fair? Prio to this post and answer, I NEVER did need a "clear" routine, since I aways create the database row, and pass that to the above floader routine, and that does not clear the data, but in fact displays the new reocrd for me, and thus I never had to write the clear routine.

And in above, the save button? It is this code:

Protected Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click

    Call fWriter(EditRecord, 5, "tblHotelsA", My.Settings.TEST4)

End Sub

I just pass the "div", the table name, and the pk, and the routine does ALL of the work for me.

As I stated, if you REALLY are going to all that trouble to write some routine to clear out the controls, then you might as well leverage that work, effort, and build a data binder routine for you.

So, the code for saving those controls back to the database, looks VERY much like the clear routine - this:

Public Sub fWriter(f As HtmlGenericControl, fPK As Integer, strTable As String, strCon As String)

    ' opposte of fLoader - write a data form to table 
    Dim rstData As New DataTable
    Dim strSQL = "SELECT * FROM " & strTable & " WHERE ID = " & fPK
    Dim cmdSQL As New SqlCommand(strSQL)

    Using conn As New SqlConnection(strCon)
        cmdSQL.Connection = conn
        Using cmdSQL
            conn.Open()
            rstData.Load(cmdSQL.ExecuteReader)
        End Using
    End Using

    Dim rst As DataRow = rstData.Rows(0)

    ' send conrols to this one data row

    For Each c As System.Web.UI.Control In f.Controls
        Select Case c.GetType
            Case GetType(TextBox)
                Dim ctlC As TextBox = c
                If Not ctlC.Attributes("f") Is Nothing Then
                    If rst.Table.Columns.Contains(ctlC.Attributes("f")) Then
                        rst(ctlC.Attributes("f")) = IIf(ctlC.Text = "", DBNull.Value, ctlC.Text)
                    End If
                End If
            Case GetType(Label)
                Dim ctlC As Label = c
                If Not ctlC.Attributes("f") Is Nothing Then
                    If rst.Table.Columns.Contains(ctlC.Attributes("f")) Then
                        rst(ctlC.Attributes("f")) = IIf(ctlC.Text = "", DBNull.Value, ctlC.Text)
                    End If
                End If
            Case GetType(DropDownList)
                Dim ctlC As DropDownList = c
                If Not ctlC.Attributes("f") Is Nothing Then
                    If rst.Table.Columns.Contains(ctlC.Attributes("f")) Then
                        rst(ctlC.Attributes("f")) = IIf(ctlC.Text = "", DBNull.Value, ctlC.Text)
                    End If
                End If
            Case GetType(CheckBox)
                Dim ctlC As CheckBox = c
                If Not ctlC.Attributes("f") Is Nothing Then
                    If rst.Table.Columns.Contains(ctlC.Attributes("f")) Then
                        rst(ctlC.Attributes("f")) = ctlC.Checked
                    End If
                End If

            Case GetType(RadioButtonList)
                Dim ctlC As RadioButtonList = c
                If Not ctlC.Attributes("f") Is Nothing Then
                    If rst.Table.Columns.Contains(ctlC.Attributes("f")) Then
                        rst(ctlC.Attributes("f")) = ctlC.SelectedValue
                    End If
                End If
        End Select
    Next

    ' data row is filled, write out changes
    Using conn As New SqlConnection(strCon)
        Using cmdSQL
            cmdSQL.Connection = conn
            conn.Open()
            Dim da As New SqlDataAdapter(cmdSQL)
            Dim daU As New SqlCommandBuilder(da)
            da.Update(rstData)
        End Using
    End Using

End Sub

So, since you HAVE to spend that cup of coffee building a routine to get the kind of control, and hten clear it? Might as well cut paste that routine, and then create your floader() routine and the fwriter() routine - since all 3 of them have code to determine the kind of control(s) you using to clear.

As noted, over time, you can add more controls, such as a listbox, or better support for say a radiobutton list, or checkbox list.

  • Related