Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在使用formview将文本框中的值保存/插入到数据源之前修剪文本框中值的最佳方法_C#_Asp.net_Trim_Formview - Fatal编程技术网

C# 在使用formview将文本框中的值保存/插入到数据源之前修剪文本框中值的最佳方法

C# 在使用formview将文本框中的值保存/插入到数据源之前修剪文本框中值的最佳方法,c#,asp.net,trim,formview,C#,Asp.net,Trim,Formview,我所说的修剪是指类似这样的东西 名字“约翰”->改为“约翰” 姓氏“列侬”->修剪为“列侬” 我在FormView中有这些文本框,使用Bind(“FirstName”)与属性绑定 FormView与实体数据源绑定 我希望在保存之前对这些值进行修剪,最好的方法是什么?方法就是您想要的。用修剪过的文本替换文本框文本。然后编写代码,将其从文本框文本保存到数据库中。这样,UI和后端中的文本将相同并得到更正 mytextbox.Text = mytextbox.Text.Trim(); //save t

我所说的修剪是指类似这样的东西

名字“约翰”->改为“约翰”

姓氏“列侬”->修剪为“列侬”

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

FormView与实体数据源绑定


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

方法就是您想要的。

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

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

//save textbox text in database  
试试这个:

TextBox.Text.Trim();
formView.Controls
第()类
托利斯先生()
.ForEach(t=>t.Text=t.Text.Trim());
为了节省人力,您可以创建一个helper方法,并在任何绑定集之前拦截它

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

我只是创建了一个静态方法,IDBCommand除外。然后将所有参数记录在其中。在那之后,执行了命令

范例

public static CommandIntercepter 
{
  void ExecuteNonQuery(IDbCommand command)
  {
   ...//logged all parameters
   command.ExecuteNonQuery()
  }
 }
现在我通过代码将command.ExecuteNonQuery替换为CommandIntercepter.ExecuteNonQuery(command)

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

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

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

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

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

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

您也可以在发送到任何数据源之前,通过使用
OnItemInserting
OnItemUpdate
事件修剪数据

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();
                }
            }
        } 
    }
受保护的无效项插入字符串(对象发送方,FormViewInsertEventArgs e)
{
FormView fv=发件人作为FormView;
foreach(fv.Controls[0].Controls中的FormViewRow r)
{
foreach(r.Controls中的TableCell)
{
foreach(TextBox txtin cell.Controls.OfType())
{
txt.Text=txt.Text.Trim();
}
}
} 
}

您可以这样做:

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));
    }
}
这将修剪主网格中的所有文本框。
希望对你有所帮助感谢所有的答案。。我终于想到了这个

在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();
        }         
    }
受保护的void frmSubScription\u项插入(对象发送方,FormViewInsertEventArgs e)
{
第页:验证(“注册”);
如果(Page.IsValid==false)
{
e、 取消=真;
}
//微调值
对于(int i=0;i
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();
            }
        }        
    }
受保护的无效gdvSubscribers\u行更新(对象发送方,GridViewUpdateEventArgs e)
{
//微调值
for(int i=0;i
TextBox.Text().Trim()
?一个接一个地键入此内容需要花费太多人力,有没有更快的解决方案?说真的?将其转换为助手/扩展方法并传入值。我看不出修剪两个字符串是多么的“费力”(原文如此)当很多页面中有10-15个文本框时,这将是一项非常费力的工作。计算机通常擅长多次重复任务,无论是10次、15次还是1478239129次-我想我分享的想法是针对您问题的单个实例进行操作,这是你的职责,你得想办法解决它。我并不是建议你为从数据源绑定到模型的每一件事都编写一个
Trim()
命令。要一个接一个地键入这个命令需要花费太多的人力,有没有更快的解决方案?@SarawutPositwinyu你已经花时间键入了一个关于它的问题。我相信你可以多次键入单词
Trim
。当很多页面中有大约10-15个文本框时,这将是一项艰巨的工作。也许可以将其放入实用程序类(我就是这么做的),以防逻辑需要更改。
TrimTextBoxes(this.Grid1);
    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