2013-06-20 85 views
-3
public partial class StudentView : System.Web.UI.Page 
{ 
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DBCS"].ConnectionString); 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     if (!string.IsNullOrEmpty(TextBox1.Text)) 
     { 
      string str = "Mysqlqeury"; 

      con.Open(); 
      SqlCommand cmd = new SqlCommand(str, con); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 

      if (Page.IsPostBack) 
      { 
       da.Fill(ds, str); 
       GDStudents.DataSource = ds; 
       GDStudents.DataBind(); 
      } 
      else 
      { 
       string myStringVariable1 = "No Student Record(s) Exist!! "; 
       ClientScript.RegisterStartupScript(this.GetType(), "myAlert", "alert('" + myStringVariable1 + "');", true); 
      } 
     } 
     else 
     { 
      string myStringVariable = "Enter Student Id Or Student Name!"; 
      ClientScript.RegisterStartupScript(this.GetType(), "myAlert", "alert('" + myStringVariable + "');", true); 
     } 
     con.Close(); 
    } 
} 
+0

文本框的值是字符串或整數 – user72

+4

你可以稍微花更多的精力來編寫你的問題嗎? – JohnFx

+0

你測試什麼場景?你必須在這裏更具體。第一個人是什麼意思? –

回答

1

Page.IsPostBack將始終爲true,在這種情況下它只會在首頁加載時爲false。因爲這個條件在一個按鈕點擊事件處理程序中,所以Page.IsPostBack永遠不會是false,所以else部分永遠不會被執行。

這裏是Page.IsPostBack屬性

http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx

你,如果你需要在第一頁加載執行代碼只應該4.使用Page.IsPostback一些文檔。

究竟是什麼原因試圖在這裏測試?

如果您只想測試數據庫中是否有任何記錄,那麼只需檢查您要返回的DataSet是否爲空。試試這樣的:

public partial class StudentView : System.Web.UI.Page 
{ 
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DBCS"].ConnectionString); 

    bool IsDataSetEmpty(DataSet dataSet) 
    { 
     foreach(DataTable table in dataSet.Tables) 
     { 
      if (table.Rows.Count != 0) 
      return false; 
     } 

     return true; 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     if (!string.IsNullOrEmpty(TextBox1.Text)) 
     { 
      string str = "Mysqlqeury"; 

      con.Open(); 
      SqlCommand cmd = new SqlCommand(str, con); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, str); 
      if (!IsDataSetEmpty(ds)) 
      { 
       GDStudents.DataSource = ds; 
       GDStudents.DataBind(); 
      } 
      else 
      { 
       string myStringVariable1 = "No Student Record(s) Exist!! "; 
       ClientScript.RegisterStartupScript(this.GetType(), "myAlert", "alert('" + myStringVariable1 + "');", true); 
      } 
    } 
    else 
    { 
     string myStringVariable = "Enter Student Id Or Student Name!"; 
     ClientScript.RegisterStartupScript(this.GetType(), "myAlert", "alert('" + myStringVariable + "');", true); 
    } 
    con.Close(); 
} 
} 
+0

我編輯了我的答案,以包含替代解決方案以實現您的期望結果。如果有幫助,請將其標記爲答案。謝謝,祝你好運。 @Sugu –

+0

是的,它的工作!,謝謝! – user72

0

它看起來像其他第一不會叫任何的理由如下:

  • TextBox1.Text沒有任何價值
  • Page.IsPostBack是真的
  • 發生某種異常

試着檢查這些,你可能會發現一個swer。

+0

爲什麼IsPostBack是真的?在聲明的其他部分沒有IsPostBack條件... 同樣,因爲它在一個按鈕上單擊,我會認爲它總是會回發 –