views:

25

answers:

1

Hi everyone,

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)"

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);
+1  A: 

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
WOW. It's that simple. I got confused with the methods. Thanks.
janejanejane
Another question about this, is it possible to store the "EMP_ID" in Session? And if yes, how is it done? Thanks
janejanejane
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
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