怎么实现点击GridView的下载按钮获取当前行的数据
如何实现点击GridView的下载按钮获取当前行的数据。

想要实现的功能:
点击当前行的“下载论文”按钮,获取当前行sid列(也就是第一列)的的数据,然后根据获取的sid查询thesis表的thesisname字段,然后下载。现在遇到的问题就是 GridViewCommandEventArgs e没有e.RowIndex。我用的SqlDataSource绑定GridView的方法,感觉不太好,如果要用Gridview绑定Datatable,那怎么写好?最好写点代码参考下吧。求各位大神指导下新手,不胜感激。
SqlDataSource的数据是SELECT [sid], [student_department], [student_major], [student_name], [student_grade] FROM [student] WHERE ([tid] = @tid) @tid是当前教师号, 教师号我用session[membername]保存下来了.
下面是我的代码:
想要实现的功能:
点击当前行的“下载论文”按钮,获取当前行sid列(也就是第一列)的的数据,然后根据获取的sid查询thesis表的thesisname字段,然后下载。现在遇到的问题就是 GridViewCommandEventArgs e没有e.RowIndex。我用的SqlDataSource绑定GridView的方法,感觉不太好,如果要用Gridview绑定Datatable,那怎么写好?最好写点代码参考下吧。求各位大神指导下新手,不胜感激。
SqlDataSource的数据是SELECT [sid], [student_department], [student_major], [student_name], [student_grade] FROM [student] WHERE ([tid] = @tid) @tid是当前教师号, 教师号我用session[membername]保存下来了.
下面是我的代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string nid = GridView1.Rows[0].Cells[0].Text; //获取当前行的学生学号
string name;
SqlConnection Con = new SqlConnection("data source=localhost;Initial Catalog=paper;Persist Security Info=True;User ID=sa;Password=123456789");
String SqlCmd = "SELECT Thesisname FROM Thesis WHERE ([sid] = @nid)";
SqlCommand Cmdifo = new SqlCommand(SqlCmd, Con);//定义SqlCommand对象
Cmdifo.Parameters.Add("@nid", SqlDbType.Int, 10).Value = nid;
try
{
Con.Open();//打开数据库
SqlDataReader reader = Cmdifo.ExecuteReader();
while (reader.Read())
{
Session["name"] = reader["Thesisname"].ToString();
}
reader.Close();
}
catch (SqlException ex)
{
name = null;
throw new ApplicationException(ex.Message);
}
finally
{
Con.Close();//关闭数据库连接
}
string source = Server.MapPath("../") + "upfile\\";
DirectoryInfo sourceDirectory = new DirectoryInfo(source);
name = Session["name"].ToString();
FileInfo[] files = sourceDirectory.GetFiles(name);
Session.Remove("name");
foreach (FileInfo file in files)
{
//以字符流的形式下载文件
FileStream fs = new FileStream(source + file.Name, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}