【问题标题】:how to bind value to hyperlink in gridview in c# code behind如何在后面的c#代码中将值绑定到gridview中的超链接
【发布时间】:2013-01-16 10:30:18
【问题描述】:

我有一个自定义网格,我在后面的 c# 代码中绑定了数据。我已经为我的一个专栏提供了一个超链接字段。如果我单击超链接值,它应该导航到该超链接值的详细信息页面。代码如下,

  protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink myLink = new HyperLink();
            myLink.Text = e.Row.Cells[2].Text;
            e.Row.Cells[2].Controls.Add(myLink);
            myLink.NavigateUrl = "Estimation.aspx?EstimateID=" + EstimateID + "&VersionNo=" + VersionNo;
        }
    }

如果我单击该链接,页面将被导航,但我没有得到该页面中已经预加载的详细信息。请给我有关如何合并它的建议。 谢谢

【问题讨论】:

  • but i am not getting the details which are already pre-loaded in that page. 你能详细解释一下吗.. 你说的是哪个页面?以及预加载的值是多少?
  • 您能否解释一下使用查询字符串值 EstimateID 和 VersionNo 重定向到 Estimation.aspx 页面后的确切行为。
  • 我认为,你需要处理 Estimation.aspx 页面的 PageLoad 事件。但是您的问题并不完全清楚。
  • 嗨。有一个名为 EstimateList 的页面,其中我有网格和超链接列。如果我单击该链接,那么它应该重定向到“估计”页面,在那里我可以获取有关在上一页中创建的估计的详细信息。
  • 那么当您点击链接时,它会导航到哪个 URL?它是否填充了EstimateID 和VersionNo 属性,或者它们是否缺失?问题是您的代码生成了错误的链接,或者该链接没有返回您期望的内容。是哪个?

标签: c# gridview binding hyperlink


【解决方案1】:

你可以用这个来重定向,阅读this

<asp:HyperLink ID="HyperLink1"   
               runat="server"   
               NavigateUrl="Default2.aspx">  
                 HyperLink  
</asp:HyperLink> 

要添加带有链接的属性,只需添加

HyperLink1.Attributes.Add ("");

【讨论】:

    【解决方案2】:

    你需要在 RowDataBound 事件中做一个小改动

    myLink.Attributes.Add("href","你的网址");

    【讨论】:

      【解决方案3】:

      您需要从网格行数据中获取EstimateID 和VersionNo 的值。查看GridViewRowEventArgs 的文档,您会看到有一个 .Row 属性。

      所以你的代码需要是这样的:

      myLink.NavigateUrl = "Estimation.aspx?EstimateID=" + e.Row.Cells[4].Text + "&VersionNo=" + e.Row.Cells[5].Text;
      

      或者,也许您需要获取与网格行关联的数据项,在这种情况下,请查看 e.Row.DataItem,GridViewRow.DataItem property。此 DataItem 需要转换为您绑定到网格的数据类型才能从中获取数据,这可能类似于:

      ((MyCustomDataRow)e.Row.DataItem).EstimateID
      

      【讨论】:

        【解决方案4】:

        尝试以下解决方案:

        Page-1 是您的列表页面: ASPX 代码:

        <asp:GridView ID="GridView1" runat="server" 
                onrowdatabound="GridView1_RowDataBound">
            <Columns>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
            </ItemTemplate>
            </asp:TemplateField>
            </Columns>
            </asp:GridView>
        

        代码背后:

        protected void Page_Load(object sender, EventArgs e)
            {
                List<Data> lstData = new List<Data>();
                for (int index = 0; index < 10; index++)
                {
                    Data objData = new Data();
                    objData.EstimateID = index;
                    objData.VersionNo = "VersionNo" + index;
                    lstData.Add(objData);
                }
        
                GridView1.DataSource = lstData;
                GridView1.DataBind();
            }
        
            public class Data
            {
                public int EstimateID { get; set; }
                public string VersionNo { get; set; }
            }
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    HyperLink HyperLink1 = e.Row.FindControl("HyperLink1") as HyperLink;
                    HyperLink1.NavigateUrl = "Details.aspx?EstimateID=" + e.Row.Cells[1].Text + "&VersionNo=" + e.Row.Cells[2].Text;
                }
            }
        

        Page-2 是您的详细信息页面: 后面的代码:

        protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write(Request.QueryString["EstimateID"].ToString());
                Response.Write(Request.QueryString["VersionNo"].ToString());
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-01-27
          • 1970-01-01
          • 2017-06-14
          • 2021-05-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多