简体   繁体   中英

How to raise click event of a client side `reset ` button from code behind in asp.net?

I am trying to raise click event of

<input type="reset" id="btnreset" class="ButtonText"/>

using (in code behind):

ScriptManager.RegisterStartupScript(this, this.GetType(), "pop", "<script>alert('" + ds.Tables[0].Rows[0][0].ToString() + "'); document.getElementById('btnreset').click();</script>", false);

EDIT:

ScriptManager.RegisterStartupScript(this, this.GetType(), "pop", "<script>function myFunction() { alert('" + ds.Tables[0].Rows[0][0].ToString() + "'); document.getElementById('btnreset').click(); }</script>", false);

There are no errors in console. But it doesn't work......

This is because your script is possibly executing before document loads,

Try,

ScriptManager.RegisterStartupScript(this, this.GetType(), "pop", "<script>function myFunction() { alert('" + ds.Tables[0].Rows[0][0].ToString() + "'); document.getElementById('btnreset').click(); }</script>", false);

And call this function when documents loads like,

<body onload="myFunction()">

Or you can do it in JavaScript code too,

window.onload="myFunction()";

Hope it helps, thanks.

You are trying to reset form that is not set (changed) yet.

You are resetting form on page load that wont have any effect as reset will reset the form values to the one that you have when page is loaded.

To see the effect of reset you should change the control the form control values first and then reset the form.

You probably need to initialize the value eg setting the all textboxes to blank etc.

You can better understand with this example.

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

Here we are reset form on load with no effect but if we change the value in textbox and reset the form using button the initial value of textbox will be restored.

To initialize value of inputs you can do something like

$('input:text').val('');
$(':checkbox').prop('checked', false);

If you want to initial the form to the state that is at the first load then you can use Response.Redirect on the postback. It will initialize the form controls with minimum effort. You can use some session to show some message when page loads after redirect eg showing that previous record saved successfully. You should not redirect in case of failure.

<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 file like:

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')");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM