通过triming我的意思是。
名字“John” - >修剪为“约翰”
姓氏“Lennon” - >修剪为“列侬”
我在FormView中有那些Textbox,使用Bind(“FirstName”)绑定属性。
FormView与实体数据源绑定。
我希望在保存之前修剪这些值,最好的方法是什么?
答案 0 :(得分:6)
formView.Controls
.OfType<System.Web.UI.WebControls.TextBox>()
.ToList()
.ForEach(t => t.Text = t.Text.Trim());
答案 1 :(得分:4)
试试这个:
TextBox.Text.Trim();
答案 2 :(得分:0)
string.Trim()
方法就是你想要的。
答案 3 :(得分:0)
用修剪过的文本替换文本框文本。然后编写代码,将其从Textbox文本保存到数据库中。这样,UI和后端中的文本将相同并更正。
mytextbox.Text = mytextbox.Text.Trim();
//save textbox text in database
答案 4 :(得分:0)
String.Trim() //for triming
为了节省人力,你可以做的是创建一个帮助方法,并在任何绑定集之前拦截它。
我曾经有过类似的情况,我需要用参数记录每个SQL查询(ExecuteReader,ExecuteNonQuery)。
我只是创建一个静态方法,除了IDBCommand。然后记录其中的所有参数。之后叫做执行。
实施例
public static CommandIntercepter
{
void ExecuteNonQuery(IDbCommand command)
{
...//logged all parameters
command.ExecuteNonQuery()
}
}
现在我通过代码用CommandIntercepter.ExecuteNonQuery(命令)替换了command.ExecuteNonQuery。
这样,我就可以在不同的代码点记录单个查询参数。
如果你在Bind(String propertyName)中有这样的拦截点,你可以在那里修剪。或者您可以创建TrimBind函数,并调用TrimBind而不是Bind
void TrimBind(String propertyName)
{
...//do trimming
Bind(propertyName);
}
答案 5 :(得分:0)
如果您只有两个文本框,则可以使用
string firstName = txtFirstName.Text.Trim();
string lastName = txtLastName.Text.Trim();
如果您不知道要使用多少个文本框或者有多个文本框并且您想要修剪它们,即使对于多个页面,我更喜欢为TextBox创建扩展属性并覆盖其Text属性以始终返回修剪价值。
答案 6 :(得分: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();
}
}
}
}
答案 7 :(得分: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);
这将修剪主网格中的所有文本框。 希望它有所帮助
答案 8 :(得分:0)
赞赏所有答案..我最终想出了这个。
Formview上的Trimimg
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();
}
}
}
答案 9 :(得分:0)
public string PersonName
{
get { return txtPersonName.Text.Trim(' '); }
set { txtPersonName.Text = value; }
}
答案 10 :(得分: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