gridview HyperLink 动态改变

1、首先在在gridview 的column 列新增一个HyperLink 列

                    <asp:TemplateField HeaderText="合同信息" >
                                 <ItemTemplate>
                   
                                <asp:HyperLink ID="HyperLink1"  runat="server"  ></asp:HyperLink> 
                        </ItemTemplate>
                               <HeaderStyle HorizontalAlign="Center" Width="50px" />
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>

2、在 GridView1_RowDataBound 事件中进行动态管理代码如下

数据库中增加一个state ,新增一个state列来判断是新增还是修改

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                if (drv.Row["ywhetong"].ToString() == "1")
                {
                    ((HyperLink)e.Row.Cells[5].FindControl("HyperLink1")).Text = "管理";
                    ((HyperLink)e.Row.Cells[5].FindControl("HyperLink1")).NavigateUrl = string.Format("hetongNowEdit.aspx?projectid={0}", drv.Row["projectid"].ToString());
                }

                else
                {
                    ((HyperLink)e.Row.Cells[5].FindControl("HyperLink1")).Text = "新建合同";
                    ((HyperLink)e.Row.Cells[5].FindControl("HyperLink1")).NavigateUrl = string.Format("hetongNewAdd.aspx?id={0}", drv.Row["id"].ToString());
                }

            }
           
        }
原文地址:https://www.cnblogs.com/w6w6/p/10971999.html