2

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

名字“John” -> 修剪为“John”

姓氏“Lennon” -> 修剪为“Lennon”

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

FormView 与实体数据源绑定。

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

4

11 回答 11

7
formView.Controls
    .OfType<System.Web.UI.WebControls.TextBox>()
    .ToList()
    .ForEach(t => t.Text = t.Text.Trim());
于 2012-05-17T08:42:31.767 回答
4

试试这个:

TextBox.Text.Trim();
于 2012-05-17T08:39:23.240 回答
0

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

于 2012-05-17T08:36:03.550 回答
0

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

mytextbox.Text = mytextbox.Text.Trim();

//save textbox text in database  
于 2012-05-17T08:37:07.980 回答
0
String.Trim() //for triming

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

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

我所做的只是创建一个除 IDBCommand 之外的静态方法。然后记录其中的所有参数。之后称为执行。

例子

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

现在我已经通过代码用 CommandIntercepter.ExecuteNonQuery(command) 替换了 command.ExecuteNonQuery。

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

如果您在 Bind(String propertyName) 中有这样的截取点,则可以在那里进行修剪。或者您可以创建 TrimBind 函数,并调用 TrimBind 而不是 Bind

void TrimBind(String propertyName)
{
  ...//do trimming
  Bind(propertyName);
}
于 2012-05-17T08:46:04.500 回答
0

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

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

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

于 2012-05-17T08:53:32.090 回答
0

OnItemInserting您还可以使用或OnItemUpdating事件在发送到任何数据源之前修剪数据

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();
                }
            }
        } 
    }
于 2012-05-17T09:09:21.120 回答
0

你可以这样做:

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

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

于 2012-05-17T09:12:47.717 回答
0

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

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

在 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();
            }
        }        
    }
于 2012-05-17T11:34:02.160 回答
0
public string PersonName
{
  get { return txtPersonName.Text.Trim(' '); }
  set { txtPersonName.Text = value; }
}
于 2014-07-11T13:11:49.553 回答
0
  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
于 2019-10-30T13:52:04.377 回答