Problem with custom web control imagebutton event and textbox viewstate

K

Koen

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

Teemu Keiski

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top