IPostBackDataHandler not working properly with DataGrid

Discussion in 'ASP .Net Datagrid Control' started by buzlite@sympatico.ca, Aug 11, 2006.

  1. Guest

    Hello All,

    I am having some difficulties with a user control implementing
    IPostBackDataHandler when it is placed inside a datagrid.

    I have created a user control (MyUserControl.ascx) containing 1 TextBox
    that implements the IPostBackDataHandler.
    I have also created a form (WebForm1.aspx) that contains 1 datagrid of
    which the datagrid contains the above user control as specified in its
    template.

    The purpose is to dynamically create one MyUserControl for each item in
    the datasource for display and editing.

    When the program is run and text is entered into MyUserControl's
    textbox, the LoadPostData() of MyUserControl.ascx is called as
    expected. But even though LoadPostData() returns true the
    corresponding RaisePostDataChangedEvent() is not called.


    Note: if I were to simply include the MyUserControl.ascx in
    WebForm1.aspx, then both LoadPostData() and RaisePostDataChangedEvent()
    of MyUserControl.ascx are called as expected.

    Would someone give me some insight as to why this is not working ? Am
    I missing a step somewhere ?


    Attached is the bare essential code snippets to reproduce the problem.


    MyUserControl.ascx
    -----------------------------------------------------
    <%@ Control Language="c#" AutoEventWireup="false"
    Codebehind="MyUserControl.ascx.cs" Inherits="test.MyUserControl"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>


    MyUserControl.ascx.cs
    -----------------------------------------------------
    public class MyUserControl : System.Web.UI.UserControl,
    IPostBackDataHandler, INamingContainer
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;


    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);

    if (Page != null)
    {
    Page.RegisterRequiresPostBack(this);
    }
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion


    public virtual bool LoadPostData(string postDataKey,
    System.Collections.Specialized.NameValueCollection postCollection)
    {
    return true;
    }

    public virtual void RaisePostDataChangedEvent()
    {
    }

    private void Page_Load(object sender, System.EventArgs e)
    {
    }
    }



    WebForm1.aspx
    ------------------------------------------------------
    <%@ Register TagPrefix="f4c" TagName="MyUserControl"
    Src="MyUserControl.ascx" %>
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    AutoEventWireup="false" Inherits="test.WebForm1" %>
    ....
    <form id="Form1" method="post" runat="server">
    <asp:datagrid id="dgListing" runat="server"
    AutoGenerateColumns="False">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <f4c:MyUserControl id="MyUserControl1"
    runat="server"></f4c:MyUserControl>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    </form>


    WebForm1.aspx.cs
    ------------------------------------------------------
    public class WebForm1 : System.Web.UI.Page
    {

    ....
    private void Page_Load(object sender, System.EventArgs e)
    {
    //setup handlers
    this.Button1.Click += new System.EventHandler(this.Button1_Click);

    //setup datagrid
    dgListing.DataSource = this.CreateDataSource();
    dgListing.DataBind();
    }

    private void Button1_Click(object sender, System.EventArgs e)
    {
    }

    ICollection CreateDataSource()
    {
    DataTable dt = new DataTable();
    DataRow dr;

    dt.Columns.Add(new DataColumn("SomeColumn", typeof(Int32)));

    for (int i = 0; i < 2; i++)
    {
    dr = dt.NewRow();
    dr[0] = i;
    dt.Rows.Add(dr);
    }

    DataView dv = new DataView(dt);
    return dv;
    }
    }
     
    , Aug 11, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Showjumper

    Re: Help w/ IPostBackDataHandler

    Showjumper, Jun 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,640
    Showjumper
    Jun 24, 2003
  2. One Handed Man \( OHM - Terry Burns \)

    Custom Control & System.Web.UI.IPostBackDataHandler.LoadPostData

    One Handed Man \( OHM - Terry Burns \), Jun 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,940
    One Handed Man \( OHM - Terry Burns \)
    Jun 17, 2004
  3. RSB
    Replies:
    0
    Views:
    386
  4. Replies:
    8
    Views:
    184
    Alessandro Zifiglio
    Aug 19, 2006
  5. mh

    ASP.Net Datagrid Not Working Properly

    mh, Jun 19, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    122
Loading...

Share This Page