English 中文(简体)
在通过表格对数据来源进行检索之前在案文箱中进行三重价值的最佳方式
原标题:Best way to trim value in textbox before save/insert using formview into datasource

三角意味着什么。

第一名称“John”-> trim to “John”

“Lennon”-> trim to “Lennon”

我在表态中有这些文本箱,与使用Bind(“FirstName”)的财产有约束力。

表格对实体数据来源具有约束力。

我希望这些价值在拯救之前是微不足道的,这样做的最佳方式是什么?

最佳回答
formView.Controls
    .OfType<System.Web.UI.WebControls.TextBox>()
    .ToList()
    .ForEach(t => t.Text = t.Text.Trim());
问题回答

引证:

TextBox.Text.Trim();

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  
String.Trim() //for triming

为了节省劳动力,你可以做的是,在任何具有约束力的规定之前,制定一种帮助办法并拦截。

I had once such similar scenario, where i need to log every SQL query (ExecuteReader, ExecuteNonQuery) with parameters.

除了美洲开发银行外,只是制造一种静态方法。 之后,它的所有参数都存在漏洞。 之后称为被处决者。

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

我现在取代指挥。 ExecuteNon Query, with Front Intercepter.ExecuteNon Query (command) through the Code.

这样,即从不同代码点的伐木单质参数中节省下来。

如果你在宾德(String propertyName)内有这种拦截点,你就可以在那里进行 tri。 或你可以设立Trim Bind职能,代之以Trim Bind

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

如果只有两个文本箱,你可以使用。

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

如果你不知道有多少文本箱将使用,或有许多文本箱,而且你希望将其全部用掉,哪怕是多页,那么我更倾向于为文本Box制造扩展性财产,并超越其文本财产,以换取总是微不足道的价值。

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

这将激发你的所有控制。 在此情况下,你必须指定主电网<Grid x:Name=“Grid1”>,然后在您的代码上,请你说明这一方法。

TrimTextBoxes(this.Grid1);

This will trim all your textboxes within your main grid. Hope it helps

也可在通过<>关于项目设计/代码>或<>关于项目升级的活动>向任何数据来源发送之前,对您的数据进行分类。

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

回答。 我最后谈到这一点。

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签