English 中文(简体)
正在将查询参数添加到 GridgeView 项目模板
原标题:Adding Querystring Parameter to GridView ItemTemplate

我在第一列中有一个带有超链接的网格视图。 点击超链接后, 用户会被重定向到 supperal. aspx 。 现在, 我需要将消费者( 点击的行) ID 作为查询字符串传送到 supperal. aspx 。

实现该目标的最佳方法是什么?我们是否有办法只使用标记代码来处理它?

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

<强 > READINDs:

  1. < a href=>>Set Griview Data NavigateUrlFormatString 动态内用户控制

  2. < a href=" "https://stackoverflow.com/ questions/5949860/how-do-i-add-source-source-to-datanavigateurlformatstring" "数据 NavigateUrlFormatString 中如何添加“ & amp; 来源”?

  3. < a href=" "https://stackoverflow.com/ questions/5605637/select-row-row- in-grigview- with-javascript" > 与 JavaScript 连接的GridView 选择行

  4. < a href=" "https://stackoverflow.com/ questions/6539667/how-to-bind-the-url-of-a-gridview-hyperlinkfield- when-the-bound-value-contains-a",当捆绑值包含结肠时,如何绑定网格View HyperLink Field 的 URL?

  5. asp.net 网格view Data NavigateUrlFormatString from Data Source

最佳回答

尝试使用 DataNavigateUrlFormatString 尝试使用 < dataNavigateUrlFormatring

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

... 它会免除您 < code> Eval () , 以及将单引/双引号放入您的 < code>href 时的单引/双引号问题 。

您可以替换 DataText Field , 如果您喜欢的话 - 我只是把 ConcommerID 放在那里, 以便与您的示例保持一致 。

问题回答

在.aspx 文件的网格视图中重写您的超链接 :

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

然后在代码后端创建一个 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;
    }
}

希望能帮上忙

您可以在 Grid视图项目中使用数据环事件 来做同样的事情。

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签