【问题标题】:Dynamic link in asp.net gridviewasp.net gridview中的动态链接
【发布时间】:2014-08-14 17:13:50
【问题描述】:

我正在尝试创建一个带有额外列的网格视图,该列包含每一行的超链接。不幸的是,我的超链接不是从列的顶部开始,而是从超链接列的第二行开始。

查看此图片了解更多信息>>> http://i.imgur.com/TLsVo5s.png

正如您在该图片中看到的那样,有一个包含超链接的“视图”列,但问题是第一行始终为空。第二行的超链接应该在第一行,第三行应该在第二行,以此类推。

谁能告诉我哪里出错了?

这是我在我的 aspx 页面上的 gridview 声明:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" 
    OnPageIndexChanging="GridView1_PageIndexChanging" 
    OnSorting="GridView1_Sorting" PageSize="20" DataKeyNames="no_kwitansi"
    DataSourceID="home1" BackColor="White" BorderColor="#CC9966" BorderStyle="None"
    BorderWidth="1px" RowStyle-Wrap="False" OnRowDataBound="GridView1_RowDataBound">
    <AlternatingRowStyle BackColor="#CCCCCC" />
    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
    <RowStyle BackColor="White" ForeColor="#330099" />
    <RowStyle HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
    <SortedAscendingCellStyle BackColor="#FEFCEB" />
    <SortedAscendingHeaderStyle BackColor="#AF0101" />
    <SortedDescendingCellStyle BackColor="#F6F0C0" />
    <SortedDescendingHeaderStyle BackColor="#7E0000" />
    <Columns>
        <asp:BoundField HeaderText="#" />
        <asp:BoundField DataField="no_kwitansi" HeaderText="No.Kwitansi" SortExpression="no_kwitansi" ReadOnly="True" />
        <asp:BoundField DataField="nama_vendor" HeaderText="Vendor" SortExpression="nama_vendor" />
        <asp:BoundField DataField="nama_pekerja" HeaderText="Pekerja" SortExpression="nama_pekerja" />
        <asp:BoundField DataField="nama_penanggungjawab" HeaderText="Penanggungjawab" SortExpression="nama_penanggungjawab" />
        <asp:BoundField DataField="satuan" HeaderText="Satuan" SortExpression="satuan" />
        <asp:BoundField DataField="jumlah" HeaderText="Nominal" SortExpression="jumlah" />
        <asp:BoundField DataField="tanggal" HeaderText="Tanggal" SortExpression="tanggal" />
    </Columns>
</asp:GridView>

这是我的 C# 代码:

这是我的page_load函数,我在这里创建了模板字段。

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["SortExpr"] = Sort_Direction;

            TemplateField tfield = new TemplateField();
            tfield.HeaderText = "View";
            GridView1.Columns.Add(tfield);

            home1.DataBind();
        }
    }

这是我的 gridview rowDataBound 函数,我在其中创建超链接。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hlContro = new HyperLink();
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                hlContro.NavigateUrl = "./Home.aspx?ID=" + GridView1.Rows[i].Cells[1].Text;
                //hlContro.ImageUrl = "./sample.jpg";
                hlContro.Text = "Documents";
                //GridView1.Rows[i].Cells[0].Controls.Add(hlContro);
            }
            e.Row.Cells[8].Controls.Add(hlContro);
        }
    }

【问题讨论】:

  • 请在您的 aspx 中添加 gridview 声明

标签: c# asp.net gridview hyperlink


【解决方案1】:

那么为什么不只是一个模板字段,并删除所有服务器端样板?如果你改变列的位置会发生什么?

以下是您无需在服务器端代码中编写任何内容的解决方案。简单易行。

<asp:TemplateField HeaderText="Active">
   <ItemTemplate>
     <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("no_kwitansi") %>' 
        NavigateUrl= '<%# "./Home.aspx?ID=" + Eval("no_kwitansi") %>'>
     </asp:HyperLink>

   </ItemTemplate>

</asp:TemplateField>

【讨论】:

    【解决方案2】:

    当数据行(由 GridViewRow 对象表示)绑定到 GridView 控件中的数据时,将引发 RowDataBound 事件。这使您能够提供一种事件处理方法来执行自定义例程,例如在发生此事件时修改绑定到行的数据的值。

    就这样写代码

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    HyperLink hlContro = new HyperLink();
    
                        hlContro.NavigateUrl = "./Home.aspx?ID=" + e.Row.Cells[1].Text;
                        //hlContro.ImageUrl = "./sample.jpg";
                        hlContro.Text = "Documents";
                        //GridView1.Rows[i].Cells[0].Controls.Add(hlContro);
    
                    e.Row.Cells[8].Controls.Add(hlContro);
                }
            }
    

    【讨论】:

      【解决方案3】:

      你为什么要从后面的代码中添加它,即使你可以添加到 html 页面中

       <asp:TemplateField HeaderText="View">
                      <ItemTemplate> 
         <asp:LinkButton ID="test1" runat="server" ForeColor="#525252" Text="Documents" />
                      </ItemTemplate>
                  </asp:TemplateField>
      

      此链接上显示了类似的示例,您可以尝试 Link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-17
        • 2016-06-02
        • 1970-01-01
        • 2012-10-28
        • 1970-01-01
        相关资源
        最近更新 更多