how did you bind the DataGrid in your web control? care to show your code?

解决方案 »

  1.   

    DataGrid的数据源(你的datatable)是null了,
    检查一下。
      

  2.   

    控件代码如下:
    //////////////////////////////////////////////////////
    //////////////////////////////////////////////////////
    <%@ Control Language="vb" EnableViewState="True" AutoEventWireup="true" Codebehind="classindex6.ascx.vb" Inherits="website.classindex6" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True" OnPageIndexChanged="DataGrid1_PageIndexChanged" EnableViewState="True"
    ShowHeader="False" AutoGenerateColumns="false"
    BorderColor="black" PagerStyle-HorizontalAlign="center" BorderWidth="0" GridLines="Both"
      CellPadding="3"
            CellSpacing="0"
            Font-Name="Verdana"
            Font-Size="8pt"
            HeaderStyle-BackColor="#aaaadd">
    <Columns>
    <asp:HyperLinkColumn NavigateUrl="sdfsdf" ItemStyle-Width=250px
    DataTextField="ArticleTitle" 
    DataNavigateUrlFormatString="/active/open.aspx?id={0}"
    DataNavigateUrlField="ArticleID"
    Target=_blank>
    </asp:HyperLinkColumn>
    <asp:BoundColumn DataField="ArticleHit"  ItemStyle-Width=5px>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="ArticleTime" DataFormatString="{0:d}"  ItemStyle-Width=68px></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>控件classindex6.ascx.vb如下:
    //////////////////////////////////////////////////////
    //////////////////////////////////////////////////////
    Imports System.data
    Imports System.data.SqlClient
    Imports System.Configuration
    Public Class classindex6
        'update 2003.7.5
        Inherits System.Web.UI.UserControl
        Public ClassID As String
        Public PageSize As String
        Public LineLength As Integer
        Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid    Private objConn As New SqlConnection(ConfigurationSettings.AppSettings("sqldsn"))    Private objReader As SqlDataReader
        Private objDataAdapter As New SqlDataAdapter
        Private objDataSet As New DataSet    Sub BindGrid()
            Dim objCmd As New SqlCommand("SELECT ArticleOriginal,ArticleID,ArticleFileName,ArticlePic,ArticlePath,ArticleTitle,ArticleTime,ArticleNominate,ArticleHit,ArticleTitleB FROM Article where ClassID='" + ClassID + "'", objConn)
            objDataAdapter.SelectCommand = objCmd
            objDataAdapter.Fill(objDataSet, "Article")
            DataGrid1.DataSource = objDataSet.Tables("Article").DefaultView
            DataGrid1.PagerStyle.Mode = PagerMode.NumericPages
            DataGrid1.PageSize = PageSize
            DataGrid1.DataBind()
        End Sub
        Sub DataGrid1_PageIndexChanged(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
            DataGrid1.CurrentPageIndex = e.NewPageIndex
            BindGrid()
        End Sub#Region " Web 窗体设计器生成的代码 "    '该调用是 Web 窗体设计器所必需的。
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()    End Sub    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
        '不要删除或移动它。
        Private designerPlaceholderDeclaration As System.Object    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
            '不要使用代码编辑器修改它。
            InitializeComponent()
        End Sub#End Region    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not Page.IsPostBack Then
                DataGrid1.CurrentPageIndex = 0
                BindGrid()
            End If
        End SubEnd Class
      

  3.   

    控件调用页面如下:
    ///////////////////////////////////////////////
    ///////////////////////////////////////////////
    <%@ Register TagPrefix="pageUnit" TagName="classindex6" Src="classindex6.ascx" %>
    <%@ Page Language="vb" AutoEventWireup="true" EnableViewState="True" Codebehind="testdata2.aspx.vb" Inherits="website.testdata2"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
      <head>
        <title>testdata2</title>
        <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
        <meta name=vs_defaultClientScript content="JavaScript">
        <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
      </head>
      <body MS_POSITIONING="GridLayout">    <form id="Form1" method="post" runat="server">
    <pageunit:classindex6 id="Classindex6" runat="server" ClassID="other" PageSize=20></pageunit:classindex6>
        </form>
      </body>
    </html>页面testdata2.aspx.vb如下:
    ///////////////////////////////////////////////
    ///////////////////////////////////////////////
    Public Class testdata2
        Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 "    '该调用是 Web 窗体设计器所必需的。
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()    End Sub    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
        '不要删除或移动它。
        Private designerPlaceholderDeclaration As System.Object    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
            '不要使用代码编辑器修改它。
            InitializeComponent()
        End Sub#End Region    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '在此处放置初始化页的用户代码            Page.DataBind()
        End SubEnd Class
      

  4.   


    你在testdata2.aspx.vb的Page_Load中Page.DataBind()这会将PAGE中的所有控键重新Bind在调用Page.DataBind()前你ASMX中的datagrid是由BindGrid()来绑定数据的
    在调用Page.DataBind()后由于他对所有数据重新绑定,而此时ASMX中的datagrid以无数据源来绑定所有会不见这个控件了建议你在Page_Load中不要使用Page.DataBind()
    而是针对固定控键进行myActive.DataBind()
      

  5.   

    你要知道以下两点,你的问题就不难理解了
    1。Page的Page_Load比子控件的Page_Load先调用
    2。如果Page调用DataBind(),所有的子控件的DataBind()都将被iteratively调用第一次请求时,Page的Page_Load调用DataBind(),所有的子控件的DataBind()都将被iteratively调用,但其时DataGrid的DataSource是null,所有参数reset,所以没有绑定,随后子控件的Page_Load被调用,DataGrid被正确绑定当你选择PostBack时(无论是改变页数还是按个按钮),Page的Page_Load调用DataBind(),所有的子控件的DataBind()都将被调用,但其时DataGrid的DataSource是null,所有参数(包括跟分页有关的参数等等)reset,ViewState也被消除,其PageIndexChanged事件不再被调用。
    即使你的ASCX的Page_Load被调用,但因为是IsPostBack,也无济于事了