Pergunta

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);
Foi útil?

Solução

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;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top