简体   繁体   English

ASP.NET GridView中的动态链接

[英]Dynamic link in asp.net gridview

I'm trying to create a gridview with an extra column that contains a hyperlink for each row. 我正在尝试创建一个带有额外列的gridview,该列包含每一行的超链接。 Unfortunately my hyperlinks doesn't start from the top of the column, instead they are started from the second row of the hyperlink's column. 不幸的是,我的超链接不是从列的顶部开始,而是从超链接列的第二行开始。

See this picture for more information >>> http://i.imgur.com/TLsVo5s.png 有关更多信息,请参见此图片>>> http://i.imgur.com/TLsVo5s.png

As you can see in that picture, there is a 'view' column that contains hyperlinks, but the problem is the first row is always empty. 如您在该图中所看到的,有一个“视图”列包含超链接,但是问题是第一行始终为空。 The hyperlink that's on the second row should be on the first, and the third should be on the second, and so on. 第二行上的超链接应该在第一行上,第三行应该在第二行上,依此类推。

Can anyone show me where I went wrong? 谁能告诉我我哪里出问题了?

Here is my gridview declaration on my aspx page: 这是我的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>

Here is my C# code behind: 这是我后面的C#代码:

This is my page_load function, I created the template field here. 这是我的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();
        }
    }

Here is my gridview rowDataBound function, where I create the hyperlink. 这是我的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);
        }
    }

The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. 当数据行(由GridViewRow对象表示)绑定到GridView控件中的数据时,将引发RowDataBound事件。 This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs. 这使您能够提供一个事件处理方法,该方法将在发生此事件时执行自定义例程,例如修改绑定到该行的数据的值。

Just write code like this 像这样写代码

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

so why not just a template field, and remove all the server side boilerplate? 那么,为什么不只是一个模板字段,并删除所有服务器端样板呢? What happen if you would change position of your column? 如果您要更改列的位置会怎样?

Below is the solution where you need not to write anything in your server side code. 下面是不需要在服务器端代码中编写任何内容的解决方案。 Simple and easy. 简单容易。

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

why are you adding it from code behind even you can add in html page 你为什么要从代码后面添加它,即使你可以在html页面中添加

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

a similar example is shown on this link you can try Link 此链接上显示了类似的示例,您可以尝试链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM