简体   繁体   中英

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

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.

Rewrite your hyperlink in gridview in .aspx file like this:

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

Then in code-behind 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 it helps.

You can do same using at 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
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM