把我做第一次做系統時對重填按鈕的實現記錄如下:
第一次用ImageButton可以設定背景圖片,然後在背景Click事件中寫如下
this.TxtCallNo.Text = "";
this.TxtCaller.Text = "";
this.TxtTranDate.Text = "";
this.TxtTranLimit.Text = "";
this.TxtTranNo.Text = "";
this.TxtTranPres.Text = "";
this.RadioButton1.Checked = false;
this.RadioButton2.Checked = false;
this.DlstUnit.SelectedIndex = 0;
第二次也是用ImageButton,在Click事件中如下寫:
foreach (Control c in form1.Controls)
{
if (c is TextBox)
((TextBox)c).Text = "";
if (c is DropDownList)
((DropDownList)c).SelectedIndex = 0;
if (c is RadioButton)
((RadioButton)c).Checked = false;
}
第三次是用<a href="#"><img src="images/but2.gif" width="61" onclick="ReSet();" />
用JavaScript寫ReSet()方法:
function ReSet()
{
var len=document.form1.elements.length;
var i;
for (i=0;i<len;i++)
{
if (document.form1.elements[i].type=="text")
{
document.form1.elements[i].value="";;
}
if(document.form1.elements[i].type=="radio")
{
document.form1.elements[i].checked=false;
}
}
var obj1=document.getElementById("DlstCallerWay");
var obj5=document.getElementById("TxtRemark");
obj5.value="";
obj1.selectedIndex=0;
//不能獲得DropDownList 的用戶端type,不知道怎麼獲得的JS沒學過。
//當文字框的TextMode設為multiline時也不能獲得type就只能一個按照ID獲得
}
這三種方法都是清空不是重設或重填,因為有時重設的要求是控制項內有資料單擊重設按鈕後仍保留原來的
資料,例如TextBox1載入頁面時的值為“胡帥”,單擊後仍是“胡帥”,上面的方法單擊後都清空了。
第四種方法:
<input type="image" src="images/but2.gif" name="reset"
onclick="form1.reset();return false;" />