2012-06-05 3 views
0

J'ai quelques tableaux pour enregistrer mes contrôles. quand appeler une fonction initialise des tableaux et enregistre dans mes tableaux mes contrôles.obtenir la valeur de la zone de texte dynamique

code:

private Label[] lblName; 
    private TextBox[] txtName; 
    private Label[] lblSurname; 
    private TextBox[] txtSurname; 
    private Label[] lblInstitution; 
    private TextBox[] txtInstitution; 
    private Label[] lblCountry; 
    private TextBox[] txtCountry; 
    private Label[] lblEmail; 
    private TextBox[] txtEmail; 
    private PlaceHolder PlaceHolder1; 


    public int NumberOfOtherAuthors() 
    { 
     Int32 index = Convert.ToInt32(NumberList.SelectedValue); 
     return index; 
    } 

    public void GUIofOtherAuthor() 
    { 
     int authors; 
     int i = 0; 
     int j = 1; 


     authors = NumberOfOtherAuthors(); 
     lblName = new Label[authors]; 
     txtName = new TextBox[authors]; 
     lblSurname = new Label[authors]; 
     txtSurname = new TextBox[authors]; 
     lblInstitution = new Label[authors]; 
     txtInstitution = new TextBox[authors]; 
     lblCountry = new Label[authors]; 
     txtCountry = new TextBox[authors]; 
     lblEmail = new Label[authors]; 
     txtEmail = new TextBox[authors]; 
     PlaceHolder1 = new PlaceHolder(); 


     for (i = 0; i < authors; i++) 
     { 
      Label authorInformation = new Label(); 
      authorInformation.Text = "Information for Author " + j.ToString() + " :"; 

      lblName[i] = new Label(); 
      lblName[i].Text = "Name:"; 
      txtName[i] = new TextBox(); 
      lblSurname[i] = new Label(); 
      lblSurname[i].Text = "Surname:"; 
      txtSurname[i] = new TextBox(); 
      lblInstitution[i] = new Label(); 
      lblInstitution[i].Text = "Institution:"; 
      txtInstitution[i] = new TextBox(); 
      lblCountry[i] = new Label(); 
      lblCountry[i].Text = "Country:"; 
      txtCountry[i] = new TextBox(); 
      lblEmail[i] = new Label(); 
      lblEmail[i].Text = "Email:"; 
      txtEmail[i] = new TextBox(); 

      PlaceHolder1.Controls.Add(new LiteralControl("<table>")); 
      PlaceHolder1.Controls.Add(new LiteralControl("<span style=\"font-weight:bold;\" ")); 
      PlaceHolder1.Controls.Add(authorInformation); 
      PlaceHolder1.Controls.Add(new LiteralControl("</span>")); 
      PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>")); 
      PlaceHolder1.Controls.Add(lblName[i]); 
      PlaceHolder1.Controls.Add(new LiteralControl("</td><td>")); 
      PlaceHolder1.Controls.Add(txtName[i]); 
      PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>")); 
      PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>")); 
      PlaceHolder1.Controls.Add(lblSurname[i]); 
      PlaceHolder1.Controls.Add(new LiteralControl("</td><td>")); 
      PlaceHolder1.Controls.Add(txtSurname[i]); 
      PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>")); 
      PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>")); 
      PlaceHolder1.Controls.Add(lblInstitution[i]); 
      PlaceHolder1.Controls.Add(new LiteralControl("</td><td>")); 
      PlaceHolder1.Controls.Add(txtInstitution[i]); 
      PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>")); 
      PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>")); 
      PlaceHolder1.Controls.Add(lblCountry[i]); 
      PlaceHolder1.Controls.Add(new LiteralControl("</td><td>")); 
      PlaceHolder1.Controls.Add(txtCountry[i]); 
      PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>")); 
      PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>")); 
      PlaceHolder1.Controls.Add(lblEmail[i]); 
      PlaceHolder1.Controls.Add(new LiteralControl("</td><td>")); 
      PlaceHolder1.Controls.Add(txtEmail[i]); 
      PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>")); 
      PlaceHolder1.Controls.Add(new LiteralControl("</table><br /> ")); 
      Panel1.Controls.Add(PlaceHolder1); 

      j++; 
     } 
    } 

Maintenant, je veux prendre la valeur de la fonction textboxes dans une autre public void UploadForm(){...}. je l'essaye

int i; 
int numberOfOtherAuthors = NumberOfOtherAuthors(); 

for(i=0; i<numberOfOtherAuthors; i++) 
{ 
    String a = txtname[i].text 
} 

Les valeurs TextBoxes que je veux télécharger la base de données, mais disons que nous les sauvions à une chaîne. Lorsque je fais cela, j'ai l'exception NullReferenceException. Alors, comment obtenir la valeur de ce texte ??? Merci

+0

Ceci est un code moche, il suffit d'utiliser le répéteur à la place ... –

+0

Dupliquer: [Comment obtenir une valeur à partir d'un TextBox] (http://stackoverflow.com/questions/10808314/how-to-get-value- from-a-textbox) – Jeremy

+0

Désolé, mais je n'ai pas trouvé de solution pour ce problème. Si vous pouvez m'aider ... je suis nouveau à C#. Thsnks – Jimmysnn

Répondre

1

j'ai remarqué que votre fonction UploadForm() fait référence txtname, alors que votre TextBox réelle est déclarée comme txtName. Comme les noms de champs sont sensibles à la casse, le champ "txtname" (minuscule "n") n'est pas réellement défini, c'est donc probablement la cause de votre NullReferenceException dans votre page dynamique.

Questions connexes