如何在asp.net中的textchanged事件上输入文本框时避免按钮单击事件

时间:2016-04-07 22:34:14

标签: c# asp.net methods

我有一个文本框,需要一个月作为值,当更改时,text_changed事件会触发,其中包括vaildtextbasemonth()方法

 private void vaildtextbasemonth()
{
  this_month = DateTime.Now.Month;
  current_year = DateTime.Now.Year;
  this_year = Convert.ToInt32(TxtBase.Text);
  current_month = DateTime.Now.Month;
  if (swyearerror != 0)
  {
   swyearerror = 0;
   TxtBase.Focus();
   }
   else if (Information.IsNumeric(TxtBase1.Text))
   {
   base_month = Convert.ToInt32(TxtBase1.Text);

  if (base_month > this_month & current_year == this_year)
   {
   string message = "Base month may not be in future";
   System.Text.StringBuilder sb = new System.Text.StringBuilder();
   sb.Append("<script type = 'text/javascript'>");
   sb.Append("window.onload=function(){");
   sb.Append("alert('");
   sb.Append(message);
   sb.Append("')};");
   sb.Append("</script>");
  ClientScript.RegisterClientScriptBlock
  (this.GetType(), "alert", sb.ToString());
    TxtBase1.Focus();
  }
  else
   {
   if (base_month < 1 | base_month > 12)
   {
    string message = "Month must be numeric";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<script type = 'text/javascript'>");
    sb.Append("window.onload=function(){");
    sb.Append("alert('");
    sb.Append(message);
    sb.Append("')};");
    sb.Append("</script>");
    ClientScript.RegisterClientScriptBlock
    (this.GetType(), "alert", sb.ToString());
     TxtBase1.Focus();
}
  else
     {
      if (base_month != current_month)
     {
      current_month = base_month;
      Calc_Rotation();
      Calc_Best_Before();
     }
      //txtBestBefore.SetFocus
   }
 }
}
  else
{
   string message = "Month must be numeric";
   System.Text.StringBuilder sb = new System.Text.StringBuilder();
   sb.Append("<script type = 'text/javascript'>");
   sb.Append("window.onload=function(){");
   sb.Append("alert('");
   sb.Append(message);
   sb.Append("')};");
   sb.Append("</script>");
   ClientScript.RegisterClientScriptBlock
   (this.GetType(), "alert", sb.ToString());
   TxtBase1.Focus();
  }

}

这是打印按钮的.aspx

 <asp:Button ID="Print" runat="server" 
OnClick="Print_Click1" Text="Print" />

这是打印按钮的代码,这很烦人,因为根据我的程序流程用户更改月份然后新的旋转代码和最佳日期之前填充然后用户输入数量打印然后单击打印按钮但是这里因为它在用户更改文本时自动触发打印事件,所以它会抛出一个错误,指出qty为空,这在print_click事件中被检查。

 protected void Print_Click1(object sender, EventArgs e)
  {
   if (String.IsNullOrEmpty(TxtItem.Text) 
    && String.IsNullOrEmpty(TxtQty.Text))
      {
     string message = "Please Enter an Item 
        and Qty Before Clicking the Print Button";
     System.Text.StringBuilder sb = new 
     System.Text.StringBuilder();
     sb.Append("<script type = 'text/javascript'>");
     sb.Append("window.onload=function(){");
     sb.Append("alert('");
     sb.Append(message);
     sb.Append("')};");
     sb.Append("</script>");
  ClientScript.RegisterClientScriptBlock
 (this.GetType(), "alert", sb.ToString());
   }
     else if (String.IsNullOrEmpty(TxtQty.Text))
   {
  string message = "Please Enter an Qty Before
            Clicking the Print Button";
           System.Text.StringBuilder sb = new 
     System.Text.StringBuilder();
     sb.Append("<script type = 'text/javascript'>");
     sb.Append("window.onload=function(){");
     sb.Append("alert('");
     sb.Append(message);
     sb.Append("')};");
     sb.Append("</script>");
  ClientScript.RegisterClientScriptBlock
 (this.GetType(), "alert", sb.ToString());
    }
   else if (String.IsNullOrEmpty(TxtItem.Text))
   {
    string message = "Please Enter an Item 
         Before Clicking the Print Button";
     System.Text.StringBuilder sb = new 
     System.Text.StringBuilder();
     sb.Append("<script type = 'text/javascript'>");
     sb.Append("window.onload=function(){");
     sb.Append("alert('");
     sb.Append(message);
     sb.Append("')};");
     sb.Append("</script>");
  ClientScript.RegisterClientScriptBlock
 (this.GetType(), "alert", sb.ToString());            }
   else
   {
    Printlabels();
   }
   }

所以在这个事件之后,打印按钮点击事件会自动触发,我检查了代码,我没有看到任何地方的点击按钮的调用,我不知道为什么它将另一个方法视为有效的一部分基本项目方法。

非常感谢任何帮助!!!!

1 个答案:

答案 0 :(得分:0)

尝试查看方法Print_Click1是否未定义为HTML中其他事件的句柄。