3

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?

1
  • <asp:Button style="display:none"> </asp:Button> Commented Dec 26, 2012 at 12:05

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

You can edit your Web.config file:

<pages validateRequest="false" enableEventValidation="false" viewStateEncryptionMode ="Never"> 

Check this blog for details.

Comments

0

It should be like this,

var btn = document.getElementById('<%= btnHidden.ClientID %>').value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.