2013-02-20 3 views
0

JavaScript 코드를 프로그램에 추가하면 버튼 OnClick 이벤트가 실행되지 않고 왜 이런 현상이 발생합니까? 진행 막대의 C# 코드를 전달하고 클릭하면 진행률 막대가로드됩니다. 버튼 클릭 이벤트가 발생하지 않는 이유를 알고 싶습니다.click 이벤트가 발생하지 않습니다.

HTML과 자바 스크립트 코드 : ProgressBar의를 들어

  <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> 

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 
} 
+1

'OnClientClick = "시도해보기 firm_Click "' – HShbib

+0

나는 OnClientClick ="Confirm_Click "을 시도했다. 이것은 jscpirt 코드를 실행하기 위해 예외를 던진다. C# 클래스에서 – user2082903

+0

메서드는 OnClick이다 – user2082903

답변

2

WebForms 컨트롤은 기본적으로 서버 태그의 ID 특성에있는 정확한 id 특성을 방출하지 않습니다. 당신은 당신의 jQuery 선택기이 비슷한을 사용해야합니다 :

$('#<%: Confirm_btn.ClientID %>') 
+0

무례한 것에 대해 유감스럽게 생각한다. 그러나 나는 이것을 시험해 보았다. 내가 lastr 코드 조각에 뿌린 방법을 클릭하십시오 – user2082903

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

}); 

이 시도, 확실히

그렇지

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

}); 

건배

Phani *

작품
+0

tese 작업의 도움을 주셔서 감사합니다 – user2082903

관련 문제