2013-05-04 81 views
2

我的代碼:停止服務器端函數在C#

<asp:LinkButton ID="Search" CssClass="button" runat="server" Text='<%$Resources:Strings,Search %>' OnClick="Search_Click" /> 

服務器端:

protected void Search_Click(object sender, EventArgs e) 
{ 
    var script = "CheckIfItemSelected()"; 
    ScriptManager.RegisterStartupScript(this, GetType(), "MyScript", script, true); 
} 

客戶端:

function CheckIfItemSelected() { 
     var SalesmanSelectedItem = $find('<%= SalesmanGrid_grd.ClientID %>').get_masterTableView().get_selectedItems().length; 
     var ProductsSelectedItem = $find('<%= SalsemanProductsGrid_grd.ClientID %>').get_masterTableView().get_selectedItems().length; 

     if (SalesmanSelectedItem == 0 && ProductsSelectedItem == 0) { 

      $("#<%=ErrorMsg.ClientID %>").html("please select a salesman at least<br>please select a product at least"); 
      return false; 
     } 
     if (SalesmanSelectedItem == 0) { 
      $("#<%=ErrorMsg.ClientID %>").html("please select a salesman at least"); 
      return false ; 
     } 
     if (ProductsSelectedItem == 0) { 
      $("#<%=ErrorMsg.ClientID %>").html("please select a product at least"); 
      return false; 
     } 
     $("#<%=ErrorMsg.ClientID %>").html(""); 
     return true; 
    } 

的問題是,如果我的客戶端功能返回false我想停止服務器端功能,我應該怎麼做?

回答

1

發現我的解決方案,我希望它WLD幫助:

protected void Search_Click(object sender, EventArgs e) 
{ 
    int SelectedSalesmans= SalesmanGrid_grd.SelectedItems.Count; 
    int SelectedProducts = SalsemanProductsGrid_grd.SelectedItems.Count; 
    if (SelectedSalesmans == 0) 
    { 
     var script = "DisplayErrorMsg(\"Please Choose a salesman AT least. \")"; 
     ScriptManager.RegisterStartupScript(this, GetType(), "MyScript", script, true); 
     return; 
    } 
    if (SelectedProducts == 0) 
    { 
     var script = "DisplayErrorMsg(\"Please Choose a Product AT least. \")"; 
     ScriptManager.RegisterStartupScript(this, GetType(), "MyScript", script, true); 
     return; 
    } 
} 

客戶端:

function DisplayErrorMsg(msg) { 
     $("#<%=ErrorMsg.ClientID %>").html(msg); 
    }