Announcement

Collapse
No announcement yet.

Dynamisch erstellte TextBox nach PostBack auslesen!

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamisch erstellte TextBox nach PostBack auslesen!

    Hallo Zusammen!

    Habe ein kleines Problem mit meiner Webapplikation.

    Nach der auswahl eines Eintrages einer DropDownList folgt eine Funktion die dynamisch TextBoxen und Labels erstellt. Die Anzahl der TextBoxen und der Labels hängt vom Ausgewählten Eintrag ab.


    Beim klick auf einen ImageButton sollte der Text aus den TextBoxen gelesen werden und danach in ein Objekt geschrieben werden.

    PROBLEM: Nach dem ich auf den ImageButton geklickt habe folgt ein PostBack was meine TextBoxen zurücksetz, sodass kein Text mehr vorhanden ist.

    FRAGE: Habe keine Ahnung wie ich das nun Lösen könnte. Habe schon fast alles Probiert, jedoch ohne erfolg. Wie kann man erreichen das die TextBoxen so bleiben wie si sind!

    Wenn ihr den Code benötigt nur Fragen!

    Gruss Floydmovie

  • #2
    Versuch mal BEVOR du das ganze wechselst, sämtliche inhalte in ein Array zu schreiben und danach zurück in die jeweilige Textbox zu schreiben, dann sollte der Text eigentlich bleiben.
    Ist vieleicth nicht perfekt, aber danach sollte es eigentlich laufen.

    Comment


    • #3
      Danke für die Antwort...


      Wie kann ich die inhalte in Array speichern bevor gewechselt wird?

      Gruss

      Comment


      • #4
        Merk Dir doch einfach eine Referenz auf die dynamisch erstellte TextBox. Funktionier das nicht?

        [highlight=c#]
        public class MyPage:Page
        {
        private TextBox _myTextBox;

        public void ErzeugeTextBox()
        {
        _myTextBox = new TextBox();
        myPanel.Controls.Add(_myTextBox);
        }
        }
        [/highlight]

        Sowas in der Art ungefähr. Wenn Du mehrere brauchst, dann kannst Du sie Dir in einer Liste merken (List<TextBox>). Arrays verwendet man im Prinzip gar nicht mehr, ausser es ist wirklich von Nöten.

        Comment


        • #5
          Danke..

          Habe es nun mal so gemacht:

          Code:
          private List<TextBox> textBoxen = new List<TextBox>();
          Code:
          private void setTextBoxTable()
                  {
                      if (toolDropDownList.SelectedValue != "")
                      {
                          ToolId = Convert.ToInt32(toolDropDownList.SelectedValue);
                      }
                      if (ToolId != -1 && ToolId != 0)
                      {
                          AttributeDAL attributeDAL = new AttributeDAL();
                          ToolDAL toolDAL = new ToolDAL();
                          int attributeId = toolDAL.GetAttributeId(ToolId);
                          Attribute attribute = attributeDAL.GetAttributByAttributId(attributeId);
                          List<String> attributeListe = attributeDAL.ToList(attribute);
                          attributeListe.Add("");
                          attributeListe.Add("");
                          attributeListe.Add("");
                          attributeListe.Add("");
                          attributeListe.Add("");
                          attributeListe.Add("");
                          attributeListe.Add("");
          
                          int index = 0;
          
                          for (int i = 0; i <= 25; i++)
          			    {
                              TableRow tableRow1 = new TableRow();
                              TableRow tableRow2 = new TableRow();
                              tableRow1.Style.Value = "vertical-align:bottom";
          
                              TableCell tableCell1 = new TableCell();                    
                              TableCell tableCell2 = new TableCell();
                              TableCell tableCell3 = new TableCell();
                              TableCell tableCell4 = new TableCell();
                              TableCell tableCell5 = new TableCell();
                              TableCell tableCell6 = new TableCell();
                              TableCell tableCell7 = new TableCell();
                              TableCell tableCell8 = new TableCell();
                              tableCell1.Width = 200;
                              tableCell2.Width = 200;
                              tableCell3.Width = 200;
                              tableCell4.Width = 200;
          
                              if(attributeListe.Count > index)
                              {
                                  if (attributeListe[index+3] != "-1")
                                  {
                                      if (attributeListe[index+3] != "")
                                      {
                                          TextBox testTextBox = new TextBox();
                                          tableCell1.Text = attributeListe[index+3];
                                          tableRow1.Cells.Add(tableCell1);
          
                                          testTextBox.ID = "A" + (index) + "TextBox";
                                          testTextBox.Width = 180;
                                          tableCell5.Controls.Add(testTextBox);
                                          tableRow1.Cells.Add(tableCell1);
                                          tableRow2.Cells.Add(tableCell5);
                                          textBoxen.Add(testTextBox);
                                          index++;
                                      }
                                  }
                              }
          
          
                              if(attributeListe.Count > index)
                              {
                                  if (attributeListe[index-1] != "-1")
                                  {
                                      if (attributeListe[index-1] != "")
                                      {
                                          TextBox testTextBox = new TextBox();
                                          tableCell2.Text = attributeListe[index - 1];
                                          tableRow1.Cells.Add(tableCell2);
                                          testTextBox.ID = "A" + (index) + "TextBox";
                                          testTextBox.Width = 180;
                                          tableCell6.Controls.Add(testTextBox);
                                          tableRow1.Cells.Add(tableCell1);
                                          tableRow2.Cells.Add(tableCell6);
                                          index++;
                                          textBoxen.Add(testTextBox);
                                      }
                                  }
                              }
          
                              if(attributeListe.Count > index)
                              {
                                  if (attributeListe[index-1] != "-1")
                                  {
                                      if (attributeListe[index-1] != "")
                                      {
                                          TextBox testTextBox = new TextBox();
                                          tableCell3.Text = attributeListe[index - 1];
                                          tableRow1.Cells.Add(tableCell3);
                                          testTextBox.ID = "A" + (index) + "TextBox";
                                          testTextBox.Width = 180;
                                          tableCell7.Controls.Add(testTextBox);
                                          tableRow1.Cells.Add(tableCell1);
                                          tableRow2.Cells.Add(tableCell7);
                                          index++;
                                          textBoxen.Add(testTextBox);
                                      }
                                  }
                              }
          
                              if(attributeListe.Count > index)
                              {
                                  if (attributeListe[index-1] != "-1")
                                  {
                                      if (attributeListe[index-1] != "")
                                      {
                                          TextBox testTextBox = new TextBox();
                                          tableCell4.Text = attributeListe[index - 1];
                                          tableRow1.Cells.Add(tableCell4);
                                          testTextBox.ID = "A" + (index) + "TextBox";
                                          testTextBox.Width = 180;
                                          tableCell8.Controls.Add(testTextBox);
                                          tableRow1.Cells.Add(tableCell1);
                                          tableRow2.Cells.Add(tableCell8);
                                          index++;
                                          textBoxen.Add(testTextBox);
                                      }
                                  }
                              }
          
          
                              neuTextBoxTable.Rows.Add(tableRow1);
                              neuTextBoxTable.Rows.Add(tableRow2);
                          }
          
                          neuDiv.Visible = true;
                      }
                  }

          Code:
          protected void addImageButton_Click(object sender, ImageClickEventArgs e)
                  {
          
                      Parameter parameter = new Parameter();
                      ParameterDAL attributeDAL = new ParameterDAL();
          
                      int index = 1;
          
                      TextBox textBox = new TextBox();
          
                      if (textBoxen[index] != null)
                      {
                          parameter.A1 = textBoxen[0].Text;
                      }
                      index++;
          
          ---> Würden noch bis 99 gehen!
          
                      parameter.Titel = titelTextBox.Text;
                      parameter.ToolId = Convert.ToInt32(toolDropDownList.SelectedValue);
                      parameter.CreateDate = DateTime.Now;
                      parameter.CreateUser = System.Environment.UserName;
                      parameter.UpdateDate = DateTime.Now;
                      parameter.UpdateUser = System.Environment.UserName;
          
                      attributeDAL.Insert(parameter);
          
                      titelTextBox.Text = "";
                      toolDropDownList.SelectedIndex = 0;
          
                      abbrechenMeldungDiv.Visible = false;
                      grundDiv.Visible = false;
                      neuDiv.Visible = false;
                      erfolgreichMeldungDiv.Visible = true;
                  }

          Funktioniert leider so noch nicht!!!

          Ja habe eben alle TextBoxen(Gesammt 99 wegen dem ist der Code so *scheisse* in die Liste übernommen. Leider wenn ich wieder zugreifen will nach dem klick des ImageButton ist die Liste wieder null.

          Comment


          • #6
            Habe eine neue Seite kurz erstellt und hier funktioniert es mit der Liste -.-

            Code:
                    private List<TextBox> textBoxen = new List<TextBox>();
            
                    protected void Page_Load(object sender, EventArgs e)
                    {
                        setTextBoxTable();
                    }
            
                    private void setTextBoxTable()
                    {
                        for (int i = 0; i < 20; i++)
            			{
            			    TableRow tableRow = new TableRow();
                            TableCell tableCell = new TableCell();
            
                            TextBox textBoxe = new TextBox();
                            textBoxe.ID = "Test" + i;
            
                            tableCell.Controls.Add(textBoxe);
                            tableRow.Cells.Add(tableCell);
            
                            Table1.Rows.Add(tableRow);
            
                            textBoxen.Add(textBoxe);
            			} 
                    }
            
                    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
                    {
                        string test = textBoxen[0].Text;
                        string test2 = textBoxen[1].Text;
                        textBoxen[0].BackColor = System.Drawing.Color.AliceBlue;
                    }

            Comment


            • #7
              Naja wenn Du natürlich das TextBox Array bei jedem PostBack neu erzeugst ist mir klar dass das so funktioniert. Machst Du das in deiner Applikation auch so?

              Comment


              • #8
                Ow stimmt.. Ne mache ich nicht ... Darum wird es bei mir nun angezeigt. Würde in meiner Applikation nicht funktionieren dadruch das die Erstellung von der Dropdownlist abhängig ist.

                Comment


                • #9
                  Versuch mal dem selbst erzeugten control eine id zuzuweisen. Nach dem PostBack kannst Du in der Seite mit this.FindControl(string id) nach dem Control suchen. Eigentlich müsste das dank ViewState noch da sein.

                  [highlight=c#]
                  var myTextBox = new TextBox();
                  myTextBox.ID = "SupiTextBox1";

                  ...

                  var textBoxNachDemPostBack = (TextBox)this.FindControl("SupiTextBox1");
                  [/highlight]

                  Comment


                  • #10
                    Habe es probiert. Wurde nicht gefunden. "null" -.-

                    Ist es möglich eine List<TextBox> in eine ViewState zu übergeben?

                    oder

                    was könnte ich sonst noch ausprobieren.. Mit dem auslesen bevor ich ein Postback mache .. würde dies gehen?

                    Gruss und Danke

                    Comment


                    • #11
                      Es gibt den ViewState auch als Variable im Programm. Kannst ja mal in der Richtung suchen.

                      Comment


                      • #12
                        Hast du es so gemeint?

                        Code:
                                public List<TextBox> ListeTextBox
                                {
                                    get
                                    {
                                        if (ViewState["listeTextBox"] == null)
                                        {
                                            ViewState["listeTextBox"] = new List<TextBox>();
                                        }
                                        return (List<TextBox>)ViewState["listeTextBox"];
                                    }
                                    set 
                                    {
                                        ViewState["listeTextBox"] = value; 
                                    }
                                }
                        Wenn Ja, gibt einen Fehler das er die Liste nicht in ein ViewState speichern kann.

                        Comment

                        Working...
                        X