已经不是分不分的事情了! 谁能帮帮我? 我还特意开个QQ群来探讨。如果小弟那里没写明白,请进来,小弟先谢谢大家了!
QQ群号码:24143061
要实现的功能:
在DataGrid里面的ItemTemplate项里添加一个我自己做的用户控件.然后在HTML页面内用给用户控件的StrProID属性付值.<ItemTemplate>
<uc1:lxdatagrid id="Lxdatagrid1" runat="server" StrProID='<%# DataBinder.Eval(Container.DataItem,"proID")%>'>
</uc1:lxdatagrid>
</ItemTemplate>然后,点击DataGrid的 属性生成器 设置分页.问题所在:
运行起来,第一次的第一页很OK.点击页码,在弹出的页面只能显示出控件的个数,上下页码数还是那么多! 但是用户控件却接收不到值了.再回第一页,值也没了.也就是说在第一次加载的第一页是OK的! 其他页面的用户控件是接收不到值的!即便是返回第一页。郁闷所在:
在 DataGrid的 属性生成器 添加如 超链接列、绑定列等,这么实现的分页就OK。我真是没办法了!
下面为这些页面的代码:
WebForm1.aspx<%@ Register TagPrefix="uc1" TagName="lxdatagrid" Src="lxdatagrid.ascx" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="lx.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="760" border="1">
<TR>
<TD></TD>
</TR>
<TR>
<TD>
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="744px" PageSize="5"
AllowPaging="True">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<uc1:lxdatagrid id="Lxdatagrid1" runat="server" StrProID='<%# DataBinder.Eval(Container.DataItem,"proID")%>'>
</uc1:lxdatagrid>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Position="TopAndBottom" Mode="NumericPages"></PagerStyle>
</asp:DataGrid></TD>
</TR>
<TR>
<TD></TD>
</TR>
<TR>
<TD></TD>
</TR>
</TABLE>
</FONT>
</form>
</body>
</HTML>WebForm1.aspx.csusing System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace lx
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
this.BindTODataGridDgrd();
}// 在此处放置用户代码以初始化页面
} private void BindTODataGridDgrd()
{ //绑定
//把大类绑定到本也的dgrd1上
//strY_SmallTypeID=Request.QueryString["SmallTypeID"].ToString();// strY_SmallTypeID=Request.QueryString["SmallTypeID"].ToString();
// strSmallTypeName=Bbs.BbsClass.BbsSmallType.BbsSmallTypeName(strY_SmallTypeID);
SqlConnection con=DB.createCon();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from product",con);
DataSet ds=new DataSet();
sda.Fill(ds,"emp");
this.DataGrid1.DataSource=ds.Tables["emp"];
this.DataGrid1.DataBind(); } #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
//this.sqlDataAdapter2.Fill   (this.dataSet11     );
// this.BindTODataGridDgrd();
this.BindTODataGridDgrd();
}
}
}lxdatagrid.ascx<%@ Control Language="c#" AutoEventWireup="false" Codebehind="lxdatagrid.ascx.cs" Inherits="lx.lxdatagrid" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="1">
<TR>
<TD><FONT face="宋体"></FONT></TD>
<TD></TD>
<TD>
<asp:Label id="Label1" runat="server">Label</asp:Label></TD>
</TR>
<TR>
<TD></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD></TD>
<TD></TD>
<TD></TD>
</TR>
</TABLE>
lxdatagrid.ascx.csnamespace lx
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; /// <summary>
/// lxdatagrid 的摘要说明。
/// </summary>
public class lxdatagrid : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1; private string strProID;
public string StrProID
{
set
{
this.strProID=value.ToString();
}
} private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
this.Label1.Text=strProID;
}// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}DB.csusing System;namespace lx
{
/// <summary>
/// DB 的摘要说明。
/// </summary>
public class DB
{
public DB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static System.Data.SqlClient.SqlConnection createCon()
{
return new System.Data.SqlClient.SqlConnection("server=192.168.0.200;database=dz;uid=sa;pwd=;"); }
}
}

解决方案 »

  1.   

    以下是前一段时间其他人问的问题的解决办法,问题与楼主是一样的,第一次对用户控件赋值成立,但页面一旦提交,同样的赋值,用户控件却无法接收到.
    --------------------------------------------------
    总算解决了这个问题,代码如下:/// <summary>
    /// 定义变量
    /// </summary>
    private string itemID_m = "1";
    /// <summary>
    /// 定义变量
    /// </summary>
    public string itemID_m
    {
    get
    {
    return(this.itemID_m);
    }
    set
    {
    this.itemID_m = value;
    //必须在此处直接调用你需要的代码
    this.myBind(value);
    }
    }//事件
    private void myBind(string myValue)
    {
    this.Label1.Text = myValue;
    }确实如楼主所说,在初次加载时,在用户控件里面调用this.itemID是没有问题的。但一旦页面提交之后,再进行同样的调用,却没有办法显示.
    解决的办法是,不要在用户控件里面事件执行的时候调用this.itemID,而是在该属性的set里面,直接指挥该事件,并且把获取的value直接传递到事件里面,如此就可以显示.
      

  2.   

    说说原理吧:
    主要是<%# DataBinder.Eval(Container.DataItem,"proID")%>'>在什么时候解析的问题。我们都知道是迟绑定。即在DataGrid的DataBind()的时候进行求值。。让我们来看看你的第一页是怎么绑定的:if(!this.IsPostBack)
    {
       this.BindTODataGridDgrd();
    }// 在此
    在此时数据绑定并解析DataBinder.Eval(),此时将会给StrProID赋值。
    赋值完毕后,Page页的Page_Load事件执行完毕。现在轮到用户控件lxdatagrid的Page_Load事件里。在这里this.Label1.Text=strProID;,即用户控件得到了值。。在后面翻页事件中,实质上就是一个this.IsPostBack==true的永恒成立。Page页是在private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
    //this.sqlDataAdapter2.Fill   (this.dataSet11     );
    // this.BindTODataGridDgrd();
    this.BindTODataGridDgrd();
    }
    }
    }中作DataGrid的DataBind事件。。这个过程就和上面一样。将会给StrProID赋值。但是这里问题就出来了。为什么呢?
    因为我们都知道在ASP。NET的页面周期里。Page_Load是在分页事件前执行的。当然用户控件的
    Page_Load事件也会在分页事件前执行。
    这样问题就出来了:
    this.Label1.Text=strProID在this.IsPostBack==true的时候永远会在分页事件前执行。看出问题了吧。我想只要
    public string StrProID
    {
    set
    {
    this.strProID=value.ToString();
    //多加一句:
       this.Label1.Text=strProID
    }
    }
    一切就应该OK了。。建议LZ多看看ASPNET的页面执行原理。