[英]How to raise click event of a client side `reset ` button from code behind in asp.net?
我正在尝试引发的点击事件
<input type="reset" id="btnreset" class="ButtonText"/>
使用(在后面的代码中):
ScriptManager.RegisterStartupScript(this, this.GetType(), "pop", "<script>alert('" + ds.Tables[0].Rows[0][0].ToString() + "'); document.getElementById('btnreset').click();</script>", false);
编辑:
ScriptManager.RegisterStartupScript(this, this.GetType(), "pop", "<script>function myFunction() { alert('" + ds.Tables[0].Rows[0][0].ToString() + "'); document.getElementById('btnreset').click(); }</script>", false);
控制台中没有错误。 但这行不通......
这是因为您的脚本可能在文档加载之前就已执行,
尝试,
ScriptManager.RegisterStartupScript(this, this.GetType(), "pop", "<script>function myFunction() { alert('" + ds.Tables[0].Rows[0][0].ToString() + "'); document.getElementById('btnreset').click(); }</script>", false);
并在文档加载时调用此函数,
<body onload="myFunction()">
或者您也可以使用JavaScript代码来完成此操作,
window.onload="myFunction()";
希望能有所帮助,谢谢。
您正在尝试重置尚未设置(更改)的表单。
您正在重置页面加载时的表单,该操作不会产生任何效果,因为重置会将表单值重置为页面加载时的表单值。
若要查看重置的效果,您应该先更改控件的表单控制值,然后重置表单。
您可能需要初始化该值,例如将所有文本框设置为空白等。
您可以通过此示例更好地理解。
<form id="frm">
<input value="123" />
<input type="button" id="btn" onclick="myfun();" />
</form>
document.getElementById('frm').reset();
alert("resetting form wont make the textbox empty on load");
function myfun(){
document.getElementById('frm').reset();
}
在这里,我们将在加载时重置表单,但没有任何效果,但是,如果我们更改文本框中的值并使用按钮重置表单,则文本框的初始值将被恢复。
要初始化输入值,您可以执行以下操作
$('input:text').val('');
$(':checkbox').prop('checked', false);
如果要将表单初始化为第一次加载时的状态,则可以在回发中使用Response.Redirect 。 它将以最小的努力初始化表单控件。 重定向后页面加载时,您可以使用某个会话来显示一些消息,例如,显示先前的记录已成功保存。 如果发生故障,则不应重定向。
<head runat="server">
<title></title>
<script type="text/javascript">
function YorFnName(str) {
alert(str);
return false;
}
</script>
<body>
<form id="form1" runat="server">
<div>
<input type="reset" runat="server" id="btnreset" class="ButtonText" />
</div>
</form>
cs文件,例如:
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
//whatever you want to pass , write it in yourfnname as a parameter
btnreset.Attributes.Add("onclick", "return YorFnName('test')");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.