7

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

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I used HyperLinkField similar to BoundField and it worked.
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.