Issue
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 How to clear the text of all textBoxes in the form? but my ide is no recognizing it. Click to view Code snippet I have included these libraries:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;
And this is how one of the textbox is created in:
<div class="form-group" style="width: 80%">
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text headDetails">Guide Name</div>
</div>
<asp:TextBox ID="TextBox9" runat="server" CssClass="form-control"></asp:TextBox>
</div>
</div>
How can i clear all the textboxes by clicking on the "Clear All" button?
Solution
This is a common problem. In fact, I ALSO have a routine to fill the text boxes from the database. So, now I don't have to write my binding code.
so, I tend to have two routines:
floader() This routine takes ONE data row, and push out to a set of controls (I place them in a div with a "id").
fwreite() This routine takes the table name, pk id, and grabs the values from the controls, and saves back to the database.
The result is I only wrote the above two routines - and now data binding in, and out of controls on a web page are automatic, and I don't write this code over and over.
However, I never did have a "clear" the controls routine. But, I can take of of the above routines, and simple modify it, and now have a "clear" routine.
So, say I have this markup:
<div id="EditRecord" runat="server" style="float:left;display: normal;border:solid 2px;padding:5px">
<div style="float:left" class="iForm">
<label>HotelName</label>
<asp:TextBox ID="txtHotel" runat="server" f="HOtelName" width="280" /> <br />
<label>First Name</label>
<asp:TextBox ID="tFN" runat="server" f="FirstName" Width="140" /> <br />
<label>Last Name</label>
<asp:TextBox ID="tLN" runat="server" f="LastName" Width="140" /> <br />
<label>City</label>
<asp:TextBox ID="tCity" runat="server" f="City" Width="140" /> <br />
<label>Province</label><asp:TextBox ID="tProvince" runat="server" f="Province" Width="75"></asp:TextBox> <br />
</div>
<div style="float:left;margin-left:20px" class="iForm">
<label>Description</label> <br />
<asp:TextBox ID="txtNotes" runat="server" Width="400" TextMode="MultiLine"
Height="150px" f="Description" ></asp:TextBox> <br />
<asp:CheckBox ID="chkActive" f="Active" Text=" Active" runat="server" TextAlign="Right" />
<asp:CheckBox ID="chkBalcony" f="Balcony" Text=" Has Balcony" runat="server" TextAlign="Right" />
</div>
<div style="clear:both"></div>
<asp:Button ID="cmdClear" runat="server" Text="Clear" CssClass="btn-info" />
</div>
And the clear button, in above has this code:
Protected Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
' clear all controls in div - with f tag
fclear(Me.EditRecord)
End Sub
So, now we have this:
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:
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.
Answered By - Albert D. Kallal


0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.