0

一旦我将 JavaScript 代码添加到程序中,按钮 OnClick 事件就不会触发,有什么想法为什么会发生这种情况?,我已经传递了进度条的 C# 代码,单击它时会加载进度条。我想知道为什么按钮单击事件未触发。

HTML 和 JavaScript 代码

            <asp:Button ID="Confirm_btn" CssClass="confirmBtn" runat="server" Text="Generate Presentation" UseSubmitBehavior="true" 
                          ClientIDMode="Static" OnClick = "Confirm_Click" />

                         </td></tr>                          
                     <tr><td colspan="2" align="center">
                        <div id="progressbar" style="width:500px"></div>
                        </td></tr>    
                        </table>
                    </div>

                    <asp:Label ID="error" runat="server" ClientIDMode="Static" 
                        EnableViewState="False" Font-Names="Arial"></asp:Label>                    
                </center>
            </div>     

</asp:Content>
  <asp:Content ID="Content3" ContentPlaceHolderID="ScriptContent" runat="server">
    <script type="text/javascript">
        $.updateProgressbar = function () {
            //Calling PageMethod for current progress
            PageMethods.OperationProgress(function (result) {
                //Updating progress
                $("#progressbar").progressbar('value', result.progress)
                //If operation is complete
                if (result.progress == 100) {
                    //Enable button
                    $("#Confirm_btn").attr('disabled', '');
                }
                //If not
                else {
                    //Reset timer
                    setTimeout($.updateProgressbar, 500);
                }
            });
        };

        $(document).ready(function () {
            //Progressbar initialization
            $("#progressbar").progressbar({ value: 0 });
            //Button click event
            $("#Confirm_btn").click(function (e) {
                e.preventDefault();
                //Disabling button
                $("#Confirm_btn").attr('disabled', 'disabled');
                //Making sure that progress indicate 0
                $("#progressbar").progressbar('value', 0);
                //Call PageMethod which triggers long running operation
                PageMethods.Operation(function (result) {
                    if (result) {
                        //Updating progress
                        $("#progressbar").progressbar('value', result.progress)
                        //Setting the timer
                        setTimeout($.updateProgressbar, 500);
                    }
                });
            });
        });
    </script>
</asp:Content>

ProgressBar 的 C# 代码

/// <summary>
    /// PageMethod for triggering long running operation
    /// </summary>
    /// <returns></returns>
    [System.Web.Services.WebMethod(EnableSession = true)]
    public static object Operation()
    {
        HttpSessionState session = HttpContext.Current.Session;

        //Separate thread for long running operation
        ThreadPool.QueueUserWorkItem(delegate
        {
            int operationProgress;
            for (operationProgress = 0; operationProgress <= 100; operationProgress = operationProgress + 2)
            {
                session["OPERATION_PROGRESS"] = operationProgress;
                Thread.Sleep(1000);
            }
        });

        return new { progress = 0 };
    }

    /// <summary>
    /// PageMethod for reporting progress
    /// </summary>
    /// <returns></returns>
    [System.Web.Services.WebMethod(EnableSession = true)]
    public static object OperationProgress()
    {
        int operationProgress = 0;

        if (HttpContext.Current.Session["OPERATION_PROGRESS"] != null)
            operationProgress = (int)HttpContext.Current.Session["OPERATION_PROGRESS"];

        return new { progress = operationProgress };
    }
}

按钮单击事件的 C# 代码

protected void Confirm_Click(object sender, EventArgs e)
{
   // my process run here 
}
4

2 回答 2

2

默认情况下,WebForms 控件不会发出id存在于ID服务器标记属性中的确切属性。你需要为你的 jQuery 选择器使用类似的东西:

$('#<%: Confirm_btn.ClientID %>')
于 2013-02-20T10:03:15.410 回答
0
$("#Confirm_btn").live('click', function (e) {

});

试试这个,肯定有效

别的

$("#Confirm_btn").on('click', function (e) {

});

干杯

帕尼*

于 2013-02-20T10:24:33.230 回答