在使用表单视图将/插入数据源之前修剪文本框中值的最佳方法

本文关键字:文本 修剪 方法 最佳 表单 视图 数据源 插入 | 更新日期: 2023-09-27 18:34:38

通过修剪,我的意思是类似的东西。

名字"约翰"->修剪为"约翰">

姓氏"列侬"->修剪为"列侬">

我在 FormView 中有这些文本框,使用 Bind("FirstName"( 与属性绑定。

窗体视图与实体数据源绑定。

我希望在保存之前修剪这些值,最好的方法是什么?

在使用表单视图将/插入数据源之前修剪文本框中值的最佳方法

formView.Controls
    .OfType<System.Web.UI.WebControls.TextBox>()
    .ToList()
    .ForEach(t => t.Text = t.Text.Trim());

试试这个:

TextBox.Text.Trim();

string.Trim() 方法就是你想要的。

文本框文本替换为修剪后的文本。然后编写代码以将其从文本框文本保存在数据库中。这样,UI和后端中的文本将相同并得到更正。

mytextbox.Text = mytextbox.Text.Trim();
//save textbox text in database  
String.Trim() //for triming

为了节省劳动力,您可以做的是创建一个帮助程序方法,并在任何绑定集之前截获它。

我曾经遇到过类似的场景,我需要用参数记录每个SQL查询(ExecuteReader,ExecuteNonQuery(。

我所做的只是创建一个静态方法,除了IDBCommand。然后记录其中的所有参数。之后调用被执行。

public static CommandIntercepter 
{
  void ExecuteNonQuery(IDbCommand command)
  {
   ...//logged all parameters
   command.ExecuteNonQuery()
  }
 }

现在我已经取代了指挥。ExecuteNonQuery,通过代码使用 CommandIntercepter.ExecuteNonQuery(command(。

这样,我就不必在不同的代码点记录单个查询参数。

如果你在 Bind(字符串属性名称(中有这样的截取点,你可以在那里进行修剪。或者你可以创建 TrimBind 函数,并调用 TrimBind 而不是 Bind

void TrimBind(String propertyName)
{
  ...//do trimming
  Bind(propertyName);
}

如果只有两个文本框,则可以使用

string firstName = txtFirstName.Text.Trim();
string lastName = txtLastName.Text.Trim();

如果您不知道将使用多少个文本框,或者有很多文本框并且您想将它们全部修剪,即使是对于多个页面,我更喜欢为 TextBox 创建扩展属性并覆盖其 Text 属性以返回始终修剪的值。

你可以做这样的事情:

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));
    }
}

这将遍历所有控件。你必须命名你的主网格,在这种情况下,<Grid x:Name="Grid1">,然后在你的代码后面,你调用方法

TrimTextBoxes(this.Grid1);

这将修剪主网格中的所有文本框。希望对你有帮助

您还可以

在发送到任何数据源之前使用OnItemInsertingOnItemUpdating事件来修剪数据

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();
                }
            }
        } 
    }

欣赏所有的答案..我终于想出了这个。

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();
        }         
    }

在网格视图上修剪

    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();
            }
        }        
    }
public string PersonName
{
  get { return txtPersonName.Text.Trim(' '); }
  set { txtPersonName.Text = value; }
}
  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