C# 在使用formview保存/插入数据源之前在文本框中修剪值的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10632277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to trim value in textbox before save/insert using formview into datasource
提问by Sarawut Positwinyu
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").
我在 FormView 中有那些文本框,使用 Bind("FirstName") 与属性绑定。
The FormView bind with Entity Datasource.
FormView 与实体数据源绑定。
I want those value to be trim before saving, what is the best way to do this ?
我希望在保存之前修剪这些值,最好的方法是什么?
采纳答案by L.B
formView.Controls
.OfType<System.Web.UI.WebControls.TextBox>()
.ToList()
.ForEach(t => t.Text = t.Text.Trim());
回答by Tim Rogers
The string.Trim()method is what you want.
该string.Trim()方法是你想要的。
回答by Nikhil Agrawal
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.
用修剪过的文本替换你的文本框文本。然后编写代码将其从文本框文本保存在数据库中。这样,您在 UI 和后端中的文本将相同并得到更正。
mytextbox.Text = mytextbox.Text.Trim();
//save textbox text in database
回答by Vinod
Try this:
尝试这个:
TextBox.Text.Trim();
回答by Tilak
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.
我曾经有过这样类似的场景,我需要用参数记录每个 SQL 查询(ExecuteReader、ExecuteNonQuery)。
What i simply did was to create a static method excepting IDBCommand. Then logged all the parameters in it. After that called the executed.
我所做的只是创建一个除 IDBCommand 之外的静态方法。然后记录其中的所有参数。之后被称为执行者。
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.
现在我已经通过代码用 CommandIntercepter.ExecuteNonQuery(command) 替换了 command.ExecuteNonQuery。
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
如果你在 Bind(String propertyName) 中有这样的拦截点,你可以在那里做修剪。或者您可以创建 TrimBind 函数,并调用 TrimBind 而不是 Bind
void TrimBind(String propertyName)
{
...//do trimming
Bind(propertyName);
}
回答by Imran Rizvi
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.
如果您不知道要使用多少个文本框,或者有多个文本框并且您想将它们全部修剪,即使是多个页面,我更喜欢为 TextBox 创建扩展属性并覆盖其 Text 属性以返回始终修剪的值。
回答by Mironline
you can also trim your data before sending to any data-source by using OnItemInsertingor OnItemUpdatingevents
您还可以在使用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();
}
}
}
}
回答by hjgraca
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
这将遍历您的所有控件。你必须命名你的主网格,在这种情况下<Grid x:Name="Grid1">,然后在你的代码后面,你调用方法
TrimTextBoxes(this.Grid1);
This will trim all your textboxes within your main grid. Hope it helps
这将修剪主网格中的所有文本框。希望能帮助到你
回答by Sarawut Positwinyu
Appreciating all the answer.. I come up with this finally.
感谢所有的答案..我终于想出了这个。
Trimimg on Formview
在 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
在 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();
}
}
}
回答by Montaldo
public string PersonName
{
get { return txtPersonName.Text.Trim(' '); }
set { txtPersonName.Text = value; }
}

