3

Can someone please explain to me how I can include a boundfield (Emp_ID) to the HyperLinkField (IDNumber) in a code generated GridView using C#? So that the url string would be '../Pages/Home.aspx?Emp_ID=(Emp_ID)'>(IDNumber)</a>

Thanks

Snippet code below:

IDColumn.DataField = "Emp_ID";
IDColumn.HeaderText = "Emp_ID";
string[] id = new string[] { "Emp_ID" };
IDColumn.Visible = false;
grid.Columns.Add(IDColumn);

hyperlinkedColumn.DataTextField = "IDNumber";
hyperlinkedColumn.HeaderText = "ID No.";
hyperlinkedColumn.DataNavigateUrlFields = id;
hyperlinkedColumn.DataTextFormatString = "<a href='../Pages/Home.aspx?Emp_ID='>{0}</a>";
hyperlinkedColumn.Visible = true;
grid.DataKeyNames = id;
grid.Columns.Add(hyperlinkedColumn);
Taz
  • 3,718
  • 2
  • 37
  • 59
janejanejane
  • 496
  • 2
  • 12
  • 30

1 Answers1

2

What you are going to want to do is set the DataNavigateUrlFormatString instead of DataTextFormatString.

string[] id = new string[] { "Emp_ID" }; 

HyperLinkField hyperlinkedColumn = new HyperLinkField();
hyperlinkedColumn.DataTextField = "IDNumber";
hyperlinkedColumn.HeaderText = "ID No.";
hyperlinkedColumn.DataNavigateUrlFields = id;
hyperlinkedColumn.DataNavigateUrlFormatString = "../Pages/Home.aspx?Emp_ID={0}";
hyperlinkedColumn.Visible = true;
kevev22
  • 3,737
  • 21
  • 32
  • WOW. It's that simple. I got confused with the methods. Thanks. – janejanejane Oct 16 '10 at 01:32
  • Another question about this, is it possible to store the "EMP_ID" in Session? And if yes, how is it done? Thanks – janejanejane Oct 19 '10 at 08:26
  • I'm not exactly sure what you mean. You can store anything you want in the session state by using Session["Emp_ID"] = "whatever". Do you mean ViewState? If you want to store the Emp_ID in the viewstate so you can access it for each row, try adding datakeys to the gridview - grid.DataKeyNames = new string[] { "Emp_ID" } – kevev22 Oct 19 '10 at 13:41
  • Ooops, sorry for the confusion. Suppose I want to store EMP_ID in a Session variable, instead of putting it in the querystring, how can this be achieved? – janejanejane Oct 23 '10 at 03:46