如何在按钮点击时显示消息 [英] how to show a message on button click

查看:93
本文介绍了如何在按钮点击时显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我的代码如下。我想在检查所有验证控件后显示已成功添加的消息。但它在检查所有验证控件之前显示消息。

Hello Everyone. My code is given below. I want to show the Successfully added message after checking all the validation control. But it showing the message before checking all validation control.

protected void Button2_Click(object sender, EventArgs e)
  {
      Label Total = (Label)GridView1.FooterRow.FindControl("Label9");
      int total = 0;
      for (int i = 0; i < GridView1.Rows.Count; i++)
      {
          CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
          if (chk.Checked == true)
          {
              TextBox TextBox1 = (TextBox)GridView1.Rows[i].FindControl("TextBox3");
              if (TextBox1.Text != "")
              {
                  total += Convert.ToInt32(TextBox1.Text);
              }
          }

          Total.Text = total.ToString();
      }
      for (int i = 0; i < GridView1.Rows.Count; i++)
      {
          Label lab = (Label)GridView1.Rows[i].Cells[0].FindControl("Label1");
          Label ColTotal = (Label)GridView1.FooterRow.FindControl("Label9");
          TextBox quantity = (TextBox)GridView1.Rows[i].Cells[1].FindControl("TextBox2");
          TextBox TAmount = (TextBox)GridView1.Rows[i].Cells[2].FindControl("TextBox3");
          string amount = quantity.Text;
          DropDownList ddl = (DropDownList)GridView1.Rows[i].Cells[3].FindControl("DropDownList1");
          TextBox ph = (TextBox)GridView1.Rows[i].Cells[1].FindControl("TextBox6");
          CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("Checkbox1");
          CustomValidator com = (CustomValidator)GridView1.Rows[i].Cells[1].FindControl("CustomValidator1");
          if (chk.Checked == true)
          {
              using (SqlCommand comm = new SqlCommand("select [Serial_Number_Required] from Products where Product=@Product ", con))
              {
                  con.Open();
                  //string Role = (string)(Session["Role"]);
                  // 2. define parameters used in command object
                  SqlParameter para = null;
                  para = new SqlParameter();
                  para.ParameterName = "@Product";
                  para.Value = lab.Text;
                  comm.Parameters.Add(para);

                  //Pass @Pages parameter

                  SqlDataReader reade = comm.ExecuteReader();
                  while (reade.Read())
                  {
                      Session["Serial_Number_Required"] = Convert.ToString(reade["Serial_Number_Required"]);
                  }
                  con.Close();
                  string serials = ph.Text;
                  string[] sl = serials.Split(',');

                  if (Session["Serial_Number_Required"].ToString() == "Y")
                  {
                      if (quantity.Text.Trim() != "" && TAmount.Text != "")
                      {

                          for (int j = 0; j < sl.Length; j++)
                          {
                              for (int k = i; k < sl.Length - 1; k++)
                              {
                                  //if (sl[i].ToString() == sl[k + 1].ToString())
                                  //{
                                  //    Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Enter Unique serial Number for '" + lab.Text + "'');", true);
                                  //}
                                  string str = "select count(*)from Inventory where [Serial Number]='" + sl[j].ToString() + "'";
                                  con.Open();
                                  SqlCommand commm = new SqlCommand(str, con);

                                  int count = Convert.ToInt32(commm.ExecuteScalar());

                                  con.Close();

                                  if (count == 0 && sl[j].ToString() != sl[k + 1].ToString())
                                  {
                                      con.Open();
                                      string query1 = "insert into Inventory ([Invoice Number],Product,[Serial Number],Amount,Status) values('" + TextBox5.Text + "', '" + lab.Text + "','" + sl[j].ToString() + "','" + Convert.ToInt32(TAmount.Text) / Convert.ToInt32(quantity.Text) + "','" + ddl.SelectedValue + "')";
                                      SqlCommand cmd = new SqlCommand(query1, con);
                                      cmd.ExecuteNonQuery();
                                      con.Close();
                                      con.Open();
                                      string query = "insert into FSPL_Inventory ([Invoice Number],[Date],[Total Amount],Product,[Serial Number],Amount,Status) values('" + TextBox5.Text + "','" + Label7.Text + "','" + ColTotal.Text + "','" + lab.Text + "','" + sl[j].ToString() + "','" + Convert.ToInt32(TAmount.Text) / Convert.ToInt32(quantity.Text) + "','" + ddl.SelectedValue + "')";
                                      SqlCommand comd = new SqlCommand(query, con);
                                      comd.ExecuteNonQuery();
                                      con.Close();
                                  }
                              }
                          }
                        //  Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Items are added successfully into FSPL Inventory');", true);

                      }
                  }
                  if (Session["Serial_Number_Required"].ToString() == "N")
                  {
                      if (quantity.Text.Trim() != "" && TAmount.Text != "")
                      {
                          for (int j = 0; j < Convert.ToInt16(quantity.Text); j++)
                          {
                              string S = "";
                              con.Open();
                              string query1 = "insert into Inventory ([Invoice Number],Product,Amount,Status,[Serial Number]) values('" + TextBox5.Text + "', '" + lab.Text + "','" + Convert.ToInt32(TAmount.Text) / Convert.ToInt32(quantity.Text) + "','" + ddl.SelectedValue + "','" + S.ToString() + "')";
                              SqlCommand cmd = new SqlCommand(query1, con);
                              cmd.ExecuteNonQuery();


                              con.Close();

                              con.Open();
                              string query = "insert into FSPL_Inventory ([Invoice Number],[Date],[Total Amount],Product,Amount,Status,[Serial Number] ) values('" + TextBox5.Text + "','" + Label7.Text + "','" + ColTotal.Text + "','" + lab.Text + "','" + Convert.ToInt32(TAmount.Text) / Convert.ToInt32(quantity.Text) + "','" + ddl.SelectedValue + "','" + S.ToString() + "')";
                              SqlCommand comd = new SqlCommand(query, con);
                              comd.ExecuteNonQuery();
                              con.Close();
                              Response.Write("<script>alert('Successfully added');</script>");
                          }
                         // Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Items are added successfully into FSPL Inventory');", true);
                      }
                  }
              }
          }
          bind();
      }
  }

推荐答案

使用Response.Write添加脚本标记(包含警告它在页面中的任何其他内容之前,因此它将在其他任何内容之前运行(包括在客户端上的实际呈现)...

您必须在最后渲染脚本 - 尝试 ScriptManager [ ^ ] ...
With Response.Write you add a script tag (with your alert in it) just before anything else in your page, so it will be running before anything else (include actual rendering on the client)...
You have to render the script at the end - try ScriptManager[^]...


试试这个..你肯定会解决你的问题..



Try This .. Defiantly you will Solve you Problem..

ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "whatever", "alert('Items are added successfully into FSPL Inventory')", true);









谢谢



AARIF SHAIKH





Thanks

AARIF SHAIKH


By使用脚本管理器,您可以显示警告,确认等消息
By using Script Manager you can show message like alert, confirm, etc


这篇关于如何在按钮点击时显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆