【问题标题】:how to avoid button click event when enter the textbox on textchanged event in asp .Net在asp .Net中的textchanged事件上输入文本框时如何避免按钮单击事件
【发布时间】:2016-04-07 22:34:13
【问题描述】:

我有一个以一个月为值的文本框,当它被更改时,会触发 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" />

这是打印按钮的代码,这很烦人,因为根据我的程序流程,用户更改月份,然后填充新的轮换代码和最佳日期,然后用户输入要打印的数量,然后单击打印按钮但是在这里,因为当用户更改文本时它会自动触发打印事件,所以它会抛出一个错误,指出数量为空,这在 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();
   }
   }

因此,在此事件之后,打印按钮单击事件会自动触发,我检查了代码,我在任何地方都没有看到对单击按钮的调用,我不确定为什么它将另一种方法视为有效基础项的一部分方法。

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

【问题讨论】:

    标签: c# asp.net methods


    【解决方案1】:

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

    【讨论】:

    • 我该怎么做,据我所知,它没有被定义为任何其他事件的句柄
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    相关资源
    最近更新 更多