1
votes

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.

3
possible duplicate of How to select a cell in datagrid onClick in ASP.net C# also read up on Templates - MethodMan
try <%#Eval("uniId")%> - Rohit

3 Answers

2
votes

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>
2
votes

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...

  1. Handle the SelectedIndexChanged event of the Gridview
  2. Obtain the uniID from the gridview DataKeys based on the row selection
  3. Set a Session Variable or similar with the Key value that will be used by Details.aspx
1
votes

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"