html:
------------------------------------------
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 304px; POSITION: absolute; TOP: 128px" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" Width="480px" Height="320px">
<Columns>
     <asp:TemplateColumn>
<ItemTemplate>
<uc1:SnackShow id="SnackShow1" runat="server" SnackID=<%# DataBinder.Eval(Container.DataItem, "id") %>></uc1:SnackShow>
</ItemTemplate>
     </asp:TemplateColumn>
</Columns>
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
----------------------------------------------------------------------------------
代码
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
dgDataBind();
} }
private void dgDataBind()
{
Business.MovieNews mn = new MovieNews();声明业务层对象
this.DataGrid1.DataSource = mn.GetNews("");//获取dataset
this.DataGrid1.DataBind();
}
private void DataGrid1_PageIndexChanged(object source,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex =e.NewPageIndex;
dgDataBind();
}
-----------------------------------------
症状:
一开始运行,dg里的控件显示正常,但是一翻页,dg里的控件就没有绑定(因为dg里的控件有图片
显示叉号,还有其他字段没有赋值)。小弟百思不得其解
烦请各位高手指点

解决方案 »

  1.   

    改写一下控件:
    public SnackID
    {
       get
       {
       .....
       }
       set
       {
           this.TextBox1.Text=".....";
           this.Image1 .......
       }
    }
      

  2.   

    回复Eddie005(╬) 暴赱 (╬) ( ) 信誉:100 照你那么写我嵌套用户控件还有意义吗
    我就是想传一个ID进去其他什么都不用做
      

  3.   

    你的用户控件里面是怎么控制的?
    翻页的时候对DataGrid1重新绑定了。。
    <uc1:SnackShow id="SnackShow1" runat="server" SnackID=<%# DataBinder.Eval(Container.DataItem, "id") %>></uc1:SnackShow>
    上面的SnackID应该变了。也就是说应该出在你的用户控件。。
      

  4.   

    估计原因,如果控件的属性SnackID在set的时候没有相应的代码,那么它只是简单的给变量赋值就算了,
    也许你希望在控件的Page_Load里根据属性SnackID做文章,但是这些事件执行的先后顺序是这样的:
    主页面Page_Load -> 控件Page_Load -> DataGrid翻页 -> DataGrid绑定
      

  5.   

    snackshow代码
    namespace DVD.UserControls
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using DVD.Business;
    /// <summary>
    /// SnackShow 的摘要说明。
    /// </summary>
    public class SnackShow : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.Image img;
    protected System.Web.UI.WebControls.Label id;
    protected System.Web.UI.WebControls.Label MovieName;
    protected System.Web.UI.WebControls.TextBox Mount;
    protected System.Web.UI.WebControls.Button Buy;
    protected System.Web.UI.WebControls.Label Price; private void Page_Load(object sender, System.EventArgs e)
    {
    if(!this.IsPostBack)
    {
    Business.MovieNews mo = new MovieNews(int.Parse(SnackID));
    this.MovieName.Text = mo.MovieName;
    this.Price.Text  = mo.Price;
    //this.id.Text  = SnackID;

    this.img.ImageUrl =  "../Web/showsmallimage.aspx?id="+ SnackID +"";

    Buy.Attributes["onClick"]="window.open('../../web/shopping/ShopCar.aspx?strID=" + ViewState["id"].ToString() + "&Mount=" + this.Mount.Text +" &visable=1','','width=800,height=800,top=1,left=1,scrollbars=yes');return false;";
    } } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.Buy.Click += new System.EventHandler(this.Buy_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void btnBuy_Click(object sender, System.EventArgs e)
    {
    // Response.Write("<script>window.open('../../../web/shopping/ShopCar.aspx?strID=" + id.Text + "&monut=" + this.Mount.Text +"');</script>"); } private void Buy_Click(object sender, System.EventArgs e)
    {
    Response.Write("<script>window.open('../../../web/shopping/ShopCar.aspx?strID=" + ViewState["id"].ToString() + "&monut=" + this.Mount.Text +"');</script>");
    }

    public String SnackID 
    {
    get 
    {
    return (string)ViewState["id"]; //return id.Text ;
    }
    set 
    {
    //id.Text = value;
    ViewState["id"]= value; }
    }
    public String SnackPrice 
    {
    get 
    {
    return Price.Text ;
    }
    set 
    {
    Price.Text = value;
    }
    }
    }
    }
    ---------------------------------------
      

  6.   

    private void DataGrid1_PageIndexChanged(object source,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    this.DataGrid1.CurrentPageIndex =e.NewPageIndex;
    dgDataBind();
    }
    少句柄了啊 所以没执行
      

  7.   

    解决方法 双击datagrid 或在后台代码中从列表里选Datagrid 重新生成一下DataGrid1_PageIndexChanged事件例如 以下是vb的
    Private Sub dgOrder_PageIndexChanged(ByVal source As Object, 
        ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgOrder.PageIndexChanged  你少了这个
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      

  8.   

    问题已经解决代码如下:
    ===========================================
    namespace DVD.UserControls
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using DVD.Business;
    /// <summary>
    ///SnackShow 的摘要说明。
    /// </summary>
    public class SnackShow : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.Label id;
    protected System.Web.UI.WebControls.TextBox Mount;
    protected System.Web.UI.WebControls.Button Buy;
    protected System.Web.UI.WebControls.Label dmerchant;
    protected System.Web.UI.WebControls.Label Label4;
    protected System.Web.UI.WebControls.Label PremieveDate;
    protected System.Web.UI.WebControls.Label Label8;
    protected System.Web.UI.WebControls.Label MovieLong;
    protected System.Web.UI.WebControls.Label Label7;
    protected System.Web.UI.WebControls.Label MovieLevel;
    protected System.Web.UI.WebControls.Label Label6;
    protected System.Web.UI.WebControls.Label MovieType;
    protected System.Web.UI.WebControls.Label Label5;
    protected System.Web.UI.WebControls.Label Player;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.Label Director;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.Label MoiveName;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Image img;
    protected System.Web.UI.WebControls.Label Label12;
    protected System.Web.UI.WebControls.Label Price; private void Page_Load(object sender, System.EventArgs e)
    { } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    } /// <summary>
    ///设计器支持所需的方法 - 不要使用代码编辑器
    ///修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.Buy.Click += new System.EventHandler(this.Buy_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void btnBuy_Click(object sender, System.EventArgs e)
    {
    //Response.Write("<script>window.open('../../../web/shopping/ShopCar.aspx?strID=" + id.Text + "&monut=" + this.Mount.Text +"');</script>"); } private void Buy_Click(object sender, System.EventArgs e)
    {
    Response.Write("<script>window.open('ShopCar.aspx?strID=" + ViewState["id"].ToString() + "&monut=" + this.Mount.Text +"');</script>");
    } public String SnackID 
    {
    get 
    {
    return (string)ViewState["id"]; //return id.Text ;
    }
    set 
    {
    //id.Text = value;
    ViewState["id"]= value;
    // if(!this.IsPostBack)
    // {
    Business.MovieNews mo = new MovieNews(int.Parse(SnackID));
    this.MoiveName.Text = mo.MovieName;
    this.Price.Text  = mo.Price;
    Director.Text = mo.Director;
    Player.Text = mo.MostylPlayer;
    MovieType.Text = mo.MovieType;
    MovieLevel.Text = mo.MovieLevel;
    MovieLong.Text = mo.MovieLong;
    PremieveDate.Text = mo.PremieveDate.ToShortDateString().ToString();
    //this.id.Text  = SnackID; this.img.ImageUrl =  "../Web/showsmallimage.aspx?id="+ SnackID +""; Buy.Attributes["onClick"]="window.open('ShopCar.aspx?strID=" + ViewState["id"].ToString() + "&Mount=" + this.Mount.Text +" &visable=1','','width=800,height=800,top=1,left=1,scrollbars=yes');return false;";
    // }
    }
    }
    public String SnackPrice 
    {
    get 
    {
    return Price.Text ;
    }
    set 
    {
    Price.Text = value;
    }
    }
    }
    }
      

  9.   

    主要是 
    set 
    {
    //id.Text = value;
    ViewState["id"]= value;
    //if(!this.IsPostBack)
    //{
    Business.MovieNews mo = new MovieNews(int.Parse(SnackID));
    this.MoiveName.Text = mo.MovieName;
    this.Price.Text  = mo.Price;
    Director.Text = mo.Director;
    Player.Text = mo.MostylPlayer;
    MovieType.Text = mo.MovieType;
    MovieLevel.Text = mo.MovieLevel;
    MovieLong.Text = mo.MovieLong;
    PremieveDate.Text = mo.PremieveDate.ToShortDateString().ToString();
    //this.id.Text  = SnackID;this.img.ImageUrl =  "../Web/showsmallimage.aspx?id="+ SnackID +"";Buy.Attributes["onClick"]="window.open('ShopCar.aspx?strID=" + ViewState["id"].ToString() + "&Mount=" + this.Mount.Text +" &visable=1','','width=800,height=800,top=1,left=1,scrollbars=yes');return false;";
    //}