Problem with custom web control imagebutton event and textbox viewstate

Discussion in 'ASP .Net Web Controls' started by Koen, May 17, 2007.

  1. Koen

    Koen Guest

    Hi,

    I'm struggling with a web custom control, i've searched on the forums
    and tried a lot several stuff but without any luck.

    I have a webcontrol with a gridview and above a search textbox and
    imagebutton.
    The gridview keeps its viewstate, but the textbox does not.
    Also the search imagebutton Click event does not fire correctly. The
    gridview pagechangedevent fires without any problem.

    Does anyone have any idea why there are these differences?

    I tried to inherit from CompositeControl instead of WebControl, and
    added the RecreateChildControls method but that didn't fire either :-(

    Any help would be great!


    Kind regards,

    Koen



    public class ccCustomersList : WebControl, INamingContainer
    {
    private bool _ControlFilled = false;

    Label lblTitle = null;
    Label lblRowsCount = null;
    Label lblNrOfRows = null;
    Label lblSearch = null;
    ImageButton ibSearch = null;
    TextBox tbSearch = null;
    HyperLink hlNew = null;

    private GridView gvList = null;

    public ccCustomersList()
    {
    this.EnsureChildControls();
    }

    protected override void OnInit(EventArgs e)
    {
    this.EnsureChildControls();
    base.OnInit(e);
    }
    protected override void OnLoad(EventArgs e)
    {
    if (!Page.IsPostBack && !_ControlFilled)
    {
    FillControl();
    }
    base.OnLoad(e);
    }
    protected override void CreateChildControls()
    {
    if (!ChildControlsCreated)
    {
    base.CreateChildControls();

    lblTitle = new Label();
    lblTitle.ID = "lblTitle";
    lblTitle.Text = ResCustomersList.Title;
    lblTitle.SkinID = "title";
    lblTitle.EnableViewState = false;
    Controls.Add(lblTitle);

    lblNrOfRows = new Label();
    lblNrOfRows.ID = "lblNrOfRows";
    lblNrOfRows.Text = ResCustomersList.RowsCount;
    lblNrOfRows.EnableViewState = false;
    Controls.Add(lblNrOfRows);

    lblRowsCount = new Label();
    lblRowsCount.ID = "lblRowsCount";
    lblRowsCount.EnableViewState = false;
    Controls.Add(lblRowsCount);

    hlNew = new HyperLink();
    hlNew.ID = "hlNew";
    hlNew.NavigateUrl = "~/CustomerEdit.aspx";
    hlNew.Text = ResCustomersList.New;
    hlNew.EnableViewState = false;
    Controls.Add(hlNew);

    lblSearch = new Label();
    lblSearch.ID = "lblSearch";
    lblSearch.Text = ResCustomersList.Search;

    tbSearch = new TextBox();
    tbSearch.ID = "tbSearch";
    tbSearch.EnableViewState = true;

    ibSearch = new ImageButton();
    ibSearch.CommandName = "search";
    ibSearch.CommandArgument = "search";
    ibSearch.ID = "ibSearch";
    ibSearch.Click += new
    ImageClickEventHandler(ibSearch_Click);
    ibSearch.ImageUrl = "~/images/search.png";

    if (gvList == null)
    {
    CreateGrid();
    }

    ChildControlsCreated = true;
    }
    }
    // does not fire!!!
    protected void ibSearch_Click(object sender,
    ImageClickEventArgs e)
    {
    string search = tbSearch.Text.Trim();
    if (search == string.Empty)
    FillControl();
    else
    FillControl(search);
    }

    void gvList_PageIndexChanging(object sender,
    GridViewPageEventArgs e)
    {
    if (gvList == null)
    {
    CreateGrid();
    }

    gvList.PageIndex = e.NewPageIndex;
    if (SearchFilter == string.Empty)
    FillControl();
    else
    FillControl(SearchFilter);
    }
    }
    Koen, May 17, 2007
    #1
    1. Advertising

  2. Koen

    Teemu Keiski Guest

    Where are the lines where you add the TextBox and ImageButton to Controls
    collection? E.g

    Controls.Add(tbSearch);
    //...
    Controls.Add(ibSearch);

    In order to raise events or load postback data they must exist in Controls
    collection.


    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net


    "Koen" <> wrote in message
    news:...
    > Hi,
    >
    > I'm struggling with a web custom control, i've searched on the forums
    > and tried a lot several stuff but without any luck.
    >
    > I have a webcontrol with a gridview and above a search textbox and
    > imagebutton.
    > The gridview keeps its viewstate, but the textbox does not.
    > Also the search imagebutton Click event does not fire correctly. The
    > gridview pagechangedevent fires without any problem.
    >
    > Does anyone have any idea why there are these differences?
    >
    > I tried to inherit from CompositeControl instead of WebControl, and
    > added the RecreateChildControls method but that didn't fire either :-(
    >
    > Any help would be great!
    >
    >
    > Kind regards,
    >
    > Koen
    >
    >
    >
    > public class ccCustomersList : WebControl, INamingContainer
    > {
    > private bool _ControlFilled = false;
    >
    > Label lblTitle = null;
    > Label lblRowsCount = null;
    > Label lblNrOfRows = null;
    > Label lblSearch = null;
    > ImageButton ibSearch = null;
    > TextBox tbSearch = null;
    > HyperLink hlNew = null;
    >
    > private GridView gvList = null;
    >
    > public ccCustomersList()
    > {
    > this.EnsureChildControls();
    > }
    >
    > protected override void OnInit(EventArgs e)
    > {
    > this.EnsureChildControls();
    > base.OnInit(e);
    > }
    > protected override void OnLoad(EventArgs e)
    > {
    > if (!Page.IsPostBack && !_ControlFilled)
    > {
    > FillControl();
    > }
    > base.OnLoad(e);
    > }
    > protected override void CreateChildControls()
    > {
    > if (!ChildControlsCreated)
    > {
    > base.CreateChildControls();
    >
    > lblTitle = new Label();
    > lblTitle.ID = "lblTitle";
    > lblTitle.Text = ResCustomersList.Title;
    > lblTitle.SkinID = "title";
    > lblTitle.EnableViewState = false;
    > Controls.Add(lblTitle);
    >
    > lblNrOfRows = new Label();
    > lblNrOfRows.ID = "lblNrOfRows";
    > lblNrOfRows.Text = ResCustomersList.RowsCount;
    > lblNrOfRows.EnableViewState = false;
    > Controls.Add(lblNrOfRows);
    >
    > lblRowsCount = new Label();
    > lblRowsCount.ID = "lblRowsCount";
    > lblRowsCount.EnableViewState = false;
    > Controls.Add(lblRowsCount);
    >
    > hlNew = new HyperLink();
    > hlNew.ID = "hlNew";
    > hlNew.NavigateUrl = "~/CustomerEdit.aspx";
    > hlNew.Text = ResCustomersList.New;
    > hlNew.EnableViewState = false;
    > Controls.Add(hlNew);
    >
    > lblSearch = new Label();
    > lblSearch.ID = "lblSearch";
    > lblSearch.Text = ResCustomersList.Search;
    >
    > tbSearch = new TextBox();
    > tbSearch.ID = "tbSearch";
    > tbSearch.EnableViewState = true;
    >
    > ibSearch = new ImageButton();
    > ibSearch.CommandName = "search";
    > ibSearch.CommandArgument = "search";
    > ibSearch.ID = "ibSearch";
    > ibSearch.Click += new
    > ImageClickEventHandler(ibSearch_Click);
    > ibSearch.ImageUrl = "~/images/search.png";
    >
    > if (gvList == null)
    > {
    > CreateGrid();
    > }
    >
    > ChildControlsCreated = true;
    > }
    > }
    > // does not fire!!!
    > protected void ibSearch_Click(object sender,
    > ImageClickEventArgs e)
    > {
    > string search = tbSearch.Text.Trim();
    > if (search == string.Empty)
    > FillControl();
    > else
    > FillControl(search);
    > }
    >
    > void gvList_PageIndexChanging(object sender,
    > GridViewPageEventArgs e)
    > {
    > if (gvList == null)
    > {
    > CreateGrid();
    > }
    >
    > gvList.PageIndex = e.NewPageIndex;
    > if (SearchFilter == string.Empty)
    > FillControl();
    > else
    > FillControl(SearchFilter);
    > }
    > }
    >
    Teemu Keiski, May 17, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gill Smith
    Replies:
    1
    Views:
    650
    Teemu Keiski
    May 21, 2004
  2. Alex D.
    Replies:
    5
    Views:
    4,527
    Alejandro Penate-Diaz
    Jun 1, 2005
  3. =?Utf-8?B?amVzc2UgamFtZXMgMjAyMA==?=

    Custom Web Control - viewstate and event wiring questions

    =?Utf-8?B?amVzc2UgamFtZXMgMjAyMA==?=, Aug 18, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    406
    =?Utf-8?B?amVzc2UgamFtZXMgMjAyMA==?=
    Aug 18, 2006
  4. Replies:
    4
    Views:
    520
  5. vtxr1300

    getting value of textbox in control after save viewstate event

    vtxr1300, Nov 7, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    192
    vtxr1300
    Nov 7, 2005
Loading...

Share This Page