J
Jowita
I'm using VS 2005.
I created a sample composite control with a textbox and a button. I'm
having problems getting the button event.
The control implements IPostBackEventHandler. Is it necessary if I
just want to handle the event internally in the control?
I added this code to CreateChildControls:
protected override void CreateChildControls()
{
Table t = new Table();
t.Rows.Add(new TableRow());
t.Rows[0].Cells.Add(new TableCell());
titleLabel = new Label();
titleLabel.Text = "Advanced Search";
titleLabel.ForeColor = this.ForeColor;
t.Rows[0].Cells[0].Controls.Add(titleLabel);
Literal lit = new Literal();
lit.Text = "<br/><br/>";
t.Rows[0].Cells[0].Controls.Add(lit);
queryLabel = new Label();
queryLabel.Text = "Keyword:";
queryLabel.ForeColor = this.ForeColor;
t.Rows[0].Cells[0].Controls.Add(queryLabel);
Literal lit2 = new Literal();
lit2.Text = " ";
t.Rows[0].Cells[0].Controls.Add(lit2);
queryTextBox = new TextBox();
t.Rows[0].Cells[0].Controls.Add(queryTextBox);
// This is when I thought I hooked up the event handler
function to the button's Click event
submitButton = new Button();
submitButton.Text = "Submit";
submitButton.Click += new
EventHandler(this.submitButton_Click);
t.Rows[0].Cells[0].Controls.Add(submitButton);
Literal lit3 = new Literal();
lit3.Text = "<br/><br/>";
t.Rows[0].Cells[0].Controls.Add(lit3);
nameLabel = new Label();
t.Rows[0].Cells[0].Controls.Add(nameLabel);
this.Controls.Add(t);
base.CreateChildControls();
}
So I thought the event hook is at the following lines:
submitButton = new Button();
submitButton.Text = "Submit";
submitButton.Click += new
EventHandler(this.submitButton_Click);
t.Rows[0].Cells[0].Controls.Add(submitButton);
The event handler looks like this:
private void submitButton_Click(object source, EventArgs e)
{
this.NameLabelText = "submitted!";
OnChange(EventArgs.Empty);
}
I also implemented the following just in case:
public event EventHandler Change;
protected void OnChange(EventArgs e)
{
if (Change != null)
{
Change(this, e);
}
}
// Define the method of IPostBackEventHandler that raises
change events.
public void RaisePostBackEvent(string eventArgument)
{
OnChange(new EventArgs());
}
NameLabelText is a property as follows:
[
Bindable(true),
Category("Appearance"),
DefaultValue("Enter name:"),
Description("The text for the name label.")
]
public string NameLabelText
{
get
{
EnsureChildControls();
return nameLabel.Text;
}
set
{
EnsureChildControls();
nameLabel.Text = value;
}
}
When I click on the button, the nameLabel control doesn't change the
text as I would expect.
Anyone knows what I'm missing?
Thanks
J
I created a sample composite control with a textbox and a button. I'm
having problems getting the button event.
The control implements IPostBackEventHandler. Is it necessary if I
just want to handle the event internally in the control?
I added this code to CreateChildControls:
protected override void CreateChildControls()
{
Table t = new Table();
t.Rows.Add(new TableRow());
t.Rows[0].Cells.Add(new TableCell());
titleLabel = new Label();
titleLabel.Text = "Advanced Search";
titleLabel.ForeColor = this.ForeColor;
t.Rows[0].Cells[0].Controls.Add(titleLabel);
Literal lit = new Literal();
lit.Text = "<br/><br/>";
t.Rows[0].Cells[0].Controls.Add(lit);
queryLabel = new Label();
queryLabel.Text = "Keyword:";
queryLabel.ForeColor = this.ForeColor;
t.Rows[0].Cells[0].Controls.Add(queryLabel);
Literal lit2 = new Literal();
lit2.Text = " ";
t.Rows[0].Cells[0].Controls.Add(lit2);
queryTextBox = new TextBox();
t.Rows[0].Cells[0].Controls.Add(queryTextBox);
// This is when I thought I hooked up the event handler
function to the button's Click event
submitButton = new Button();
submitButton.Text = "Submit";
submitButton.Click += new
EventHandler(this.submitButton_Click);
t.Rows[0].Cells[0].Controls.Add(submitButton);
Literal lit3 = new Literal();
lit3.Text = "<br/><br/>";
t.Rows[0].Cells[0].Controls.Add(lit3);
nameLabel = new Label();
t.Rows[0].Cells[0].Controls.Add(nameLabel);
this.Controls.Add(t);
base.CreateChildControls();
}
So I thought the event hook is at the following lines:
submitButton = new Button();
submitButton.Text = "Submit";
submitButton.Click += new
EventHandler(this.submitButton_Click);
t.Rows[0].Cells[0].Controls.Add(submitButton);
The event handler looks like this:
private void submitButton_Click(object source, EventArgs e)
{
this.NameLabelText = "submitted!";
OnChange(EventArgs.Empty);
}
I also implemented the following just in case:
public event EventHandler Change;
protected void OnChange(EventArgs e)
{
if (Change != null)
{
Change(this, e);
}
}
// Define the method of IPostBackEventHandler that raises
change events.
public void RaisePostBackEvent(string eventArgument)
{
OnChange(new EventArgs());
}
NameLabelText is a property as follows:
[
Bindable(true),
Category("Appearance"),
DefaultValue("Enter name:"),
Description("The text for the name label.")
]
public string NameLabelText
{
get
{
EnsureChildControls();
return nameLabel.Text;
}
set
{
EnsureChildControls();
nameLabel.Text = value;
}
}
When I click on the button, the nameLabel control doesn't change the
text as I would expect.
Anyone knows what I'm missing?
Thanks
J