【问题标题】:GridView RowDataBoundEvent not runningGridView RowDataBound 事件未运行
【发布时间】:2018-08-13 10:19:34
【问题描述】:

我正在使用 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>

为什么我的 RowDataBoundEvent 没有运行,我可以做些什么来解决它?

【问题讨论】:

    标签: c# asp.net visual-studio gridview tableadapter


    【解决方案1】:

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

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

    接下来,您将从后面的代码将正确的方法绑定到 GridView,但在将数据绑定到 GridView 之后。所以绑定productAdapter.GetData()的时候就不行了。

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

    【讨论】:

    • 非常感谢。我没有抓住那个!一切都按预期工作。我不敢相信我错过了。再次感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2014-08-30
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    相关资源
    最近更新 更多