有人能告訴我我做錯了什麼嗎?我試圖在頁面加載時自動調用該方法,但它不起作用。C#頁面字段未按預期方式填充
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
customerInformation();
}
}
protected void ddNames_SelectedIndexChanged(object sender, EventArgs e)
{
customerInformation();
}
private void customerInformation()
{
string dbString = ConfigurationManager.ConnectionStrings["TechSupportDBConString"].ConnectionString;
string query = "SELECT * FROM Customers WHERE Name='" + ddNames.Text + "'";
SqlConnection Connection = new SqlConnection(dbString);
Connection.Open();
SqlCommand Com = new SqlCommand(query, Connection);
SqlDataReader reader = Com.ExecuteReader();
if (reader.Read())
{
lblName.Text = reader["Name"].ToString() + "'s Personal Information";
lblAddress.Text = reader["Address"].ToString() + "\n" + reader["City"].ToString() + " " + reader["State"].ToString() + " " + reader["ZipCode"].ToString();
lblPhone.Text = reader["Phone"].ToString();
lblEmail.Text = reader["Email"].ToString();
reader.Close();
Connection.Close();
}
}
你試過調試嗎? – Alex
究竟是什麼問題?'IsPostBack'意味着你的'customerInformation'只會在第一次頁面加載時被調用。在隨後的回發中,這個方法不會被再次調用,這是預期的行爲。但是,如果你想加載一些客戶信息按鈕點擊你需要明確地調用'customerInformation' –
@Neel我希望所有的標籤都可以在頁面首次加載時使用來自數據庫的正確數據進行更新。由於在下拉列表中已經有一個選定的值。 – HereToLearn