0

词汇表.aspx

这是该网站的 html 代码。我计划添加一个登录名,以便人们可以根据活动目录编辑表格。

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title spellcheck="true">Lexis Nexis Glossary</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="margin-left: 720px">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </div>
        <asp:SqlDataSource ID="TedGlossary" runat="server" ConnectionString="<%$ ConnectionStrings:Glsry_Taylor %>" SelectCommand="SELECT [TermText], [DefNbr], [DefVerNbr], [DefText], [AmplifyingExplanationText], [SeeAlsoText], [AuthoritativeSrcText], [ScopeName], [DomnName], [GovernanceStateName], [LastUpdtTimestamp] FROM [Glossary] ORDER BY [TermText]"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server"  AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
            " DataKeyNames="TermText,DefNbr,DefVerNbr" DataSourceID="TedGlossary" EnableSortingAndPagingCallbacks="True">
            <Columns>
                <asp:BoundField DataField="TermText" HeaderText="Term" ReadOnly="True" SortExpression="TermText" />
                <asp:BoundField DataField="DefNbr" HeaderText="Definition #" ReadOnly="True" SortExpression="DefNbr" />
                <asp:BoundField DataField="DefVerNbr" HeaderText="Definition Vers #" ReadOnly="True" SortExpression="DefVerNbr" />
                <asp:BoundField DataField="DefText" HeaderText="Definition" SortExpression="DefText" />
                <asp:BoundField DataField="AmplifyingExplanationText" HeaderText="Amplifying Explanation" SortExpression="AmplifyingExplanationText" />
                <asp:BoundField DataField="SeeAlsoText" HeaderText="See Also" SortExpression="SeeAlsoText" />
                <asp:BoundField DataField="AuthoritativeSrcText" HeaderText="Authoritative Source" SortExpression="AuthoritativeSrcText" />
                <asp:BoundField DataField="ScopeName" HeaderText="Scope Name" SortExpression="ScopeName" />
                <asp:BoundField DataField="DomnName" HeaderText="Domn Name" SortExpression="DomnName" />
                <asp:BoundField DataField="GovernanceStateName" HeaderText="Governance State" SortExpression="GovernanceStateName" />
                <asp:BoundField DataField="LastUpdtTimestamp" HeaderText="Last Update" SortExpression="LastUpdtTimestamp" />
            </Columns>
        </asp:GridView>
    </form>
    <script>
        $(function () {
            $('#<%=GridView1.ClientID%>').dataTable();
        });
    </script>

</body>
</html>

词汇表.aspx.cs

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

namespace Home
{
    public partial class Glossary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.PreRender += new EventHandler(GridView1_PreRender);
        }
        protected void GridView1_PreRender(object sender, EventArgs e)
        {
           if (GridView1.Rows.Count > 0)
           {
             //forces grid to render thead/th elements 
             GridView1.UseAccessibleHeader = true; 
             GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
           }
        }
    }
}

我收到 GridView 1 在其当前上下文中不存在的错误。我不确定为什么会这样。任何见解将不胜感激。

4

3 回答 3

2

您似乎错过了 Web 表单顶部的页面声明。

你应该有这样的东西,将前端和后端连接起来。这基本上告诉设计视图支持类文件在哪里。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Glossary.aspx.cs" Inherits="Glossary" %>

如果“可能”需要改为这个 - 我看到你的代码隐藏中有一个命名空间

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Glossary.aspx.cs" Inherits="Home.Glossary" %>

这将解释您看到的错误,因为您的 .aspx.cs 文件在没有该页面指令的情况下对设计视图一无所知。

于 2013-07-10T20:03:12.963 回答
0

对于初学者,请DataKeyNames删除GridView.

于 2013-07-10T19:35:31.950 回答
0

这可能是由未在 Glossary.aspx.designer.cs 文件中声明的 GridView1 引起的。可能需要首先创建文件和/或可能需要手动添加对 GridView1 的引用。

示例 Glossary.aspx.designer.cs:

public partial class Glossary {
  protected global::Global.System.Web.UI.WebControls.GridView GridView1;
}
于 2016-03-16T15:22:39.297 回答