What's wrong with this custom control?

S

Sameeksha

I've created the following control and placed it on a web page. When I click
on 'Add' button, it should add a new item to the dropdown list, but that's
not happening. Can anybody tell what's wrong with this code?

public class TryControl : System.Web.UI.WebControls.WebControl
{
protected DropDownList lst;
protected Button btn;

protected override void CreateChildControls()
{
base.CreateChildControls ();
lst = new DropDownList();
Controls.Add(lst);
if (!Page.IsPostBack)
{
lst.Items.Add("One");
lst.Items.Add("Two");
lst.Items.Add("Three");
}
btn = new Button();
Controls.Add(btn);
btn.Text = "Add";
btn.Click += new EventHandler(btn_Click);
}

private void btn_Click(object sender, EventArgs e)
{
lst.Items.Add("Next");
}
}
 
N

Nick Stansbury

First thoughts would be that the class isn't implementing iNamingContainer
and the button is not declared WithEvents.But I stand ready to be corrected
by someone much more knowledgable.

Try something like this (excuse the vb):

public class TryControl
inherits System.Web.UI.WebControls.WebControl
implements iNamingContainer

protected withevents lst as DropDownList
protected withevents btn as Button

protected overrides sub CreateChildControls()
MyBase.CreateControls()
lst = new DropDownList()
lst.Id = "MyDropDownList" 'set ths ID as the only property before adding
to the controls collection - can someone confirm whether this is neccesary?
Me.Controls.Add(lst)
if not Page.IsPostback then
lst.items.add("One")
lst.Items.Add("Two")
lst.Items.Add("Three")
end if
btn = new Button()
btn.id = "MyButton"
Controls.Add(btn) 'now the button is tracking viewstate
btn.Text = "Add"
end sub

private sub btn_Click(object sender, EventArgs e) handles btn.Click
lst.Items.Add("Next")
end sub
end class
 
S

Sameeksha

Thanks Nick
Adding INamingContainer did the job. WithEvents is not necessary in C#

Nick Stansbury said:
First thoughts would be that the class isn't implementing iNamingContainer
and the button is not declared WithEvents.But I stand ready to be corrected
by someone much more knowledgable.

Try something like this (excuse the vb):

public class TryControl
inherits System.Web.UI.WebControls.WebControl
implements iNamingContainer

protected withevents lst as DropDownList
protected withevents btn as Button

protected overrides sub CreateChildControls()
MyBase.CreateControls()
lst = new DropDownList()
lst.Id = "MyDropDownList" 'set ths ID as the only property before adding
to the controls collection - can someone confirm whether this is neccesary?
Me.Controls.Add(lst)
if not Page.IsPostback then
lst.items.add("One")
lst.Items.Add("Two")
lst.Items.Add("Three")
end if
btn = new Button()
btn.id = "MyButton"
Controls.Add(btn) 'now the button is tracking viewstate
btn.Text = "Add"
end sub

private sub btn_Click(object sender, EventArgs e) handles btn.Click
lst.Items.Add("Next")
end sub
end class
 

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,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top