在Asp.net应用程序上抛出警报消息不起作用

时间:2016-02-10 23:01:53

标签: javascript c# asp.net

我正在尝试查询数据库并检索一行数据然后如果数据存在程序继续如果不是我想要一条警告消息弹出下面的代码应该工作,我不知道重建后发生了什么应用程序运行警报消息代码就像它是正常程序一样,并且不显示消息并且没有发生错误,我希望弹出警报,以及当用户在项目文本框上的警报焦点上单击“确定”时。请帮助下面是我正在使用的代码:

 try
 {

 var connectionstring = ConfigurationManager.ConnectionStrings["TestDataTable"].ConnectionString;
 OdbcConnection con = new OdbcConnection(connectionstring);
 //con.ConnectionString = connectionstring;
 if (TxtItem.Text != hold_item)
 {
 con.Open();
 OdbcCommand cmd = new OdbcCommand("Select t_item,t_idsc,t_upct,t_item_upc,t_ctyp,t_citg,t_best,t_disp,t_mold,t_csel " + "from informix.tsckcm907 " + "where t_item = " + stitem, con);

 OdbcDataReader myReader = cmd.ExecuteReader();
 DataTable testdt = new DataTable();

 testdt.Load(myReader);
 if (testdt.Rows.Count > 0)
 {
 {
 foreach (DataRow row in testdt.Rows)
 {
 lbldesc.Text = row["t_idsc"].ToString();
 lbldesc.Visible = true;


 Spanish_Item();
 if (hold_lang_value == 0)
 {
 TxtBestBeforeMonths.Text = row["t_best"].ToString();
 holdbest = Convert.ToInt16(TxtBestBeforeMonths.Text);
 }
 else
 {
 DropDownList2.SelectedIndex = 1;
 object stlanguage = 1;
 hold_language = Convert.ToString(stlanguage);
 TxtBestBeforeMonths.Text = row["t_best"].ToString();
 holdbest = Convert.ToInt16(TxtBestBeforeMonths.Text);
 }
 }
 }




 myReader.Close();
 myReader.Dispose();
 cmd.Dispose();
 con.Close();
 con.Dispose();
 }
 else
 {
 string message = "Item Not Found";
 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()); ;
 TxtItem.Focus();

 }
 }  

1 个答案:

答案 0 :(得分:0)

所以这就解决了我上面的问题,

我不得不使用bool来阻止程序继续并抛出错误

 else
    {
     string strErrorDesc = "Item Not Found";
     Response.Write(@"<script language='javascript'>
alert('The following errors have occurred: \n" + strErrorDesc + " .');</script>");
    value = false;
    }
    if (value == false)
    {

    }
    else
    {
    Calc_Rotation();
    Calc_Best_Before();
    }