IPostBackDataHandler not working properly with DataGrid

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default IPostBackDataHandler not working properly with DataGrid

    Hello All,


    I've posted this in
    microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got not
    reply so I'll try my luck here.

    Any help would be greatly appreciated.
    Thanks



    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;
    }
    }

    buzlite@sympatico.ca Guest

  2. Similar Questions and Discussions

    1. Not working properly in Linux
      Hi. I use SuSE 10.0 and Flash player (7) isn't working properly. When I try to view a webpage with a Flash file, Firefox often freezes. Google...
    2. Help! Behaviors not working properly!
      Hello I'm developing a layer-heavy page that has interaction which allows the user to jump form layer to layer. Here's a semi-working page...
    3. Macromedia Flash Player installed and working properly suddenlys stops working..
      No idea what has caused the Flash player to stop working. This is not my machine but a relatives who has asked for help over the T'giving...
    4. Not working properly...
      For some reason the playing has whited out some images ont he flash games I play. For example walls will be white, or sometimes the entire...
    5. AI8.0 not working properly with new mac. HELP!
      I just installed AI 8.0 on my new emac with os 10.2 and the tools don't all show up in the tool box. I can't draw rectangles or any other shape. I...
  3. #2

    Default Re: IPostBackDataHandler not working properly with DataGrid

    > The purpose is to dynamically create one MyUserControl for each item in
    > the datasource for display and editing.
    How and where are you adding it?

    The best place is CreateChildControls() method.


    --
    Happy Hacking,
    Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    -------------------


    Gaurav Vaish \(www.EduJini.IN\) Guest

  4. #3

    Default Re: IPostBackDataHandler not working properly with DataGrid

    The user control is added inside the WebForm1.aspx file inside the
    column template of the datagrid control

    Thanks


    Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    > > The purpose is to dynamically create one MyUserControl for each item in
    > > the datasource for display and editing.
    >
    > How and where are you adding it?
    >
    > The best place is CreateChildControls() method.
    >
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    > [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    > -------------------
    buzlite@sympatico.ca Guest

  5. #4

    Default Re: IPostBackDataHandler not working properly with DataGrid

    Hello All,
    Any help would be appreciated.
    Thanks



    [email]buzlite@sympatico.ca[/email] wrote:
    > Hello All,
    >
    >
    > I've posted this in
    > microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got not
    > reply so I'll try my luck here.
    >
    > Any help would be greatly appreciated.
    > Thanks
    >
    >
    >
    > 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;
    > }
    > }
    buzlite@sympatico.ca Guest

  6. #5

    Default Re: IPostBackDataHandler not working properly with DataGrid

    Hi,

    The only missing thing that will need to fill, as I think, is a simple
    hidden field... so that it can be captured in LoadPostData.

    btw, what happens if you blindly return 'true' in LoadPostData?


    --
    Happy Hacking,
    Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    -------------------


    <buzlite@sympatico.ca> wrote in message
    news:1155567351.199524.188830@m79g2000cwm.googlegr oups.com...
    > Hello All,
    > Any help would be appreciated.
    > Thanks
    >
    >
    >
    > [email]buzlite@sympatico.ca[/email] wrote:
    >> Hello All,
    >>
    >>
    >> I've posted this in
    >> microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got not
    >> reply so I'll try my luck here.
    >>
    >> Any help would be greatly appreciated.
    >> Thanks
    >>
    >>
    >>
    >> 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;
    >> }
    >> }
    >

    Gaurav Vaish \(www.EduJini.IN\) Guest

  7. #6

    Default Re: IPostBackDataHandler not working properly with DataGrid

    Hello Gaurav,

    I do not know the reasoning for adding a hidden field and what purpose
    that would accomplish.
    >From the sample code attachments, when WebForm1.aspx is run and the
    Datagrid generates MyUserControl user control, any text that is entered
    in this user control TextBox would be properly posted back to the
    server. This is true in that when MyUserControl.ascx.cs,
    LoadPostData() is called, the data can be retrieved from the input
    parameter of this method. So...this is working OK.

    The problem is, when MyUserControl.ascx.cs, LoadPostData() returns a
    value of 'true', RaisePostDataChangedEvent() is never invoked. In the
    MyUserControl.ascx.cs code snippet, I hardcoded a return of 'true'.
    Even then, RaisePostDataChangedEvent() is never invoked. This is the
    main problem that I am experiencing.

    Any insights ?

    Thanks



    Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    > Hi,
    >
    > The only missing thing that will need to fill, as I think, is a simple
    > hidden field... so that it can be captured in LoadPostData.
    >
    > btw, what happens if you blindly return 'true' in LoadPostData?
    >
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    > [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    > -------------------
    >
    >
    > <buzlite@sympatico.ca> wrote in message
    > news:1155567351.199524.188830@m79g2000cwm.googlegr oups.com...
    > > Hello All,
    > > Any help would be appreciated.
    > > Thanks
    > >
    > >
    > >
    > > [email]buzlite@sympatico.ca[/email] wrote:
    > >> Hello All,
    > >>
    > >>
    > >> I've posted this in
    > >> microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got not
    > >> reply so I'll try my luck here.
    > >>
    > >> Any help would be greatly appreciated.
    > >> Thanks
    > >>
    > >>
    > >>
    > >> 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;
    > >> }
    > >> }
    > >
    buzlite@sympatico.ca Guest

  8. #7

    Default Re: IPostBackDataHandler not working properly with DataGrid

    Let me give it a shot by creating a dummy 'UserControl' on my own.

    --
    Happy Hacking,
    Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    -------------------


    <buzlite@sympatico.ca> wrote in message
    news:1155591996.737095.53890@b28g2000cwb.googlegro ups.com...
    > Hello Gaurav,
    >
    > I do not know the reasoning for adding a hidden field and what purpose
    > that would accomplish.
    >
    >>From the sample code attachments, when WebForm1.aspx is run and the
    > Datagrid generates MyUserControl user control, any text that is entered
    > in this user control TextBox would be properly posted back to the
    > server. This is true in that when MyUserControl.ascx.cs,
    > LoadPostData() is called, the data can be retrieved from the input
    > parameter of this method. So...this is working OK.
    >
    > The problem is, when MyUserControl.ascx.cs, LoadPostData() returns a
    > value of 'true', RaisePostDataChangedEvent() is never invoked. In the
    > MyUserControl.ascx.cs code snippet, I hardcoded a return of 'true'.
    > Even then, RaisePostDataChangedEvent() is never invoked. This is the
    > main problem that I am experiencing.
    >
    > Any insights ?
    >
    > Thanks
    >
    >
    >
    > Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    >> Hi,
    >>
    >> The only missing thing that will need to fill, as I think, is a simple
    >> hidden field... so that it can be captured in LoadPostData.
    >>
    >> btw, what happens if you blindly return 'true' in LoadPostData?
    >>
    >>
    >> --
    >> Happy Hacking,
    >> Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    >> [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    >> -------------------
    >>
    >>
    >> <buzlite@sympatico.ca> wrote in message
    >> news:1155567351.199524.188830@m79g2000cwm.googlegr oups.com...
    >> > Hello All,
    >> > Any help would be appreciated.
    >> > Thanks
    >> >
    >> >
    >> >
    >> > [email]buzlite@sympatico.ca[/email] wrote:
    >> >> Hello All,
    >> >>
    >> >>
    >> >> I've posted this in
    >> >> microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got not
    >> >> reply so I'll try my luck here.
    >> >>
    >> >> Any help would be greatly appreciated.
    >> >> Thanks
    >> >>
    >> >>
    >> >>
    >> >> 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;
    >> >> }
    >> >> }
    >> >
    >

    Gaurav Vaish \(www.EduJini.IN\) Guest

  9. #8

    Default Re: IPostBackDataHandler not working properly with DataGrid

    Problem solved.
    btw, the hidden field would be required because of the following reason:

    For each ClientID that appears in the form and the control corresponding to
    the same, the IPostBackDataHandler methods would be called.
    And for no else.

    Here's a trick:

    Add a hidden field during 'Render' to the output.

    You may download the code (that I wrote) from the following URL:
    [url]http://www.edujini.in/samples/ASP.Net/2.0/DataHandlerControl.zip[/url]


    --
    Happy Hacking,
    Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    -------------------


    "Gaurav Vaish (www.EduJini.IN)" <gaurav.vaish.nospam@nospam.gmail.com> wrote
    in message news:OOulE4BwGHA.1288@TK2MSFTNGP02.phx.gbl...
    > Let me give it a shot by creating a dummy 'UserControl' on my own.
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    > [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    > -------------------
    >
    >
    > <buzlite@sympatico.ca> wrote in message
    > news:1155591996.737095.53890@b28g2000cwb.googlegro ups.com...
    >> Hello Gaurav,
    >>
    >> I do not know the reasoning for adding a hidden field and what purpose
    >> that would accomplish.
    >>
    >>>From the sample code attachments, when WebForm1.aspx is run and the
    >> Datagrid generates MyUserControl user control, any text that is entered
    >> in this user control TextBox would be properly posted back to the
    >> server. This is true in that when MyUserControl.ascx.cs,
    >> LoadPostData() is called, the data can be retrieved from the input
    >> parameter of this method. So...this is working OK.
    >>
    >> The problem is, when MyUserControl.ascx.cs, LoadPostData() returns a
    >> value of 'true', RaisePostDataChangedEvent() is never invoked. In the
    >> MyUserControl.ascx.cs code snippet, I hardcoded a return of 'true'.
    >> Even then, RaisePostDataChangedEvent() is never invoked. This is the
    >> main problem that I am experiencing.
    >>
    >> Any insights ?
    >>
    >> Thanks
    >>
    >>
    >>
    >> Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    >>> Hi,
    >>>
    >>> The only missing thing that will need to fill, as I think, is a simple
    >>> hidden field... so that it can be captured in LoadPostData.
    >>>
    >>> btw, what happens if you blindly return 'true' in LoadPostData?
    >>>
    >>>
    >>> --
    >>> Happy Hacking,
    >>> Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    >>> [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    >>> -------------------
    >>>
    >>>
    >>> <buzlite@sympatico.ca> wrote in message
    >>> news:1155567351.199524.188830@m79g2000cwm.googlegr oups.com...
    >>> > Hello All,
    >>> > Any help would be appreciated.
    >>> > Thanks
    >>> >
    >>> >
    >>> >
    >>> > [email]buzlite@sympatico.ca[/email] wrote:
    >>> >> Hello All,
    >>> >>
    >>> >>
    >>> >> I've posted this in
    >>> >> microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got not
    >>> >> reply so I'll try my luck here.
    >>> >>
    >>> >> Any help would be greatly appreciated.
    >>> >> Thanks
    >>> >>
    >>> >>
    >>> >>
    >>> >> 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;
    >>> >> }
    >>> >> }
    >>> >
    >>
    >
    >

    Gaurav Vaish \(www.EduJini.IN\) Guest

  10. #9

    Default Re: IPostBackDataHandler not working properly with DataGrid

    Hello Gaurav,

    Thanks for you attempt but it was not answering the initial problem.

    The initial problem was that the MyUserControl is embedded in a
    Datagrid control. Please checkout the file WebForm1.aspx in my previous
    email.

    With the configuration as shown in WebForm1.aspx, the user control is
    embedded in a datagrid control. When this file is executed and text is
    entered into the user control textbox, LoadPostData() of the user
    control is invoked. But even though LoadPostData() returns 'true', the
    corresponding RaisePostDataChangedEvent() is not called. This is the
    problem I am trying to find a solution to.

    By the way, I am using Visual Studio.NET 2003 with .NET framework 1.1.
    I think you are using Visual Studio.NET 2005 with .NET framework 2.0.
    Nevertheless, I do not know if this would affect the result. I would
    be curious though.

    Thanks



    Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    > Problem solved.
    > btw, the hidden field would be required because of the following reason:
    >
    > For each ClientID that appears in the form and the control corresponding to
    > the same, the IPostBackDataHandler methods would be called.
    > And for no else.
    >
    > Here's a trick:
    >
    > Add a hidden field during 'Render' to the output.
    >
    > You may download the code (that I wrote) from the following URL:
    > [url]http://www.edujini.in/samples/ASP.Net/2.0/DataHandlerControl.zip[/url]
    >
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    > [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    > -------------------
    >
    >
    > "Gaurav Vaish (www.EduJini.IN)" <gaurav.vaish.nospam@nospam.gmail.com> wrote
    > in message news:OOulE4BwGHA.1288@TK2MSFTNGP02.phx.gbl...
    > > Let me give it a shot by creating a dummy 'UserControl' on my own.
    > >
    > > --
    > > Happy Hacking,
    > > Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    > > [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    > > -------------------
    > >
    > >
    > > <buzlite@sympatico.ca> wrote in message
    > > news:1155591996.737095.53890@b28g2000cwb.googlegro ups.com...
    > >> Hello Gaurav,
    > >>
    > >> I do not know the reasoning for adding a hidden field and what purpose
    > >> that would accomplish.
    > >>
    > >>>From the sample code attachments, when WebForm1.aspx is run and the
    > >> Datagrid generates MyUserControl user control, any text that is entered
    > >> in this user control TextBox would be properly posted back to the
    > >> server. This is true in that when MyUserControl.ascx.cs,
    > >> LoadPostData() is called, the data can be retrieved from the input
    > >> parameter of this method. So...this is working OK.
    > >>
    > >> The problem is, when MyUserControl.ascx.cs, LoadPostData() returns a
    > >> value of 'true', RaisePostDataChangedEvent() is never invoked. In the
    > >> MyUserControl.ascx.cs code snippet, I hardcoded a return of 'true'.
    > >> Even then, RaisePostDataChangedEvent() is never invoked. This is the
    > >> main problem that I am experiencing.
    > >>
    > >> Any insights ?
    > >>
    > >> Thanks
    > >>
    > >>
    > >>
    > >> Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    > >>> Hi,
    > >>>
    > >>> The only missing thing that will need to fill, as I think, is a simple
    > >>> hidden field... so that it can be captured in LoadPostData.
    > >>>
    > >>> btw, what happens if you blindly return 'true' in LoadPostData?
    > >>>
    > >>>
    > >>> --
    > >>> Happy Hacking,
    > >>> Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    > >>> [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    > >>> -------------------
    > >>>
    > >>>
    > >>> <buzlite@sympatico.ca> wrote in message
    > >>> news:1155567351.199524.188830@m79g2000cwm.googlegr oups.com...
    > >>> > Hello All,
    > >>> > Any help would be appreciated.
    > >>> > Thanks
    > >>> >
    > >>> >
    > >>> >
    > >>> > [email]buzlite@sympatico.ca[/email] wrote:
    > >>> >> Hello All,
    > >>> >>
    > >>> >>
    > >>> >> I've posted this in
    > >>> >> microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got not
    > >>> >> reply so I'll try my luck here.
    > >>> >>
    > >>> >> Any help would be greatly appreciated.
    > >>> >> Thanks
    > >>> >>
    > >>> >>
    > >>> >>
    > >>> >> 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;
    > >>> >> }
    > >>> >> }
    > >>> >
    > >>
    > >
    > >
    buzlite001@hotmail.com Guest

  11. #10

    Default Re: IPostBackDataHandler not working properly with DataGrid

    No problem.
    Embed the control in DataGrid.... should not be a problem.

    Did you try using my control in DataGrid?

    --
    Happy Hacking,
    Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    -------------------


    <buzlite001@hotmail.com> wrote in message
    news:1155671651.189969.144180@m79g2000cwm.googlegr oups.com...
    > Hello Gaurav,
    >
    > Thanks for you attempt but it was not answering the initial problem.
    >
    > The initial problem was that the MyUserControl is embedded in a
    > Datagrid control. Please checkout the file WebForm1.aspx in my previous
    > email.
    >
    > With the configuration as shown in WebForm1.aspx, the user control is
    > embedded in a datagrid control. When this file is executed and text is
    > entered into the user control textbox, LoadPostData() of the user
    > control is invoked. But even though LoadPostData() returns 'true', the
    > corresponding RaisePostDataChangedEvent() is not called. This is the
    > problem I am trying to find a solution to.
    >
    > By the way, I am using Visual Studio.NET 2003 with .NET framework 1.1.
    > I think you are using Visual Studio.NET 2005 with .NET framework 2.0.
    > Nevertheless, I do not know if this would affect the result. I would
    > be curious though.
    >
    > Thanks
    >
    >
    >
    > Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    >> Problem solved.
    >> btw, the hidden field would be required because of the following reason:
    >>
    >> For each ClientID that appears in the form and the control corresponding
    >> to
    >> the same, the IPostBackDataHandler methods would be called.
    >> And for no else.
    >>
    >> Here's a trick:
    >>
    >> Add a hidden field during 'Render' to the output.
    >>
    >> You may download the code (that I wrote) from the following URL:
    >> [url]http://www.edujini.in/samples/ASP.Net/2.0/DataHandlerControl.zip[/url]
    >>
    >>
    >> --
    >> Happy Hacking,
    >> Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    >> [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    >> -------------------
    >>
    >>
    >> "Gaurav Vaish (www.EduJini.IN)" <gaurav.vaish.nospam@nospam.gmail.com>
    >> wrote
    >> in message news:OOulE4BwGHA.1288@TK2MSFTNGP02.phx.gbl...
    >> > Let me give it a shot by creating a dummy 'UserControl' on my own.
    >> >
    >> > --
    >> > Happy Hacking,
    >> > Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    >> > [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    >> > -------------------
    >> >
    >> >
    >> > <buzlite@sympatico.ca> wrote in message
    >> > news:1155591996.737095.53890@b28g2000cwb.googlegro ups.com...
    >> >> Hello Gaurav,
    >> >>
    >> >> I do not know the reasoning for adding a hidden field and what purpose
    >> >> that would accomplish.
    >> >>
    >> >>>From the sample code attachments, when WebForm1.aspx is run and the
    >> >> Datagrid generates MyUserControl user control, any text that is
    >> >> entered
    >> >> in this user control TextBox would be properly posted back to the
    >> >> server. This is true in that when MyUserControl.ascx.cs,
    >> >> LoadPostData() is called, the data can be retrieved from the input
    >> >> parameter of this method. So...this is working OK.
    >> >>
    >> >> The problem is, when MyUserControl.ascx.cs, LoadPostData() returns a
    >> >> value of 'true', RaisePostDataChangedEvent() is never invoked. In the
    >> >> MyUserControl.ascx.cs code snippet, I hardcoded a return of 'true'.
    >> >> Even then, RaisePostDataChangedEvent() is never invoked. This is the
    >> >> main problem that I am experiencing.
    >> >>
    >> >> Any insights ?
    >> >>
    >> >> Thanks
    >> >>
    >> >>
    >> >>
    >> >> Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    >> >>> Hi,
    >> >>>
    >> >>> The only missing thing that will need to fill, as I think, is a
    >> >>> simple
    >> >>> hidden field... so that it can be captured in LoadPostData.
    >> >>>
    >> >>> btw, what happens if you blindly return 'true' in LoadPostData?
    >> >>>
    >> >>>
    >> >>> --
    >> >>> Happy Hacking,
    >> >>> Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    >> >>> [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    >> >>> -------------------
    >> >>>
    >> >>>
    >> >>> <buzlite@sympatico.ca> wrote in message
    >> >>> news:1155567351.199524.188830@m79g2000cwm.googlegr oups.com...
    >> >>> > Hello All,
    >> >>> > Any help would be appreciated.
    >> >>> > Thanks
    >> >>> >
    >> >>> >
    >> >>> >
    >> >>> > [email]buzlite@sympatico.ca[/email] wrote:
    >> >>> >> Hello All,
    >> >>> >>
    >> >>> >>
    >> >>> >> I've posted this in
    >> >>> >> microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got
    >> >>> >> not
    >> >>> >> reply so I'll try my luck here.
    >> >>> >>
    >> >>> >> Any help would be greatly appreciated.
    >> >>> >> Thanks
    >> >>> >>
    >> >>> >>
    >> >>> >>
    >> >>> >> 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;
    >> >>> >> }
    >> >>> >> }
    >> >>> >
    >> >>
    >> >
    >> >
    >

    Gaurav Vaish \(www.EduJini.IN\) Guest

  12. #11

    Default Re: IPostBackDataHandler not working properly with DataGrid

    Yes I have. I had to adapt your code to Visual Studio.NET 2003 though.
    It was exhibiting the same problem in that RaisePostDataChangedEvent()
    of the user control is not being called even when LoadPostData() of the
    user control returns 'true' back to the framework.

    Hope you have better luck.




    Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    > No problem.
    > Embed the control in DataGrid.... should not be a problem.
    >
    > Did you try using my control in DataGrid?
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    > [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    > -------------------
    >
    >
    > <buzlite001@hotmail.com> wrote in message
    > news:1155671651.189969.144180@m79g2000cwm.googlegr oups.com...
    > > Hello Gaurav,
    > >
    > > Thanks for you attempt but it was not answering the initial problem.
    > >
    > > The initial problem was that the MyUserControl is embedded in a
    > > Datagrid control. Please checkout the file WebForm1.aspx in my previous
    > > email.
    > >
    > > With the configuration as shown in WebForm1.aspx, the user control is
    > > embedded in a datagrid control. When this file is executed and text is
    > > entered into the user control textbox, LoadPostData() of the user
    > > control is invoked. But even though LoadPostData() returns 'true', the
    > > corresponding RaisePostDataChangedEvent() is not called. This is the
    > > problem I am trying to find a solution to.
    > >
    > > By the way, I am using Visual Studio.NET 2003 with .NET framework 1.1.
    > > I think you are using Visual Studio.NET 2005 with .NET framework 2.0.
    > > Nevertheless, I do not know if this would affect the result. I would
    > > be curious though.
    > >
    > > Thanks
    > >
    > >
    > >
    > > Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    > >> Problem solved.
    > >> btw, the hidden field would be required because of the following reason:
    > >>
    > >> For each ClientID that appears in the form and the control corresponding
    > >> to
    > >> the same, the IPostBackDataHandler methods would be called.
    > >> And for no else.
    > >>
    > >> Here's a trick:
    > >>
    > >> Add a hidden field during 'Render' to the output.
    > >>
    > >> You may download the code (that I wrote) from the following URL:
    > >> [url]http://www.edujini.in/samples/ASP.Net/2.0/DataHandlerControl.zip[/url]
    > >>
    > >>
    > >> --
    > >> Happy Hacking,
    > >> Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    > >> [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    > >> -------------------
    > >>
    > >>
    > >> "Gaurav Vaish (www.EduJini.IN)" <gaurav.vaish.nospam@nospam.gmail.com>
    > >> wrote
    > >> in message news:OOulE4BwGHA.1288@TK2MSFTNGP02.phx.gbl...
    > >> > Let me give it a shot by creating a dummy 'UserControl' on my own.
    > >> >
    > >> > --
    > >> > Happy Hacking,
    > >> > Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    > >> > [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    > >> > -------------------
    > >> >
    > >> >
    > >> > <buzlite@sympatico.ca> wrote in message
    > >> > news:1155591996.737095.53890@b28g2000cwb.googlegro ups.com...
    > >> >> Hello Gaurav,
    > >> >>
    > >> >> I do not know the reasoning for adding a hidden field and what purpose
    > >> >> that would accomplish.
    > >> >>
    > >> >>>From the sample code attachments, when WebForm1.aspx is run and the
    > >> >> Datagrid generates MyUserControl user control, any text that is
    > >> >> entered
    > >> >> in this user control TextBox would be properly posted back to the
    > >> >> server. This is true in that when MyUserControl.ascx.cs,
    > >> >> LoadPostData() is called, the data can be retrieved from the input
    > >> >> parameter of this method. So...this is working OK.
    > >> >>
    > >> >> The problem is, when MyUserControl.ascx.cs, LoadPostData() returns a
    > >> >> value of 'true', RaisePostDataChangedEvent() is never invoked. In the
    > >> >> MyUserControl.ascx.cs code snippet, I hardcoded a return of 'true'.
    > >> >> Even then, RaisePostDataChangedEvent() is never invoked. This is the
    > >> >> main problem that I am experiencing.
    > >> >>
    > >> >> Any insights ?
    > >> >>
    > >> >> Thanks
    > >> >>
    > >> >>
    > >> >>
    > >> >> Gaurav Vaish ([url]www.EduJini.IN[/url]) wrote:
    > >> >>> Hi,
    > >> >>>
    > >> >>> The only missing thing that will need to fill, as I think, is a
    > >> >>> simple
    > >> >>> hidden field... so that it can be captured in LoadPostData.
    > >> >>>
    > >> >>> btw, what happens if you blindly return 'true' in LoadPostData?
    > >> >>>
    > >> >>>
    > >> >>> --
    > >> >>> Happy Hacking,
    > >> >>> Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    > >> >>> [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    > >> >>> -------------------
    > >> >>>
    > >> >>>
    > >> >>> <buzlite@sympatico.ca> wrote in message
    > >> >>> news:1155567351.199524.188830@m79g2000cwm.googlegr oups.com...
    > >> >>> > Hello All,
    > >> >>> > Any help would be appreciated.
    > >> >>> > Thanks
    > >> >>> >
    > >> >>> >
    > >> >>> >
    > >> >>> > [email]buzlite@sympatico.ca[/email] wrote:
    > >> >>> >> Hello All,
    > >> >>> >>
    > >> >>> >>
    > >> >>> >> I've posted this in
    > >> >>> >> microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got
    > >> >>> >> not
    > >> >>> >> reply so I'll try my luck here.
    > >> >>> >>
    > >> >>> >> Any help would be greatly appreciated.
    > >> >>> >> Thanks
    > >> >>> >>
    > >> >>> >>
    > >> >>> >>
    > >> >>> >> 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;
    > >> >>> >> }
    > >> >>> >> }
    > >> >>> >
    > >> >>
    > >> >
    > >> >
    > >
    buzlite@sympatico.ca Guest

  13. #12

    Default Re: IPostBackDataHandler not working properly with DataGrid

    hi, the reason your postDataChanged event never makes it is because you have
    the user control nested in a datagrid. The datagrid control internally
    overrides the onBubbleEvent, and only raises command event, to which you can
    get to using the ItemCommand method exposed by the datagrid, giving you a
    central location to branch out. Only command events are raised and exposed
    via the ItemCommand method because command events can contain CommandName
    and CommandArgument properties which can futher be used to distinguish
    between who fired what event(stored by you in CommandName property), and
    also a chance to pass some custom data(via the commandArgument property) .

    In your case you want to fire a non command event, so
    1. Nothing will be fired in the DataGrids ItemCommand, and
    2. the event is supressed since onbubble event is overridden by the datagrid
    and no action is provided to handle any events raised other than command
    events.

    To work around, you can expose a textchanged event by your usercontrol
    class, and attach the event declaratively as the following code snippet,
    note OnTextChanged="WebUserControl11_TextChanged".

    Another mistake i have seen in your code is that you rebind your datagrid in
    page_load, whether its postback or not. If you do this, then the datagrid
    will rebind, rebuilding everything and you lose state. This also will
    execute before you user control gets a chance to inspect postdata changes.
    So only rebind the grid, if its not a postback scenario.

    Regards,
    Alessandro Zifiglio
    [url]http://www.AsyncUI.net[/url]


    <asp:DataGrid id="DataGrid1" runat="server" EnableViewState="True"
    AutoGenerateColumns="False">
    <Columns>
    <asp:TemplateColumn HeaderText="Template Column1">
    <ItemTemplate>
    <uc1:WebUserControl1 id="WebUserControl11"
    OnTextChanged="WebUserControl11_TextChanged"
    runat="server"></uc1:WebUserControl1>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>


    public void WebUserControl11_TextChanged(object sender, System.EventArgs e)
    {
    string text1 = (string)((WebUserControl1)sender).Text;
    Response.Write(text1);
    }


    In your user control class you would be doing this to expose the public
    event :
    ----------------------------------------------------------------------------------


    public class WebUserControl1 : System.Web.UI.UserControl,
    System.Web.UI.IPostBackDataHandler, System.Web.UI.INamingContainer
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;

    public event EventHandler TextChanged;
    public string Text
    {
    get
    {
    return this.TextBox1.Text;
    }

    set
    {
    this.TextBox1.Text = value;
    }
    }

    // return true only if the text has changed
    // otherwise false.
    public virtual bool LoadPostData(string postDataKey, NameValueCollection
    postCollection)
    {

    String presentValue = Text;
    String postedValue = postCollection[postDataKey];
    if (postedValue == null)
    postedValue = string.Empty;
    if (!presentValue.Equals(postedValue))
    {
    return true;
    }
    return false;
    }

    public virtual void RaisePostDataChangedEvent()
    {
    OnTextChanged(EventArgs.Empty);
    }

    protected virtual void OnTextChanged(EventArgs e)
    {
    if (TextChanged != null)
    TextChanged(this,e);
    }

    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);

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

    }


    <buzlite@sympatico.ca> ha scritto nel messaggio
    news:1155336658.977686.265490@p79g2000cwp.googlegr oups.com...
    > Hello All,
    >
    >
    > I've posted this in
    > microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got not
    > reply so I'll try my luck here.
    >
    > Any help would be greatly appreciated.
    > Thanks
    >
    >
    >
    > 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;
    > }
    > }
    >

    Alessandro Zifiglio Guest

  14. #13

    Default Re: IPostBackDataHandler not working properly with DataGrid

    Thanks for the help Alessandro. Your explanation was very insightful.
    Your code snippet worked like a charm.

    Gaurav, if you're reading this posting, thanks to you too for helping.

    Have a great day All.



    Alessandro Zifiglio wrote:
    > hi, the reason your postDataChanged event never makes it is because you have
    > the user control nested in a datagrid. The datagrid control internally
    > overrides the onBubbleEvent, and only raises command event, to which you can
    > get to using the ItemCommand method exposed by the datagrid, giving you a
    > central location to branch out. Only command events are raised and exposed
    > via the ItemCommand method because command events can contain CommandName
    > and CommandArgument properties which can futher be used to distinguish
    > between who fired what event(stored by you in CommandName property), and
    > also a chance to pass some custom data(via the commandArgument property) .
    >
    > In your case you want to fire a non command event, so
    > 1. Nothing will be fired in the DataGrids ItemCommand, and
    > 2. the event is supressed since onbubble event is overridden by the datagrid
    > and no action is provided to handle any events raised other than command
    > events.
    >
    > To work around, you can expose a textchanged event by your usercontrol
    > class, and attach the event declaratively as the following code snippet,
    > note OnTextChanged="WebUserControl11_TextChanged".
    >
    > Another mistake i have seen in your code is that you rebind your datagrid in
    > page_load, whether its postback or not. If you do this, then the datagrid
    > will rebind, rebuilding everything and you lose state. This also will
    > execute before you user control gets a chance to inspect postdata changes.
    > So only rebind the grid, if its not a postback scenario.
    >
    > Regards,
    > Alessandro Zifiglio
    > [url]http://www.AsyncUI.net[/url]
    >
    >
    > <asp:DataGrid id="DataGrid1" runat="server" EnableViewState="True"
    > AutoGenerateColumns="False">
    > <Columns>
    > <asp:TemplateColumn HeaderText="Template Column1">
    > <ItemTemplate>
    > <uc1:WebUserControl1 id="WebUserControl11"
    > OnTextChanged="WebUserControl11_TextChanged"
    > runat="server"></uc1:WebUserControl1>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:DataGrid>
    >
    >
    > public void WebUserControl11_TextChanged(object sender, System.EventArgs e)
    > {
    > string text1 = (string)((WebUserControl1)sender).Text;
    > Response.Write(text1);
    > }
    >
    >
    > In your user control class you would be doing this to expose the public
    > event :
    > ----------------------------------------------------------------------------------
    >
    >
    > public class WebUserControl1 : System.Web.UI.UserControl,
    > System.Web.UI.IPostBackDataHandler, System.Web.UI.INamingContainer
    > {
    > protected System.Web.UI.WebControls.TextBox TextBox1;
    >
    > public event EventHandler TextChanged;
    > public string Text
    > {
    > get
    > {
    > return this.TextBox1.Text;
    > }
    >
    > set
    > {
    > this.TextBox1.Text = value;
    > }
    > }
    >
    > // return true only if the text has changed
    > // otherwise false.
    > public virtual bool LoadPostData(string postDataKey, NameValueCollection
    > postCollection)
    > {
    >
    > String presentValue = Text;
    > String postedValue = postCollection[postDataKey];
    > if (postedValue == null)
    > postedValue = string.Empty;
    > if (!presentValue.Equals(postedValue))
    > {
    > return true;
    > }
    > return false;
    > }
    >
    > public virtual void RaisePostDataChangedEvent()
    > {
    > OnTextChanged(EventArgs.Empty);
    > }
    >
    > protected virtual void OnTextChanged(EventArgs e)
    > {
    > if (TextChanged != null)
    > TextChanged(this,e);
    > }
    >
    > override protected void OnInit(EventArgs e)
    > {
    > InitializeComponent();
    > base.OnInit(e);
    >
    > if (Page != null)
    > {
    > Page.RegisterRequiresPostBack(this);
    > }
    >
    > }
    >
    >
    > <buzlite@sympatico.ca> ha scritto nel messaggio
    > news:1155336658.977686.265490@p79g2000cwp.googlegr oups.com...
    > > Hello All,
    > >
    > >
    > > I've posted this in
    > > microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got not
    > > reply so I'll try my luck here.
    > >
    > > Any help would be greatly appreciated.
    > > Thanks
    > >
    > >
    > >
    > > 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;
    > > }
    > > }
    > >
    buzlite@sympatico.ca Guest

  15. #14

    Default Re: IPostBackDataHandler not working properly with DataGrid

    your welcome.

    Regards,
    Alessandro Zifiglio
    [url]http://www.AsyncUI.net[/url]

    <buzlite@sympatico.ca> ha scritto nel messaggio
    news:1155919853.934639.117020@i3g2000cwc.googlegro ups.com...
    > Thanks for the help Alessandro. Your explanation was very insightful.
    > Your code snippet worked like a charm.
    >
    > Gaurav, if you're reading this posting, thanks to you too for helping.
    >
    > Have a great day All.
    >
    >
    >
    > Alessandro Zifiglio wrote:
    >> hi, the reason your postDataChanged event never makes it is because you
    >> have
    >> the user control nested in a datagrid. The datagrid control internally
    >> overrides the onBubbleEvent, and only raises command event, to which you
    >> can
    >> get to using the ItemCommand method exposed by the datagrid, giving you a
    >> central location to branch out. Only command events are raised and
    >> exposed
    >> via the ItemCommand method because command events can contain CommandName
    >> and CommandArgument properties which can futher be used to distinguish
    >> between who fired what event(stored by you in CommandName property), and
    >> also a chance to pass some custom data(via the commandArgument property)
    >> .
    >>
    >> In your case you want to fire a non command event, so
    >> 1. Nothing will be fired in the DataGrids ItemCommand, and
    >> 2. the event is supressed since onbubble event is overridden by the
    >> datagrid
    >> and no action is provided to handle any events raised other than command
    >> events.
    >>
    >> To work around, you can expose a textchanged event by your usercontrol
    >> class, and attach the event declaratively as the following code snippet,
    >> note OnTextChanged="WebUserControl11_TextChanged".
    >>
    >> Another mistake i have seen in your code is that you rebind your datagrid
    >> in
    >> page_load, whether its postback or not. If you do this, then the datagrid
    >> will rebind, rebuilding everything and you lose state. This also will
    >> execute before you user control gets a chance to inspect postdata
    >> changes.
    >> So only rebind the grid, if its not a postback scenario.
    >>
    >> Regards,
    >> Alessandro Zifiglio
    >> [url]http://www.AsyncUI.net[/url]
    >>
    >>
    >> <asp:DataGrid id="DataGrid1" runat="server" EnableViewState="True"
    >> AutoGenerateColumns="False">
    >> <Columns>
    >> <asp:TemplateColumn HeaderText="Template Column1">
    >> <ItemTemplate>
    >> <uc1:WebUserControl1 id="WebUserControl11"
    >> OnTextChanged="WebUserControl11_TextChanged"
    >> runat="server"></uc1:WebUserControl1>
    >> </ItemTemplate>
    >> </asp:TemplateColumn>
    >> </Columns>
    >> </asp:DataGrid>
    >>
    >>
    >> public void WebUserControl11_TextChanged(object sender, System.EventArgs
    >> e)
    >> {
    >> string text1 = (string)((WebUserControl1)sender).Text;
    >> Response.Write(text1);
    >> }
    >>
    >>
    >> In your user control class you would be doing this to expose the public
    >> event :
    >> ----------------------------------------------------------------------------------
    >>
    >>
    >> public class WebUserControl1 : System.Web.UI.UserControl,
    >> System.Web.UI.IPostBackDataHandler, System.Web.UI.INamingContainer
    >> {
    >> protected System.Web.UI.WebControls.TextBox TextBox1;
    >>
    >> public event EventHandler TextChanged;
    >> public string Text
    >> {
    >> get
    >> {
    >> return this.TextBox1.Text;
    >> }
    >>
    >> set
    >> {
    >> this.TextBox1.Text = value;
    >> }
    >> }
    >>
    >> // return true only if the text has changed
    >> // otherwise false.
    >> public virtual bool LoadPostData(string postDataKey,
    >> NameValueCollection
    >> postCollection)
    >> {
    >>
    >> String presentValue = Text;
    >> String postedValue = postCollection[postDataKey];
    >> if (postedValue == null)
    >> postedValue = string.Empty;
    >> if (!presentValue.Equals(postedValue))
    >> {
    >> return true;
    >> }
    >> return false;
    >> }
    >>
    >> public virtual void RaisePostDataChangedEvent()
    >> {
    >> OnTextChanged(EventArgs.Empty);
    >> }
    >>
    >> protected virtual void OnTextChanged(EventArgs e)
    >> {
    >> if (TextChanged != null)
    >> TextChanged(this,e);
    >> }
    >>
    >> override protected void OnInit(EventArgs e)
    >> {
    >> InitializeComponent();
    >> base.OnInit(e);
    >>
    >> if (Page != null)
    >> {
    >> Page.RegisterRequiresPostBack(this);
    >> }
    >>
    >> }
    >>
    >>
    >> <buzlite@sympatico.ca> ha scritto nel messaggio
    >> news:1155336658.977686.265490@p79g2000cwp.googlegr oups.com...
    >> > Hello All,
    >> >
    >> >
    >> > I've posted this in
    >> > microsoft.public.dotnet.framework.aspnet.datagridc ontrol but got not
    >> > reply so I'll try my luck here.
    >> >
    >> > Any help would be greatly appreciated.
    >> > Thanks
    >> >
    >> >
    >> >
    >> > 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;
    >> > }
    >> > }
    >> >
    >

    Alessandro Zifiglio Guest

  16. #15

    Default Re: IPostBackDataHandler not working properly with DataGrid

    > Gaurav, if you're reading this posting, thanks to you too for helping.

    Always welcome... :-)


    --
    Happy Hacking,
    Gaurav Vaish | [url]http://www.mastergaurav.org[/url]
    [url]http://www.edujini.in[/url] | [url]http://webservices.edujini.in[/url]
    -------------------


    Gaurav Vaish \(www.EduJini.IN\) Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139