2010-06-02 61 views
3

什么是在运行时使用c#清除asp.net表单最简单的方法。在运行时清除asp.net表单

感谢 SP

+0

你所说的 “在运行时” 是什么意思?这是否意味着用户正在查看该页面? – 2010-06-02 16:24:43

+1

如果你正在谈论做这个客户端,然后使用form.reset() – 2010-06-02 17:36:38

+0

@Jeff舒马赫忘了那一个....另一种选择是把复位按钮在表格 – ggonsalv 2010-06-03 03:44:56

回答

0

我已经使用以下JS/C#以清除的形式。

C#添加的JS调用的onload

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

的JS以清除表单

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; 



       } 
      } 


     } 
1

我假设你要清除输入框,下拉菜单等,这是可以做到在以下代码的方式来recursivly清除所有数据。

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

和递归函数

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); 
    } 
} 
+0

感谢您的这一点,我无法得到它作为即时通讯使用HTML控件,我将文本框切换到htmlinputtext,但仍然失败。 我已经用一些js来清除表格 感谢您的回复 Sp – Steven 2010-06-03 08:18:03