2

我的代码:

<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 我想停止服务器端函数继续我应该怎么做?

4

1 回答 1

1

找到了我的解决方案,希望对您有所帮助:

 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);
    }
于 2013-05-04T08:16:40.243 回答