简体   繁体   中英

Calling the button.click() event from javascript doesn't fire

I have the following source in aspx:

<div>
    <asp:HiddenField ID="hidValue" runat="server" />
    <asp:Button runat="server" ID="hidButton" OnClick="hidButton_Click"    /> 
    <script type="text/javascript">
        function ExtendPanel(PanelNumber) {
            var hidValue = document.getElementById('<%=hidValue.ClientID %>');
            hidValue.value = PanelNumber;

            document.getElementById('<%=hidButton.ClientID%>').fireEvent("onclick");
        }
    </script>
</div>

In my code behind, I have the following C# function declared:

protected void hidButton_Click(object sender, EventArgs e)
{
    int PanelNumber = int.Parse(hidValue.Value);
    ... do something with PanelNumber ...
}

When I click on the button using the mouse, "hidButton_Click" function is normally executed. However, when the javascript function ExtendPanel(PanelNumber) is executed, the click event seems to be fired, but the function is not executed.

Replace this

document.getElementById('<%=hidButton.ClientID%>').fireEvent("onclick");

with

__doPostBack('hidButton','OnClick');

尝试这个

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

在下面试试这个

$('#<%=hidButton.ClientID%>').click();

I got the same issues and resolved to put

OnClientClick="javascript:return true;"

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