如何从asp.net中的代码后面提出客户端`reset`按钮的click事件?

时间:2015-05-18 06:18:35

标签: javascript c# jquery asp.net

我正在尝试提升

的点击事件
For linux

使用(在后面的代码中):

<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);

控制台中没有错误。但它没有用......

3 个答案:

答案 0 :(得分:0)

这是因为您的脚本可能在文档加载之前执行,

尝试,

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()";

希望它有所帮助,谢谢。

答案 1 :(得分:0)

您正在尝试重置尚未设置(已更改)的表单。

您正在重置页面加载时不会产生任何影响的表单,因为重置会将表单值重置为加载页面时的表单值。

要查看重置效果,您应首先更改控件的表单控件值,然后重置表单。

您可能需要初始化值,例如将所有文本框设置为空白等。

您可以通过此示例更好地理解。

Live Demo

<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。它将以最小的努力初始化表单控件。您可以使用某个会话在重定向后加载页面时显示一些消息,例如显示上一条记录已成功保存。如果失败,您不应重定向。

答案 2 :(得分:0)

<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')");
}
相关问题