如何使用方法之间的返回

2 c# asp.net return

在我的程序中,我有一个按钮点击,从该onclick事件我调用一个方法进行一些文本框验证.代码是:

  protected void btnupdate_Click(object sender, EventArgs e)
  {
     CheckValidation();
    //Some other Code
  }

  public void CheckValidation()
  {
    if (txtphysi.Text.Trim() == "")
    {
       lblerrmsg.Visible = true;
       lblerrmsg.Text = "Please Enter Physician Name";
       return;
    }
    //Some other Code
  }
Run Code Online (Sandbox Code Playgroud)

这里如果txtphysi.text为null然后它进入循环并使用return然后它只来自 CheckValidation()方法并且它继续在btnupdate_Click事件中,但在这里我也想停止执行过程btnupdate_Click.我该怎么做?

Har*_*aid 9

在我的理解中,这是非常简单的编程逻辑,你需要在这里申请..

这是BooleanCheckValidation()方法返回而不是返回,void以便父函数从函数的执行中知道状态.

protected void btnupdate_Click(object sender, EventArgs e)
{
    var flag = CheckValidation();
    if(!flag)
        return;
    //Some other Code
}

public bool CheckValidation()
{
    var flag = false; // by default flag is false
    if (string.IsNullOrWhiteSpace(txtphysi.Text)) // Use string.IsNullOrWhiteSpace() method instead of Trim() == "" to comply with framework rules
    {
       lblerrmsg.Visible = true;
       lblerrmsg.Text = "Please Enter Physician Name";
       return flag;
    }
    //Some other Code
    flag = true; // change the flag to true to continue execution
    return flag;
}
Run Code Online (Sandbox Code Playgroud)