Dynamic Field

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

Edward

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.
 
E

Eduardo Rosa

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.
 

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

Top