开发者

Clear asp.net form at runtime

Whats the easiest way to clear an asp.net form at ru开发者_如何学Gontime using c#.

Thanks Sp


I assume you want to clear input boxes, dropdowns etc. This can be done the following way in code to recursivly clear all data.

foreach( var control in this.Controls )
{
   ClearControl( control );
}

and the recursive function

private void ClearControl( Control control )
{
    var textbox = control as TextBox;
    if (textbox != null)
        textbox.Text = string.Empty;

    var dropDownList = control as DropDownList;
    if (dropDownList != null)
        dropDownList.SelectedIndex = 0;

    // handle any other control //

    foreach( Control childControl in control.Controls )
    {
        ClearControl( childControl );
    }
}


I have used the following JS/c# to clear the form.

c# to add the js call onload

  Page.ClientScript.RegisterStartupScript(typeof(WebForm3), "ClearPage", "ClearForm();", true);

the JS to clear the form

function ClearForm() {
            var AllControls = document.getElementById('ctl00_ContentPlaceHolder1_PnlAll')
            var Inputs = AllControls.getElementsByTagName('input');
            for (var y = 0; y < Inputs.length; y++) {
                // define element type
                type = Inputs[y].type
                // alert before erasing form element
                //alert('form='+x+' element='+y+' type='+type);
                // switch on element type
                switch (type) {
                    case "text":
                    case "textarea":
                    case "password":
                        //case "hidden":
                        Inputs[y].value = "";
                        break;



                }
            }


        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜