Dynamic Field

  • Thread starter Thread starter Eduardo Rosa
  • Start date Start date
E

Eduardo Rosa

Hi, I need some help...
my function create dynamic fields, and a button; in a Click event I need to
access those fields but occurs a exeption:

"CS0117: 'ASP.Forum' does not contain a definition for txtTTitulo"

thanks a lot

code:

Object createAndShowNewTopicForm(){
System.Web.UI.WebControls.TextBox txtTTitulo = new
System.Web.UI.WebControls.TextBox();
System.Web.UI.WebControls.Button btnEnviar = new
System.Web.UI.WebControls.Button();

txtTTitulo.ID = "txtTTitulo";
txtTTitulo.CssClass = "novotopico-textbox-titulo";

btnEnviar.ID = "btnEnviar";
btnEnviar.Text = "Enviar";
btnEnviar.Click += new EventHandler(this.SubmitButton_Click);
btnEnviar.CausesValidation = false;
btnEnviar.CssClass = "novotopico-botao";

return (Object)txtTTitulo;
}

void SubmitButton_Click(Object sender, EventArgs e){
switch(((Button)sender).ID){
case "btnEnviar":
ValidateForm();
break;
}

}

int ValidateForm(){
this.txtTTitulo.Text ="hey"; //erro in this line

return (0);
}
 
just think about how can you make this true, if you are "csc.exe":
this.txtTTitulo.Text ="hey";

you think you have txtTTitulo, but it's dynamically created, at least in
your program code scope, csc.exe cannot find txtTTitulo.

you should use FindControl("txtTTitulo") to get the control,

if not null, then set it's value.
 
Very tanks Edward!

Edward said:
just think about how can you make this true, if you are "csc.exe":
this.txtTTitulo.Text ="hey";

you think you have txtTTitulo, but it's dynamically created, at least in
your program code scope, csc.exe cannot find txtTTitulo.

you should use FindControl("txtTTitulo") to get the control,

if not null, then set it's value.
 
Back
Top