Ng Zen
Ng Zen

Reputation: 319

How can I navigate to a different URL when user clicks the hyperlink in the GridView with a condition?

I am a beginner in learning asp.net. I have a column in a GridView with header name FORM ID. I want to be able to navigate to the different URL based on the part of the FORM ID.

For example,

I understand the use of the MID function like so v=MID(string,4,1) to capture the 4th value and redirect to page by determining the value v but I do not know how to apply this correctly. Please guide me. Your help is greatly appreciated.

The following is the aspx code I'm currently work on :

<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false">           
    <columns>
        <asp:Hyperlinkfield DataTextField="formid" HeaderText="Form ID" ItemStyle-  Width="150px" 
            DataNavigateUrlFields="formid" DataNavigateUrlFormatString="~/abc1.aspx" />         
    </Columns>
</asp:GridView>

Upvotes: 2

Views: 1076

Answers (2)

Ng Zen
Ng Zen

Reputation: 319

This is my final code which is working successfully :

<asp:GridView ID="child" runat="server" AutoGenerateColumns="false" >
  <Columns>
    <asp:TemplateField HeaderText="Form ID" >
      <ItemTemplate>
        <asp:Hyperlink runat="server"  Text='<%# Eval("formid") %>' 
         NavigateUrl='<%# Eval("formid","~/abc" + Mid(Eval("formid"), 4, 1) + ".aspx?formid={0}") %>' />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

Credits to @mason and @Tony L. for helping me out on this issue.

Upvotes: 0

Tony L.
Tony L.

Reputation: 19396

You could switch your Hyperlinkfield to a TemplateField with a HyperLink control to give you more control over the NavigateUrl like so:

<asp:TemplateField HeaderText="Form ID">
    <ItemTemplate>
        <asp:HyperLink runat="server" Text='<%# Eval("formid") %>'
            NavigateUrl='<%# "~/abc" + Mid(Eval("formid"), 4, 1) + ".aspx" %>'>
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

Upvotes: 2

Related Questions