在GridView中,如果点击“选择”,在页面中会显示另一个GridView,显示这个相应的信息?这里我用GridView2做主表,GridView2做从表。
先从GridView2的事件属性SelectedIndexChanged中写上相应的事件名:GridView2_SelectedIndexChanged;在数据属性DataKeyNames写上数据键的名字(GridView.DataKeyNames属性获取或设置一个数组,该数组包含了显示在GridView控件中的项的主键的名称,FileTransmitId)。然后编辑相应的代码:
protected
void
GridView2_SelectedIndexChanged(
object
sender, EventArgs e)
{
this.ObjectDataSource3.SelectParameters[0].DefaultValue = GridView2.SelectedValue.ToString();
ObjectDataSource3.DataBind();
GridView3.DataBind();
GridView3.Visible = true;
}
{
this.ObjectDataSource3.SelectParameters[0].DefaultValue = GridView2.SelectedValue.ToString();
ObjectDataSource3.DataBind();
GridView3.DataBind();
GridView3.Visible = true;
}
然后在编写从数据库中获取的信息:
public
static
FileTransmit GetFileTransmits(
int
fileId)
{
FileTransmit fileTransmit = null;
using (SqlConnection connection = new SqlConnection(constr))
{
connection.Open();
string sql = "select * from FileTransmit where FileTransmitId=@fileTransmitId";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.Add(new SqlParameter("@fileTransmitId", fileId));
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
fileTransmit = new FileTransmit();
fileTransmit.FileName=reader["FileName"].ToString();
fileTransmit.Content=(byte[])reader["Attachment"];
}
reader.Close();
}
return fileTransmit;
}
{
FileTransmit fileTransmit = null;
using (SqlConnection connection = new SqlConnection(constr))
{
connection.Open();
string sql = "select * from FileTransmit where FileTransmitId=@fileTransmitId";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.Add(new SqlParameter("@fileTransmitId", fileId));
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
fileTransmit = new FileTransmit();
fileTransmit.FileName=reader["FileName"].ToString();
fileTransmit.Content=(byte[])reader["Attachment"];
}
reader.Close();
}
return fileTransmit;
}