Adding the Querystring Parameter to the GridView ItemTemplate

I have a gridview with a hyperlink in the first column. By clicking on the hyperlink, the user is redirected to Vendor.aspx. Now I need to pass the consumer identifier (clicked string) as a query string in Vendor.aspx.

What is the best way to achieve it? Is there a way that we can deal with this only with a markup code?

<asp:GridView ID="grdConsumers" runat="server" AutoGenerateColumns="False" EnableViewState="True" > <Columns> <asp:TemplateField HeaderText="ConsumerID" SortExpression="ConsumerID" > <ItemTemplate> <asp:HyperLink ID="lnkConsumerID" href="Vendor.aspx" runat="server"><%# Eval("ConsumerID")%></asp:HyperLink> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Status" DataField="Status" SortExpression="Status"></asp:BoundField> </Columns> </asp:GridView> 

READINGS:

+6
source share
3 answers

Try using DataNavigateUrlFormatString

 <ItemTemplate> <asp:HyperLinkField DataNavigateUrlFields="ConsumerID" DataTextField="ConsumerID" DataNavigateUrlFormatString="Vendor.aspx?id={0}" /> </ItemTemplate> 

... it will spare you Eval() and the problem with single / double quotes when you put it in your href .

You can substitute a DataTextField if you want - I would just put the ConsumerID according to your example.

+2
source

Rewrite the hyperlink in gridview in the .aspx file as follows:

 <asp:HyperLink ID="lnkConsumerID" runat="server" Text='<%# Eval("ConsumerID")%>' /> 

Then, in the image code, create a RowDataBound event handler:

  protected void grdConsumers_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType != DataControlRowType.DataRow) return; var hlnkhlnk = (HyperLink)e.Row.FindControl("lnkConsumerID"); if (hlnkhlnk != null) { hlnkhlnk.NavigateUrl = "Vendor.aspx" + "?Consumer ID=" + hlnkhlnk.Text; } } 

Hope this helps.

+1
source

You can do the same with the Grid view Item Data Bound Event

  protected void grdConsumers_ItemDataBound(object sender,DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { // Get your consumerId here ((HyperLink)e.Item.FindControl("Edit")).NavigateUrl = "Vendor.aspx?id=" + consumerId } } 
0
source

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


All Articles