我现在做了一个消息收发功能,gridview显示未读邮件,点击选择,detailview的visible为true,并显示详情数据,同时更新选择的数据中的字段status为1,但是目前遇到的问题是点击选择按钮后,detailview的数据为空。
应该是选择事件相应后刷新页面了,然后数据绑定的数据就变了,所以变成空的了。代价如下:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
detail.Visible = true;
SqlDataSource2.SelectCommand = strCurSqlString;
//重新绑定数据源
SqlDataSource1.SelectCommand = strCurSqlString;
GridView1.DataBind();
detail.Visible = true; if (GridView1.SelectedIndex < 0) return;
if (nType == 2)
{
int ID = Convert.ToInt32(GridView1.SelectedRow.Cells[0].Text);
string strConn = ConfigurationManager.ConnectionStrings["systemConnectionString"].ToString();
string strSqlIsRead = ""; strSqlIsRead = "Update Message set Status=@Status ";
strSqlIsRead += " Where ID=" + ID; SqlConnection cn = new SqlConnection(strConn);
cn.Open(); SqlCommand cmd = new SqlCommand(strSqlIsRead, cn); SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@Status";
param1.SqlDbType = SqlDbType.SmallInt;
param1.Value = 1;
cmd.Parameters.Add(param1); cmd.ExecuteNonQuery();
cn.Close();
}
应该是选择事件相应后刷新页面了,然后数据绑定的数据就变了,所以变成空的了。代价如下:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
detail.Visible = true;
SqlDataSource2.SelectCommand = strCurSqlString;
//重新绑定数据源
SqlDataSource1.SelectCommand = strCurSqlString;
GridView1.DataBind();
detail.Visible = true; if (GridView1.SelectedIndex < 0) return;
if (nType == 2)
{
int ID = Convert.ToInt32(GridView1.SelectedRow.Cells[0].Text);
string strConn = ConfigurationManager.ConnectionStrings["systemConnectionString"].ToString();
string strSqlIsRead = ""; strSqlIsRead = "Update Message set Status=@Status ";
strSqlIsRead += " Where ID=" + ID; SqlConnection cn = new SqlConnection(strConn);
cn.Open(); SqlCommand cmd = new SqlCommand(strSqlIsRead, cn); SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@Status";
param1.SqlDbType = SqlDbType.SmallInt;
param1.Value = 1;
cmd.Parameters.Add(param1); cmd.ExecuteNonQuery();
cn.Close();
}
在初始化DetailsView的外面判断是否IsPostBack