如何从动态生成的gridview中的单元格中检索文本

时间:2014-04-15 06:17:32

标签: asp.net gridview

我想获取gridview第一列中的书名。我使用了Template字段并在其中添加了一个链接按钮。从数据库中检索书名,从而动态地进行检索。我想在另一页中以pdf打开所选书籍。

public class Book
{
    public string Bookn{get;set;}
    public string Auth { get; set; }
    public string Category { get; set; }
    public string Edi { get; set; }
    public string Yopub { get; set; }
    public string Avai { get; set; }
    public string img { get; set; }
}

public class GetData
{
    public static List<Book> Getallrecord(string author)
    {
        List<Book> ob1 = new List<Book>();

         SqlConnection con;
            SqlCommand com;
            string cs = ConfigurationManager.ConnectionStrings["SDL_DBConnectionString"].ConnectionString;

            using (con = new SqlConnection(cs))
            {
                com = new SqlCommand("searchByAuthor3", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@SAuthor", author + "%");
                con.Open();

                SqlDataReader rd = com.ExecuteReader();
                while (rd.Read())
                {
                    Book bo = new Book();
                    bo.Bookn = rd["BookName"].ToString();
                    bo.Auth = rd["Author"].ToString();
                    bo.Category = rd["Category"].ToString();
                    bo.Yopub = rd["Yopublication"].ToString();
                    bo.Edi = rd["Edition"].ToString();
                    bo.Avai = rd["Available"].ToString();
                    bo.img = rd["Images"].ToString();

                    ob1.Add(bo);

                }

            }
            return ob1;
    }
}

protected void SAButton_Click(object sender, EventArgs e)
{
    string author = TAuthor.Text;

        if (Session["ClientLogin"] != null)
        {

           SAGridView.DataSource=GetData.Getallrecord(author);
           SAGridView.DataBind();     

        }
        else
        { 
            Server.Transfer("Login.aspx");
        }

}

1 个答案:

答案 0 :(得分:0)

您可以在gridview中添加超链接字段,然后重定向到其他页面,如

<asp:HyperLinkField DataNavigateUrlFields="Bookn"   
DataNavigateUrlFormatString="View_Book.aspx?BookNo={0}" Text="View Book" />