清除字段内容后保存。netc# - Clear content from fields after save .net c#

- 此内容更新于:2016-02-01
主题:

我有aspx表单文本框和显示数据表格。当我插入数据文本框显示在gridview后点击“保存”。我需要明确的领域的内容后点击“保存”。我尝试了很多东西,在谷歌搜索,但没有效果。我在。net新..有人能帮吗?这是我的save按钮功能:这是我的页面加载:

原文:

I have aspx form with textboxes and gridview. When i insert data to textbox it's shows on the gridview after clicking "Save". I need to clear the content of fieds after i click on "Save". I tried alot of things and searched in google but nothing works. I am new at .net.. Can someone help please? This is my save button fuction:

protected void btnSave_Click(object sender, EventArgs e)
{
    Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text,                DateTime.Parse(txtBirthdate.Text)); 
    grdvUsers.DataBind(); 
}

This is my pageLoad:

protected void Page_Load(object sender, EventArgs e)
{
    grdvUsers.DataSource = Context1.sp_GetAllUserTest(); // select all users into gridview. datasorce = the data we want to dispaly.
    grdvUsers.DataBind();
}
网友:“我尝试了很多东西,在谷歌搜索,但没有工作”那么你有试过和以何种方式没有工作吗?

(原文:"I tried alot of things and searched in google but nothing works" so what have you tried and in what way didn't it work?)

网友:我试着:foreach(var项Page.Controls){如果文本框(项目){((文本框)项)。文本="";}}:foreach(控制Parent.Controlsctl){如果(ctl.GetTyep()==文本框)ctl。文本="";}和更多..但它不回应..仍然保存后显示内容

(原文:I tried this: foreach (var item in Page.Controls) { if (item is TextBox) { ((TextBox)item).Text = ""; } } and this: foreach ( Control ctl in Parent.Controls) { if (ctl.GetTyep()== TextBox) ctl.text = ""; } and more.. but it does not respond.. still showing content after saving)

网友:你试过清算领域通过设置文本属性保存函数结束时?例如txtName。文本=string.Empty

(原文:Have you tried clearing the field by setting the text property at the end of your save function? e.g. txtName.Text = string.Empty)

网友:当我做txtName。文本字符串=。空的工作,但我想做多个领域,在未来也许字段将被添加。

(原文:When i do txtName.Text = string.Empty its working but i want to do it to more than one field, and maybe fields will be added in the future.)

解决方案:
只是做一个清理控制方法要明确:保存后,称之为:
原文:

Simply make a method for clearing the controls that you want to clear:

private void ClearControls()
{
 txtName.Text =""; // resetting textbox
 txtEmail.Tex="";
 txtMobile.Text ="";
 ddlSomeDropDown.SelectedIndex = -1; // reset dropdown
 somecheckBox.Checked = false;  // reset checkbox
 someRadio.Checked = false;   // reset radio
 ..................
 .................
 // more controls here
}

and call it after saving :

protected void btnSave_Click(object sender, EventArgs e)
{
    Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text,                DateTime.Parse(txtBirthdate.Text)); 
    grdvUsers.DataBind();
    ClearControls();
}
解决方案:
文本字段分配一个空字符串。
原文:

Just assign text fields a empty string.

protected void btnSave_Click(object sender, EventArgs e)
{
    Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text, DateTime.Parse(txtBirthdate.Text)); 

 txtName.Text = String.Empty;
 txtEmail.Text= String.Empty;
 txtMobile.Text= String.Empty;
 txtBirthdate.Text= String.Empty;

    grdvUsers.DataBind(); 
}