简体   繁体   English

将Querystring参数添加到GridView ItemTemplate

[英]Adding Querystring Parameter to GridView ItemTemplate

I have a gridview with a hyperlink in first column. 我在第一列中有一个带有超链接的gridview。 Upon clicking the hyperlink, the user is redirected to Vendor.aspx. 单击超链接后,用户将重定向到Vendor.aspx。 Now, I need to pass the consumer id (of the clicked row) as a query string to the Vendor.aspx. 现在,我需要将消费者ID(被点击的行)作为查询字符串传递给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) 在用户控件(ASCX)中动态设置Gridview DataNavigateUrlFormatString

  2. How do I add "&Source" to DataNavigateUrlFormatString? 如何向DataNavigateUrlFormatString添加“&Source”?

  3. Select row in GridView with JavaScript 使用JavaScript在GridView中选择行

  4. How to bind the URL of a GridView HyperLinkField when the bound value contains a colon? 当绑定值包含冒号时,如何绑定GridView HyperLinkField的URL?

  5. asp.net gridview DataNavigateUrlFormatString from DataSource 来自DataSource的asp.net gridview DataNavigateUrlFormatString

Try using the DataNavigateUrlFormatString 尝试使用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 . ...当它放入你的href时,它将为你提供Eval()以及单/双引号的问题。

You can substitute the DataTextField if you like - I just put the ConsumerID there to be consistent with your example. 如果您愿意,可以替换DataTextField - 我只是将ConsumerID放在那里以与您的示例保持一致。

Rewrite your hyperlink in gridview in .aspx file like this: 在.aspx文件中重写gridview中的超链接,如下所示:

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

Then in code-behind create a RowDataBound event handler: 然后在代码隐藏中创建一个RowDataBound事件处理程序:

    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 您可以在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
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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