0

I am trying to add a clickable link field dynamically inside my gridview. My code is

 while (rdr.Read())
        {
            DataRow dr = dtTutorial.NewRow();
            dr["Topic"] = rdr["Topic"];
            dr["Description"] = rdr["Description"];
            HyperLinkField h = new HyperLinkField();
            h.HeaderText = "Visit";
            h.NavigateUrl = "Details.aspx";
            h.DataTextField = rdr["link"].ToString();

            dr["Link"] = h;
            dtTutorial.Rows.Add(dr);
        }

But when I am executing this page, I am not getting the filed clickable. How can i resolve this?

1
  • Put your code for adding hyperlink in Row_DataBound event of grid view. Commented Dec 17, 2017 at 4:08

1 Answer 1

1

I have figured out the solution. The code is given here

 while (rdr.Read())
        {
            DataRow dr = dtTutorial.NewRow();
            dr["Topic"] = rdr["Topic"];
            dr["Description"] = rdr["Description"];
            dr["Visit"] = rdr["id"];
            dtTutorial.Rows.Add(dr);
        }
        con.Close();
    }
    GridView1.DataSource = dtTutorial;
    GridView1.DataBind();
    foreach (GridViewRow gr in GridView1.Rows)
    {
        HyperLink hp = new HyperLink();
        hp.Text = "Click here";
        hp.NavigateUrl = "~/Details.aspx?id=" + gr.Cells[2].Text;
        gr.Cells[2].Controls.Add(hp);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.