GridView RowDataBound事件未运行

问题描述 投票:1回答:1

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

当我在if语句中设置断点时,程序为does not stop at the breakpoint

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>

为什么我的RowDataBound事件没有运行,我该怎么做才能修复它?

c# asp.net visual-studio gridview tableadapter
1个回答
1
投票

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

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

接下来,您将正确的方法从后面的代码绑定到GridView,但是在将数据绑定到GridView之后。所以绑定productAdapter.GetData()时它不起作用。

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

© www.soinside.com 2019 - 2024. All rights reserved.