Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting error when clicking a button by javaScript to call codeBehind function

I need to call some codeBehind function from JavaScript. For this, I have a hidden button (asp:Button).

When I click this button from javaScript like:

var btn = document.getElementById("btnHidden");
btn.click(); 

I got an error:

System.Web.HttpException: The state information is invalid for this page and might be corrupted. 

Could you please help me?

like image 403
Shahriar Avatar asked Aug 01 '26 19:08

Shahriar


2 Answers

i tried like this and is working

aspx

<head runat="server">
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript">
        function btnClick()
        {
            alert("clicked!!!");

            var btn=document.getElementById('<%=Button1.ClientID%>');
            btn.click();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        test page
        <input type="button" id="btn" onclick="btnClick()" value="Click" />

        <asp:Button ID="Button1" style="display:none" runat="server" Text="Button" OnClick="Button1_Click" />

    </div>
    </form>
</body>

aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

In the first button client click i tried to raise the second button click event.

like image 163
Mahesh KP Avatar answered Aug 03 '26 07:08

Mahesh KP


first you have to hide this button with css not server side , i mean display:none not Visible=false;

like this :

Button1.Attributes.CssAttributes.Add("Display","None");

then you should use ClientID

document.getElementById('<%=Button1.ClientID%>').click();

try to set UseSubmitBehaviour=false on button

hope this helps

like image 30
Mahmoud Farahat Avatar answered Aug 03 '26 09:08

Mahmoud Farahat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!