How can I programmatically Update Details View. NOT through SQL DataSource or through Wizard. I want to Update the Details View from Code- Behind (.cs)
I have a Drop down list loaded with UserNames, Up on Selecting the UserNAME, I am loading the Details View with User Inform开发者_如何学JAVAation.
Now I want to Provide Update feature programmtically.
How can I do this ?..
Try putting an Update Panel around the Details View. Add an async postback trigger to the panel - the change event of the drop down.
Set the dropdown to autopostback.
You'll get the desired behavior without a full postback.
You might be able to do better, but this will work.
If you already displaying data on any form after drop down list selection, propably you're using "SelectedIndexChanged" event.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// show user details for example by get the data from db
string query = "SELECT userId, UserName FROM Users";
SqlConnection conn = new SqlConnection("conn string");
SqlCommand comd = new SqlCommand(query, conn);
conn.Open();
using(SqlDataReader r = comd.ExecuteReader())
{
while(r.Read())
{
sb.AppendLine(r.GetInt32(0) + ", " + r.GetString(1));
}
conn.Close();
}
textBox1.Text = sb.ToString();
}
perform to refresh data by seting desired index:
this.comboBox1.SelectedIndex = 0;
it invokes SelectedIndexChanged envent and refresh user detailed information.
Of course you can do it more efficient.
[EDIT]
You can update data in database by displaying the detailed view in DataGridView control connected to db:
SqlDataAdapter adapter = new SqlDataAdapter("select * from users", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
edit data in DataGridView and after all:
adapter.Update(ds);
done!
精彩评论