GridView RowDataBoundEvent 不是 运行

GridView RowDataBoundEvent not running

我正在使用 GridView,我正在使用 TableAdapter 与我的 SQL Server 2017 Express 通信。我已经设法将 table 显示到 GridView 中,但我想让我数据库中每个条目的名称都有一个超链接,该超链接会将用户定向到另一个包含允许用户编辑的 DetailsView 的页面相应的条目。但是,我目前在使用 RowDataBoundEvent 时遇到了麻烦,因为它似乎没有触发。

当我在 if 语句处设置断点时,程序

protected void ProductViewRowBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        string hyperLink = e.Row.Cells[1].Text;
        e.Row.Cells[1].Text = "Test";
        //HyperLink link = new HyperLink;
    }
}

我检查了我的 RowDataBound 方法名称,它与我在 aspx 文件中指定的名称相匹配:

<asp:GridView ID="ProductView" runat="server" Height="299px" Width="578px" AllowPaging="True" HorizontalAlign="Center"
    OnRowDataBoundEvent="ProductViewRowBound" style="table-layout:auto">
    <HeaderStyle Width="300px" />
</asp:GridView>

CS 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class Maintenance : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            ProductSetTableAdapters.Product_Table_AlphaTableAdapter productAdapter = new ProductSetTableAdapters.Product_Table_AlphaTableAdapter();
            ProductView.DataSource = productAdapter.GetData();
            ProductView.DataBind();
        }

        ProductView.RowDataBound += new GridViewRowEventHandler(ProductViewRowBound);
    }

    protected void ProductViewRowBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            string hyperLink = e.Row.Cells[1].Text;
            e.Row.Cells[1].Text = "Test";
            //HyperLink link = new HyperLink;
        }
    }
}

ASPX 文件:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Maintenance.aspx.cs" Inherits="Maintenance" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div style="overflow-x:scroll;overflow-y:scroll; margin-left:auto; margin-right:auto;">
        <asp:GridView ID="ProductView" runat="server" Height="299px" Width="578px" AllowPaging="True" HorizontalAlign="Center"
            OnRowDataBoundEvent="ProductViewRowBound" style="table-layout:auto">
            <HeaderStyle Width="300px" />
        </asp:GridView>
    </div>

</asp:Content>

为什么我的 RowDataBoundEvent 不是 运行,我该如何解决它?

首先,OnRowDataBoundEvent不存在。在GridView中应该是OnRowDataBound.

<asp:GridView ID="ProductView" runat="server" OnRowDataBound="ProductViewRowBound">

接下来,您将从代码隐藏中将正确的方法绑定到 GridView,但是在将数据绑定到 GridView 之后。所以当你绑定 productAdapter.GetData().

时它不会起作用

因此要么在 GridView aspx 中设置正确的事件名称,要么将方法绑定移动到上方 ProductView.DataBind();