Reputation: 5042
By triming i mean something like.
First name "John " -> trim to "John"
Last name "Lennon " -> trim to "Lennon"
I have those Textbox in FormView, binding with property using Bind("FirstName").
The FormView bind with Entity Datasource.
I want those value to be trim before saving, what is the best way to do this ?
Upvotes: 3
Views: 32257
Reputation: 509
Public Sub TrimText()
Dim _allTxt As New List(Of Control)
'get all controls (recursive)
_allTxt = GetAllControls(_allTxt, Me, GetType(TextBox))
For Each _txt As TextBox In _allTxt
AddHandler _txt.Enter, AddressOf TextBox_Enter ' Event to fire on enter (or ...)
Next
End Sub
Public Shared Function GetAllControls(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
If parent Is Nothing Then Return list
If parent.GetType Is ctrlType Then
list.Add(parent)
End If
For Each _child As Control In parent.Controls
GetAllControls(list, _child, ctrlType)
Next
Return list
End Function
Private Sub TextBox_Enter(sender As Object, e As EventArgs)
Dim _txt As TextBox = CType(sender, TextBox)
_txt.Text = _txt.Text.Trim()
End Sub
Upvotes: 0
Reputation: 863
public string PersonName
{
get { return txtPersonName.Text.Trim(' '); }
set { txtPersonName.Text = value; }
}
Upvotes: 0
Reputation: 5042
Appreciating all the answer.. I come up with this finally.
Trimimg on Formview
protected void frmSubScription_ItemInserting(object sender, FormViewInsertEventArgs e)
{
Page.Validate("signUp");
if (Page.IsValid == false)
{
e.Cancel = true;
}
// trimimg value
for (int i = 0; i < e.Values.Count; i++)
{
e.Values[i] = e.Values[i].ToString().Trim();
}
}
Trimiming on GridView
protected void gdvSubscribers_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// trimimg value
for (int i = 0; i < e.NewValues.Count; i++)
{
if (e.NewValues[i] is string)
{
e.NewValues[i] = e.NewValues[i].ToString().Trim();
}
}
}
Upvotes: 0
Reputation: 2799
you can also trim your data before sending to any data-source by using OnItemInserting
or OnItemUpdating
events
protected void ItemInsetring(object sender, FormViewInsertEventArgs e)
{
FormView fv = sender as FormView;
foreach (FormViewRow r in fv.Controls[0].Controls)
{
foreach (TableCell cell in r.Controls)
{
foreach (TextBox txtin cell.Controls.OfType<TextBox>())
{
txt.Text = txt.Text.Trim();
}
}
}
}
Upvotes: 0
Reputation: 1714
you could do something like this:
private void TrimTextBoxes(DependencyObject depObject)
{
if (depObject is TextBox)
{
TextBox txt = depObject as TextBox;
txt.Text = txt.Text.Trim();
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObject); i++)
{
TrimTextBoxes(VisualTreeHelper.GetChild(depObject, i));
}
}
This will iterate trough all your controls. You have to name your main grid, in this case, <Grid x:Name="Grid1">
, then on your code behind, you call the method
TrimTextBoxes(this.Grid1);
This will trim all your textboxes within your main grid. Hope it helps
Upvotes: 0
Reputation: 7438
If you have only two text boxes you can use
string firstName = txtFirstName.Text.Trim();
string lastName = txtLastName.Text.Trim();
If you don't know how many textboxes are going to use or there are many textboxes and you want to trim them all , even for multiple pages , I prefer to create Extended Property for TextBox and override its Text property to return always trimmed value.
Upvotes: 0
Reputation: 30698
String.Trim() //for triming
For saving the labor, what you can do is to create a helper method, and intercept it before any binding set.
I had once such similar scenario, where i need to log every SQL query (ExecuteReader, ExecuteNonQuery) with parameters.
What i simply did was to create a static method excepting IDBCommand. Then logged all the parameters in it. After that called the executed.
Example
public static CommandIntercepter
{
void ExecuteNonQuery(IDbCommand command)
{
...//logged all parameters
command.ExecuteNonQuery()
}
}
Now I had replaced command.ExecuteNonQuery, with CommandIntercepter.ExecuteNonQuery(command) through the code.
This way, i was saved from logging individual query parameters at different code points.
If you have such intercepting point inside Bind(String propertyName), you can do trimming there. Or You can create TrimBind function, and call TrimBind instead of Bind
void TrimBind(String propertyName)
{
...//do trimming
Bind(propertyName);
}
Upvotes: 0
Reputation: 116138
formView.Controls
.OfType<System.Web.UI.WebControls.TextBox>()
.ToList()
.ForEach(t => t.Text = t.Text.Trim());
Upvotes: 8
Reputation: 48568
Replace your Textbox text with trimmed one. Then write code that to save it in database from Textbox text. This way your text in UI and backend will be same and corrected.
mytextbox.Text = mytextbox.Text.Trim();
//save textbox text in database
Upvotes: 0