2012-09-18 76 views
0

我想提供一個好的取消彈出'如果'gridview沒有行,我認爲這會像客戶端腳本,但是當我點擊確定時,控制跳過方法的其餘部分。無論如何,我可以執行此驗證或警報並將控件移至下一個語句?驗證回發如果gridview爲空

protected void btnRunD_Click(object sender, EventArgs e) 
{ 
     try 
     { 
      int iESStatus = 0; 
      int iRunStatus = 0; 
      ApplicationUser au = (ApplicationUser)Session["ApplicationUser"]; 
      if (UploadFileGrid.Rows.Count <= 0) 
      { 
       ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),  "err_msg", "confirm('No new data files have been uploaded. Are you sure you want to run EYDS Processing Module?');", 
      //true); 
      } 

      if (au.RoleID != 2) //RoleID is not Admin! 
      { 
       ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "err_msg", "alert('You do not have enough privileges to use this functionality!');", 
      true); 
      } 
      else 
      {.........} 
} 

我有困難的部分是在這裏:
我想提供一個確定取消彈出「如果」 gridview的沒有行

if (UploadFileGrid.Rows.Count <= 0) 
    { 
     ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "err_msg", "confirm('No new data files have been uploaded. Are you sure you want to run?');",  
    } 
+0

你可以使用JavaScript函數做到這一點,而無需回發然後是否有任何特定的需要在回發函數上調用JavaScript函數? – Priya

+0

顯示更多代碼。你確實需要在'確定'按鈕點擊執行? –

+0

@Piya如何檢查我的gridview在javascript中是否爲空? – Aravindh

回答

3

假設你的按鈕是一樣的東西此:

<asp:Button ID="btnDoSomething" runat="server" Text="Do Something" OnClientClick="return GetConfirmation();" OnClick="btnDoSomething_Click" /> 

然後添加如下JavaScript函數:

function GetConfirmation() { 
    var rowscount = document.getElementByID(<%=UploadFileGrid.ClientID%>).rows.length; 
    return rowscount > 0 || confirm('No new data files have been uploaded. Are you sure you want to run?'); 
} 

然後當用戶按下確定按鈕顯示確認按鈕時它將被回發。

+0

如果按鈕位於更新面板內,此功能可以工作嗎? – Aravindh

+0

+1在我看來'return rowscount> 0 ||確認('沒有新的數據文件已經上傳,你確定要運行?');'看起來更好 –

+0

@YuriyRozhovetskiy,歡呼隊友,更新 – CjCoax

1

可以使用JavaScript或jQuery的做到這一點:

$("#<%=btnApprove.ClientID %>").click(function() { 
      if ($("#<%=UploadFileGrid.ClientID %> ").find("tr").length == 0) { 
       if (confirm('No new data files have been uploaded. Are you sure you want to run?')) { 
        return true; 
       } 
       else { 
        return false; 
       } 
      } 

     }); 

,當用戶按下OK,你的頁面會postbacked,你可以寫你想在那之後執行的代碼。 我希望這段代碼能幫助你。

+0

如果按鈕位於更新面板內,這樣會工作嗎? – Aravindh

+0

是的....它會工作。你可以檢查你的代碼。 – Priya