Programer
Programer

Reputation: 1035

asp:GridView HYPERLINKFIELD - asp code inside datanavigateurlformatstring

I have a page to search products by their name. In many pages I work with product codes. If the user does not know the product code, I let him go to this page, search by name, and then select one of the results and get back to the page he came from.

In the results of the search by name I set an HyperLinkField that will redirect to a certain page , with a paramter of the product code.

My code is like this:

 <asp:GridView ID="GridView1" Runat="server" 
  DataSource='<%# GetData(pName.Text) %>' AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Name">
            <ItemStyle HorizontalAlign="Center" 
              VerticalAlign="Middle"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField  DataField="Code"></asp:BoundField>
        <asp:ImageField ControlStyle-Width="150px"  ControlStyle-Height="150px" DataImageUrlField="PictureURL" ></asp:ImageField>
        <ASP:HYPERLINKFIELD text=">>" datanavigateurlfields="Code"  datanavigateurlformatstring="priceUpdater.aspx?ProductCode={0}"></ASP:HYPERLINKFIELD> 
    </Columns>
</asp:GridView>

Where GetData is a function that returns an object of type Product with the fields, name,Code,image etc.

As you can see, this link in the HYPERLINKFIELD will redirect to a page called priceUpdater with the parameter of the product code.

I want this page to be dynamic. I have tried to add a paramter to the search page like this

 <%string pageRequested = Page.Request.QueryString["SearchScreen"];%>

and now im trying to use the HYPERLINK like this:

<ASP:HYPERLINKFIELD text=">>" datanavigateurlfields="Code"  datanavigateurlformatstring="<%=pageRequested%>.aspx?ProductCode={0}"></ASP:HYPERLINKFIELD> 

But the page that the link reffers to is just as plain text as wrriten (http://mysite.com/%3C%=pageRequested%>.aspx?ProductCode=2450)

How can I make this work?

Thank you!

Upvotes: 4

Views: 5349

Answers (3)

Alexander Manekovskiy
Alexander Manekovskiy

Reputation: 3203

If you want to use HyperLinkField you need to extend datasource object returned by GetData method with value that comes with pageRequested query string parameter.

In that case markup for HyperLinkField will be following:

<asp:HyperLinkField text=">>" 
    datanavigateurlfields="PageRequested,Code"
    datanavigateurlformatstring="{0}.aspx?ProductCode={1}"></asp:HyperLinkField>

But this will work only if you add PageRequested as a public field or property to an object that is returned by GetData method.

If this is not an option than you need to implement your own "LinkField" control inherited from DataControlField or use ItemTemplate as it was suggested by Nitin.

Upvotes: 3

Nitin S
Nitin S

Reputation: 7601

Replace HYPERLINKFIELD with TemplateField containing HyperLink and bind it in grid's rowdatabound event

ASPX:

<asp:GridView ID="GridView1" runat="server" DataSource='<%# GetData(pName.Text) %>'
OnRowDataBound="Grd_RowDatabound"
    AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Name">
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField DataField="Code"></asp:BoundField>
        <asp:ImageField ControlStyle-Width="150px" ControlStyle-Height="150px" DataImageUrlField="PictureURL">
        </asp:ImageField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="lnkNavigate" runat="server" NavigateUrl="" Text=">>"></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

CODEBEHIND

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            GridView1.DataBind();
    }

    public List<myData> GetData(string param)
    {
        List<myData> lst = new List<myData>();
        lst.Add(new myData() { Name = "Hello", Code = "World", PictureURL = "Images/Select.png" });
        return lst;
    }

    public string pageRequested
    {
        get {
            return Page.Request.QueryString["SearchScreen"];
        }
    }

    protected void Grd_RowDatabound(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink lnkNavigate = (HyperLink)e.Row.FindControl("lnkNavigate");
            if (lnkNavigate != null)
            {
                myData obj = (myData)e.Row.DataItem;
                lnkNavigate.NavigateUrl = pageRequested + ".aspx?ProductCode="+obj.Code;
            }
        }
    }
}

public class myData
{
    public string Name { get; set; }
    public string Code { get; set; }
    public string PictureURL { get; set; }
}

Upvotes: 1

el vis
el vis

Reputation: 1302

Try with that:

<asp:TemplateField>                     
<ItemTemplate>
    <ASP:HYPERLINK text=">>" NavigateUrl='<%# String.Format("~/{0}.aspx?ProductCode={1}",Page.Request.QueryString["SearchScreen"],Eval("Code")) %>'></ASP:HYPERLINK> 
</ItemTemplate>
</asp:TemplateField>

Upvotes: 1

Related Questions