Asp: GridView HYPERLINKFIELD - asp code inside datanavigateurlformatstring

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

In the search results by name, I install HyperLinkField, which redirects to a specific page with the product code parameter.

My code looks 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 fields, name, code, image, etc.

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

I want this page to be dynamic. I tried adding a parameter to the search page, for example,

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

and now I'm trying to use HYPERLINK as follows:

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

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

How can I do this job?

Thanks!

+4
source share
3 answers

Try with this:

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

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

In this case, the markup for HyperLinkField will be as follows:

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

But this will only work if you add pageRequested as a general field or property to the object that is returned by the GetData method.

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

+3
source

Replace HYPERLINKFIELD with a TemplateField containing HyperLink and bind it in the grid 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; } } 
+1
source

Source: https://habr.com/ru/post/1491385/


All Articles