Sathya
Sathya

Reputation: 59

Hyperlink in Gridview which is autogenerated in C#

I have a Gridview which is autogenerated by fetching data from various tables. Now my requirement is i need to make my first column as hyperlink so that if it is clicked then it has to navigate to another page with hyperlinked value. In the second page i have Lable this label should show the hyperlink value and based on that the data will be populated on the second page.

For example: I have a gridview with 10 columns..My first column is Emp Id. If that id is clicked it has to take me to second page and the label control must get this id value and based on that id the rest of the info like emp name emp DOB should be filled. i am using C# as my code behind.

Can anyone help me to proceed.. Awaiting ur reply

Upvotes: 0

Views: 1359

Answers (2)

codingbiz
codingbiz

Reputation: 26386

You need the <asp:HyperLinkField />

<asp:GridView>
<Columns>
   <asp:HyperLinkField HeaderText="Id" DataTextField="YourID" DataNavigateUrlFields="YourID" 
           DataNavigateUrlFormatString="SecondPage.aspx?Id={0}" />
</Columns>
</asp:GridView>

You might need to remove the AutoGenerateColumns="true" property and type the fields yourself, selecting only the columns you want to show - using <asp:BoundFied DataField="ColumnName" />

In case you want to pass multiple values in the querystring, separate the fields with comma

 DataNavigateUrlFields="YourID, SecondField"

and your format string would be

DataNavigateUrlFormatString="SecondPage.aspx?Id={0}&param2={1}"

Other links

Upvotes: 3

Henk Mollema
Henk Mollema

Reputation: 46581

You can use a TemplateField column in your GridView:

<asp:TemplateField>
    <ItemTemplate>
        <asp:HyperLink ID="linkToDetails" runat="server" NavigateUrl='Details.aspx?empId=<%# Eval("empId") %>' Text='<%# Eval("empId") %>'></asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

On the Details.aspx page you should get the empId from the QueryString and get the details from the database.

Upvotes: 1

Related Questions