简体   繁体   中英

Calling asp net button click event with JS will not call server side event

Sorry to post perhaps a silly problem here, but I'm at my wits end with it. I have a hidden field with a button inside an update panel like so:

<asp:UpdateProgress runat="server" ID="updprCompLines" AssociatedUpdatePanelID="updpanCompLines">
        <ProgressTemplate> 
            <img src="../Images/ajax-loader.gif" alt="Please wait..." /> </ProgressTemplate>
        </asp:UpdateProgress>

        <asp:UpdatePanel runat="server" ID="updpanCompLines" UpdateMode="Conditional">

            <%--<Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnFillMembers" />
            </Triggers>--%>

            <ContentTemplate>
            <div>
                <asp:HiddenField ID="hdnField" runat="server" />
                <asp:Button ID="btnFillMembers" runat="server" style="display:none;" 
                    Text="DummyButton" onclick="btnFillMembers_Click"  />                   
            </div>

The update panel also contains a gridview and inside my gridview I have a link button:

 <ItemTemplate>
     <asp:LinkButton ID="lkbtBenefName" runat="server" Text='<%#Eval("COMPETENCE_CODE") %>' 
      OnClientClick='<%#Eval("COMPETENCE_LINE_ID", "return SelectedCompetence({0})") %>'/> 
 </ItemTemplate>

The call is to a JS function that is supposed to call the above button:

<ajaxToolkit:ToolkitScriptManager runat="Server" EnablePartialRendering="true" ID="ScriptManager1" EnablePageMethods="true"/>   

<script type="text/javascript">
    function SelectedCompetence(CompetenceLineId) {

        document.getElementById('<%= hdnField.ClientID %>').value = CompetenceLineId;

        var clickButton = document.getElementById('<%= btnFillMembers.ClientID %>');
        clickButton.click();

    }
</script>

Button click event method:

protected void btnFillMembers_Click(object sender, EventArgs e)
{
    lblGvMemError.Text = "";
    lblGvMemError.ForeColor = Color.Red;

    if (hdnField.Value != null || hdnField.Value.ToString() != "")
    {
        try
        {
            int CompLineId = Convert.ToInt32(hdnField.Value);
            GetSelectedCompLineMembers(CompLineId);
        }
        catch (Exception ex)
        {
            lblGvMemError.Text = "Error: " + ex.Message;
        }
    }

    updpanCompLinesMembers.Update();
}

The problem is that while debugging, it never runs the click event and it doesn't give any error messages either. I don't understand, I have a similar form where this works; I don't get why it doesn't here... Any help please?

Have you confirmed that SelectedCompetence is being called via an alert or similar? Additionally, have you made sure that the clickButton variable is being assigned to successfully?

I know this isn't an answer, but don't yet have the reputation to comment, and sometimes it's the easy stuff so maybe this will help! :)

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