Ho to create a hyperlink dynamically in Gridview

Hello guys,

I have a situation that my grid view can contain the child items. And if it contains the child item i want to some how create a link button or a hyper link and open a seperate window to see the child items?
I think i need to replace the label with the hyperlink field in row data bound of the grid view but stuck on actually how to do that?

many thanks
Ali ShahSQL DeveloperAsked:
Who is Participating?
Ali ShahConnect With a Mentor SQL DeveloperAuthor Commented:
Actually i have done this by template field and row data databound. I am sure there must be a better way of doing this?

                                <asp:Label ID="lblMeasure" runat="server" Text='<%# Bind("MeasDef") %>'></asp:Label>
                                <asp:HyperLink ID="hprMeasure" runat="server" Visible ="false" Target="_blank"></asp:HyperLink>

Then in row data bound

 if (e.Row.RowType == DataControlRowType.DataRow)
                  Label lblMeasure = (Label)e.Row.FindControl("lblMeasure");
                HyperLink hprMeasure = (HyperLink)e.Row.FindControl("hprMeasure");

If (isChild) //ischild is method and returns true or false
    lblMeasure.Visible = false;
                   hprMeasure.Visible = true ;
                   hprMeasure.Text = lblMeasure.Text;
                   hprMeasure.NavigateUrl ="~/KPIs/test.aspx?OEValID=" +txtOEValID.Text .ToString();
                   lblMeasure.Visible = true;
                   hprMeasure.Visible = false;
maybe it helps you:

There are two ways. You can create HyperLinkField or add a Hyperlink in <asp:templatefield >
                <asp:HyperLinkField DataTextField="LeadID" DataNavigateUrlFields="LeadID" DataNavigateUrlFormatString="LeadInformation.aspx?LeadID={0}" Text="Lead ID" />
                <asp:BoundField DataField="DateTime" HeaderText="Date Updated" />
                        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Bind("LeadID") + Request.QueryString("type") %>'

Open in new window

Method 2

You can add a TemplateField in the GridView control using the Edit Column
option from the context menu when you click on the smart tag.
1. Add a TemplateField in the Selected Fields list.
2. Now Right click on the Grid and select Edit Templates. Now drop the
Hyperlink control in the ItemTemplate of the Grid.
3. Now bind the NavigateURL and Text property of the Hyperlink to the
hyperlink column field in the table using Data Bindings.
4. Now if you click on the hyperlink, it will take you to that page.

also VIDEO
Ali ShahSQL DeveloperAuthor Commented:
Thanks for your answer. But actually my hyperlink depends on a condition that is let's say if recored type = A then create hyperlink else display a label or text box
Ali ShahSQL DeveloperAuthor Commented:
Well it is working fine for me and i have waited for two three days that if some one woule give any better solution
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.