有人能告诉我我做错了什么吗?我试图在页面加载时自动调用该方法,但它不起作用。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