c# - 如何通过超链接字段传递变量背后的代码

标签 c# asp.net .net hyperlink

这是我在 C# 中隐藏的代码。

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

我有一个 GridView,其中有一个 Hyperlinkfield。我想通过 HyperLinkField 从 C# 页面(代码后面的一个)发送 Name 到下一页,但它不是 BoundField 之一GridView(即我从 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>

最佳答案

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

在 Aspx 页面中

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

在代码隐藏的用户 GridView 数据绑定(bind)事件中是这样的

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 +"";
        }
    }

我想这对你有帮助...

关于c# - 如何通过超链接字段传递变量背后的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15923521/

相关文章:

c# - UserManager.IsInRoleAsync 和 UserManager.GetRolesAsync 彼此不一致

c# - GetEncoderParameterList 出现意外的 "Bitmap Region is already Locked"异常。有任何想法吗?

c# - 根据 Web API 2 上的请求和响应构建/映射模型的最佳方法

c# - 在 WPF 中处理来自 Winforms 组件的未处理异常

asp.net - 将外键应用于表时mysql中的错误代码1005

asp.net - Asp.net 中的 css3PIE 或 PIE.htc

javascript - 当其他文本框被填充时填充文本框

c# - Azure Durable Function Orchestration GetInput 不起作用

.net - 从 VBA 调用 COM 加载项选项

c# - 为什么这个 Unity XML 配置没有解析类型?