【问题标题】:How to pass code behind variable via hyperlinkfield如何通过超链接字段在变量后面传递代码
【发布时间】:2013-04-02 03:46:05
【问题描述】:

这是我在 C# 中的代码。

protected void Page_Load(object sender, EventArgs e)
    {
           Name = "Nitin";                      
    } 

我有一个 GridView,其中有一个 Hyperlinkfield。我想通过 HyperLinkField 将 Name 从 C# 页面(后面的代码)发送到下一页,但它不是 GridView 的 BoundField 之一(即我从 EntityDataSource 获取)。这是我的 asp.net 代码。

代码

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="WorkItemsEntityDataSource">
    <Columns>
        <asp:hyperlinkfield datatextfield="Id"                    
                datanavigateurlfields="Id"
                datanavigateurlformatstring="~\WorkItems.aspx?Id={0}"          
                headertext="Id"/> 
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
        <asp:BoundField DataField="TFSId" HeaderText="TFSId" SortExpression="TFSId" />
        <asp:CheckBoxField DataField="IsBillable" HeaderText="IsBillable" SortExpression="IsBillable" />
    </Columns>
</asp:GridView>

【问题讨论】:

  • 发布您的网格视图代码并清楚地阐述您的问题..

标签: c# asp.net .net hyperlink


【解决方案1】:

你也可以像这样从后面的代码中传递导航 URL

在 Aspx 页面中

              <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="hyp" runat="server" Text="Navigation"></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>    

在这样的用户网格视图数据绑定事件背后的代码中

protected void grddata_RowDataBound(object sender, GridViewRowEventArgs e)
    {    
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int ID = Convert.Int32(DataBinder.Eval(e.Row.DataItem, "ID"));
            HyperLink hyp = (HyperLink)e.Row.FindControl("hyp");
            hyp.NavigateUrl = "Test.aspx?Name='" + Request.QueryString["test"] + "'&&ID"+ ID +"";
        }
    }

我认为这会对你有所帮助...

【讨论】:

  • 嗨 Rajpurohit,我想知道如何通过 HyperLinkField 从 GridView 的绑定字段中发送 ID。
【解决方案2】:

我不确定实际上是否在数据绑定控件中使用它,但是您正在构建的 url 肯定不包含 QueryString 参数“名称”。它应该是这样的:

~\WorkItems.aspx?Id={0}&Name={1}

如果不需要 Id,只需将 Id 替换为 Name:

~\WorkItems.aspx?Name={0}

至于替换 - 我不知道您的数据对象有哪些属性以及如何绑定到它们。我认为你没有问题,因为绑定到 Id 工作(可能)。

更新

如果 Name 没有从数据源返回,您应该创建包含 Name 字段的新实体,或者使用一些常量名称,就像您在页面中声明一样(不确定是否应该在每一行中显示相同的名称)。 第二个选项可以这样实现:

~\WorkItems.aspx?Id={0}&Name=<%= Name %>

【讨论】:

  • 实际上命名不是我从 EntityDataSource 获得的那个,那是变量背后的代码。所以,你知道如何发送,请回复。任何帮助将不胜感激。
  • 发布了关于将服务器变量嵌入 ASP 页面的更新
【解决方案3】:

你可以使用:

<asp:TemplateField HeaderText="test">
     <ItemTemplate>
           <asp:HyperLink runat="server" ID="temp" NavigateUrl='<%# String.Format("~\WorkItems.aspx?Id={0}&Name={1}",  Eval("Name"), Name)%>' ></asp:HyperLink>
      </ItemTemplate>
</asp:TemplateField>

【讨论】:

    【解决方案4】:

    您可以通过以下方式修改HyperLinkField 的网址:

    protected void pendingAccountsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((HyperLink) e.Row.Cell[0].Controls[0]).NavigateUrl = "http://stackoverflow.com";
        }
    }
    

    只需更改它以在e.Row.Cell[0] 中指定正确的索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 2011-10-16
      • 2019-07-14
      • 2017-02-23
      • 2013-10-17
      • 1970-01-01
      相关资源
      最近更新 更多