清除ASP.net形式的所有领域 [英] Clear all fields in ASP.net form

查看:101
本文介绍了清除ASP.net形式的所有领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法来重置表单中的所有字段?

Is there an easy way to reset all the fields in a form?

我在我的asp.net周围形成100控制并有提交和复位按钮。

I have around 100 controls in my asp.net form and there are submit and reset buttons.

我如何在字段中的所有值空当用户点击复位按钮?

How do I make all values in the fields null when user hits reset button?

我有很多的下拉框,文本框,复选框。

I have a lot of dropdown boxes, textboxes, checkboxes.

推荐答案

遍历所有页面上的控制,如果控制TextBox类型,设置Text属性的String.Empty

Loop through all of the controls on the page, and if the control is type TextBox, set the Text property to String.Empty

protected void ClearTextBoxes(Control p1)
{
    foreach (Control ctrl in p1.Controls)
    {
        if(ctrl is TextBox)
        {
             TextBox t = ctrl as TextBox;

             if(t != null)
             {
                  t.Text = String.Empty;
             }
        }
        else
       {
           if (ctrl.Controls.Count > 0)
           {
               ClearTextBoxes(ctrl);
           }
        }
    }
}

然后调用它在点击事件是这样的:

Then to call it in your click event like this:

 ClearTextBoxes(Page);

这篇关于清除ASP.net形式的所有领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆