Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Querystring Parameter to GridView ItemTemplate

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

What is the best method to achieve it? Is there a way in which we can handle it using markup code only?

  <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:

  1. Set Gridview DataNavigateUrlFormatString Dynamically inside User Control(ASCX)

  2. How do I add "&Source" to DataNavigateUrlFormatString?

  3. Select row in GridView with JavaScript

  4. How to bind the URL of a GridView HyperLinkField when the bound value contains a colon?

  5. asp.net gridview DataNavigateUrlFormatString from DataSource

like image 743
LCJ Avatar asked May 25 '12 10:05

LCJ


1 Answers

Try using the 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 putting it inside your href.

You can substitute the DataTextField if you like - I just put the ConsumerID there to be consistent with your example.

like image 119
Filburt Avatar answered Oct 12 '22 23:10

Filburt