Reputation: 1472
I am using Asp.net Grid view bound field , in bound field i am using anchor tag to open value in new tab. My anchor tag is working perfectly as it is fetching value from database , but problem is that i want to show values from database to anchor tag some thing like my db value Here is my code
<asp:BoundField DataField="uniId" ControlStyle-CssClass="bg-darkGreen" HeaderText="ID" ReadOnly="True" SortExpression="uniId" HtmlEncode="false" DataFormatString="<a target='_blank' href='Details.aspx?uniId={0}'>uniId</a>" >
</asp:BoundField>
It is showing uniId in all rows instead of their values.
I have also tried <%=uniId%> but the problem remains same.
Upvotes: 1
Views: 1601
Reputation: 4591
On a security note: you should never make primary keys publicly viewable by placing them in an URL or embedded in a griview as a value that can be viewed by any browser's View Page Source option.
You need to at the very least...
Upvotes: 2
Reputation: 18127
Use TemplateField
not BoundField
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<HyperLink ID="RedirectBtn" runat="server"
OnClick="RedirectBtn_Click" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
You can add whatever you want in OnRowDataBound
event of the grid after that. If your RowDataBound event is called Grid_RowDataBound
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem == null)
return;
DataRowView row = e.Row.DataItem as DataRowView;
HyperLinkbtn = e.Row.FindControl("RedirectBtn") as HyperLink;
b.NavigateUrl = "some text" + row["ColumnName"] + "other text";
//if you want to open new tab
b.Target="_blank";
}
You are adding the event to the grid like this:
OnRowDataBound="Grid_RowDataBound"
Upvotes: 1
Reputation: 1068
Use TemplateField
instead of BoundField
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<a target='_blank' href='Details.aspx?uniId=<%#Eval("uniId")%>'><%#Eval("uniId")%></a>
</ItemTemplate>
</asp:TemplateField>
Upvotes: 2