Dynamic web controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pisgasys
    New Member
    • May 2009
    • 6

    Dynamic web controls

    Hi,

    in my aspx page I have somting like that:
    Code:
    <asp:Content ID="Content1" ContentPlaceHolderID="Body" Runat="Server">
        <asp:UpdatePanel ID="UpdatePanel1" ChildrenAsTriggers="true" UpdateMode="Conditional" runat="server" >
            <ContentTemplate>
                <asp:Panel ID="Panel1" runat="server">
    
    <asp:Table ID="PO_Tags" Width="90%" BorderWidth="1" HorizontalAlign="Center" 
                    CellPadding ="2" CellSpacing ="1" visible="false" runat="server">
                    <asp:TableHeaderRow>
                        <asp:TableHeaderCell BackColor="AliceBlue" >Tag Name<br /><br /></asp:TableHeaderCell>
                        <asp:TableHeaderCell BackColor="AliceBlue" >Association Type<br /><br /></asp:TableHeaderCell>
                        <asp:TableHeaderCell BackColor="AliceBlue" >Tag Description<br /><br /></asp:TableHeaderCell>
                        <asp:TableHeaderCell BackColor="AliceBlue" >Tag Value<br /><br /></asp:TableHeaderCell>
                    </asp:TableHeaderRow>
                </asp:Table>
               
                <br />
                <asp:Button ID="Submit" Text="Submit" OnClick="Submit_Click" Visible="false" runat="server" />
               
                </asp:Panel>
            </ContentTemplate>
         </asp:UpdatePanel> 
    </asp:Content>
    from c# code I'm building my table rows dynamicly:
    Code:
    while (rdr.Read())
                        {
                            trRow = new TableRow();
                           
                            tcCell = new TableCell();
                            tcCell.Text = rdr["Label"].ToString();
                            trRow.Cells.Add(tcCell);
    
                            tcCell = new TableCell();
                            switch (rdr["ControlType"].ToString())
                            {
                                case "Text":
                                case "Integer":
                                case "Real":
                                    TextBox ctrl1 = new TextBox();
                                    ctrl1.ID = String.Format("tagCtrl{0}" ,countTagsControls);
                                    ctrl1.Text = "Test";
                                    tcCell.Controls.Add(ctrl1);
                                    //this.UpdatePanel1.ContentTemplateContainer.Controls.Add(ctrl1);
                                    break;
    
                                  .......
    
                         }
    
               trRow.Cells.Add(tcCell);
               PO_Tags.Rows.Add(trRow);
    Then I'm trying to get the values of those controls:
    Code:
    TextBox txtCtl = (TextBox)UpdatePanel1.FindControl(String.Format("tagCtrl{0}", i));
                        switch (rdr["ControlType"].ToString())
                        {
                            case "Text":
                                string v1 = txtCtl.Text.ToString();
                                break;
    But it's not working!!
    What I'm missing?!

    Thanks in advance.
    Last edited by PRR; May 26 '09, 10:00 AM. Reason: Code tags addded... Please post code in [code] [/code] tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You need to specify what isn't working.
    But I think I know what's wrong.
    You aren't able to find the TextBox are you?

    Well, that's because you declared the TextBoxes dynamically within the function that creates the DataTable. That means that these TextBoxes have a scope of that function...they no longer exist after the function is finished.

    Before I continue you need to specify what your problem is.
    Check out this article on How to use dynamic controls in ASP.NET

    Also research using Templates.

    Comment

    • pisgasys
      New Member
      • May 2009
      • 6

      #3
      Hi again,
      Well the problem is here:
      Code:
      TextBox txtCtl = (TextBox)UpdatePanel1.FindControl(String.format("tagCtrl{0}", i));
      string v1 = txtCtl.Text.ToString();
      The controls are created, but I'm not succeeding to get there values, If I'm trying to retrieve:
      Code:
       UpdatePanel1.FindControl(String.format("tagCtrl{0}", i)).ID
      I'm getting null!

      I tried declaring my textbox ctrl1 outside the function as public but I'm still facing the same issue!

      I'll research about Templates, thanks.
      If you have any other ideas please let me know.
      Last edited by Frinavale; May 27 '09, 01:02 PM. Reason: Added code tags. Please post code in [code] [/code] tags.

      Comment

      • pisgasys
        New Member
        • May 2009
        • 6

        #4
        When I put my code of building dynamic controls inside page_load it worked fine, but I need to run that code from another function, so I tried:
        Code:
        public partial class PO_Insert : System.Web.UI.Page
            {
         
                public TextBox ctrl1 = null;
                public TableRow trRow = null;
                public TableCell tcCell = null;
        but still have a problem!
        Last edited by Frinavale; May 27 '09, 01:02 PM. Reason: Added code tags. Please post code in [code] [/code] tags.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          You should be creating your new TextBox in the Page Init event (not the Page Load) or else you wont be able to retrieve the content from the dynamically created TextBox.

          You need to retrieve your TextBox from the control that it exists within....in this case it's part of the Table control (not the UpdatePanel).

          Don't you need more than one TextBox in your table?
          How many TextBoxes do you need?
          I still don't know what you're trying to do....

          Is the column displaying the "Associatio n Type" (the "ControlTyp e") supposed to be displaying data in TextBoxes for all rows?

          Comment

          • pisgasys
            New Member
            • May 2009
            • 6

            #6
            Hi,
            The number of my controls is changing dynamically according to association mechanism (I'm taking part of developing of system that simulating XFactory if you know it) .
            Those controls can be TextBoxes or CheckBoxes or ComboBoxes .. according to a controlType field stored in the db.
            So what I'm trying to do is:

            1) Fill a form with new production order for a factory (Line, CatalogID,...)
            2) Retrieve the Tags that are relevant to that PO
            3) Building the controls dynamically according to the tags hierarchy
            4) Filling values into those controls (manually) and submitting
            5) Build an XML file with those values
            6) Send that xml to a stored procedure that verify the data and it types (Via web service)
            7) Inserting that data into my tables if every thing is ok

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Ok, this is going to be a tricky one to implement.

              I'm not entirely sure how you're going to do this... or even if what I'm about to recommend is going work.

              I think you're going to have to do is create something to hold the dynamically created controls so that you can reference it later.

              I'm going to recommend creating a generic List of Web.UI.Control types:

              Code:
              public partial class PO_Insert : System.Web.UI.Page
              {
                List<Web.UI.Control> webControlContent;
              }
              Now in your Page Init event you're going to have poll the database in order to dynamically create your controls. Each control that you create you're going to have add to the the generic list and to your page.

              For now let's forget the Table all together.
              Let's just add the controls as one big mess to the page as we create them.

              So in the method that handles the Page Init class you'll have something like:

              Code:
              public partial class PO_Insert : System.Web.UI.Page
              {
                List<Web.UI.Control> webControlContent;
              
                private void PO_Insert_Init(Object sender, System.EventArgs e)
                {  
                   //Initializing the generic list of web controls
                   webControlContent = new List<Web.UI.Control>();
                   
                   /*
                    Now you need to call the database to figure out 
                     what type of controls to create.
                     
                     You should look into using Reflection to make this
                     process easier and cleaner.
                  */
              
                   /*
                    Add the controls to the webControlContent list and 
                    also add them Page.
              
                    Make sure to give the controls an ID so that 
                    you can access them later.
                    
                     You also need to specify any methods used to handle
                     any postback events that the controls may cause.
                  */
              
              
                }
              
              }
              You always need to recreate the controls used on the page in the Page Init event.

              After the Page Init event they are loaded with the data that was entered by the user. If the control does not exists when ASP.NET is trying to load the data you are going to have problems.

              Now that you have your controls available for you to use you can use them:

              Code:
              string v1 = "";
              foreach (Web.UI.Control ctrl in webControlContent )
              {
                  if(ctrl.ID == "txtCtrl")
                  {
                    TextBox txtCtl = (TextBox) ctrl;
                    v1 = txtCtl.Text;
                  }
              }

              Comment

              • pisgasys
                New Member
                • May 2009
                • 6

                #8
                Soluation

                That's what I did, and it works:

                Code:
                protected void Page_PreLoad(object sender, EventArgs e)
                        {
                            SqlConnection conn = null;
                            SqlCommand cmd = null;
                            SqlDataReader rdr = null;
                            int i = 0;
                
                            conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                            if (ViewState["isTextBoxCreated"] != null && (int)ViewState["isTextBoxCreated"] == 1) 
                            {
                                try
                                {
                                    TableRow trRow = null;
                                    TableCell tcCell = null;
                
                                    conn = new SqlConnection(conString);
                                    conn.Open();
                
                                    cmd = new SqlCommand("sp_BuildPODataTags", conn);
                                    cmd.CommandType = CommandType.StoredProcedure;
                
                                    cmd.Parameters.Add(new SqlParameter("@POID", txtPOID.Text));
                                    cmd.Parameters.Add(new SqlParameter("@LineUID", lstLines.SelectedValue.ToString()));
                
                                    rdr = cmd.ExecuteReader();
                
                                    while (rdr.Read())
                                    {
                                        trRow = new TableRow();
                
                                        tcCell = new TableCell();
                                        tcCell.Text = rdr["Label"].ToString();
                                        trRow.Cells.Add(tcCell);
                
                                        tcCell = new TableCell();
                                        if (Convert.ToInt16(rdr["AssociationType"]) == 100)
                                            tcCell.Text = "Global";
                                        else
                                            tcCell.Text = "Line";
                                        trRow.Cells.Add(tcCell);
                
                                        tcCell = new TableCell();
                                        tcCell.Text = rdr["Description"].ToString();
                                        trRow.Cells.Add(tcCell);
                
                                        tcCell = new TableCell();
                                        switch (rdr["ControlType"].ToString())
                                        {
                                            case "Text":
                                            case "Integer":
                                            case "Real":
                                                TextBox ctrl1 = new TextBox();
                                                ctrl1.ID = "tagCtrl" + i.ToString();
                                                tcCell.Controls.Add(ctrl1);
                                                break;
                                            case "Combo Box":
                                                break;
                                            case "Check Box":
                                                break;
                                        }
                                        i += 1;
                
                                        trRow.Cells.Add(tcCell);
                                        PO_Tags.Rows.Add(trRow);
                
                                    }
                                    
                                }
                
                                finally
                                {
                                    if (conn != null)
                                    {
                                        conn.Close();
                                    }
                                    if (rdr != null)
                                    {
                                        rdr.Close();
                                    }
                                }
                            }
                        }
                
                            
                         protected void GetTags_Click(object sender, EventArgs e)
                         {
                            if (Validate_Data())
                            {
                                SqlConnection conn = null;
                                SqlCommand cmd = null;
                                SqlDataReader rdr = null;
                                int countTagsControls = 0;
                
                                this.UpdatePanel1.FindControl("PO_Tags").Visible = true;
                                this.Submit.Visible = true;
                                
                                //Create_XML();
                                if (ViewState["isTextBoxCreated"] == null)
                                {
                
                                    try
                                    {
                                        TableRow trRow = null;
                                        TableCell tcCell = null;
                
                                        conn = new SqlConnection(conString);
                                        conn.Open();
                
                                        cmd = new SqlCommand("sp_BuildPODataTags", conn);
                                        cmd.CommandType = CommandType.StoredProcedure;
                
                                        cmd.Parameters.Add(new SqlParameter("@POID", txtPOID.Text));
                                        cmd.Parameters.Add(new SqlParameter("@LineUID", lstLines.SelectedValue.ToString()));
                
                                        rdr = cmd.ExecuteReader();
                                        while (rdr.Read())
                                        {
                                            trRow = new TableRow();
                
                                            tcCell = new TableCell();
                                            tcCell.Text = rdr["Label"].ToString();
                                            trRow.Cells.Add(tcCell);
                
                                            tcCell = new TableCell();
                                            if (Convert.ToInt16(rdr["AssociationType"]) == 100)
                                                tcCell.Text = "Global";
                                            else
                                                tcCell.Text = "Line";
                                            trRow.Cells.Add(tcCell);
                
                                            tcCell = new TableCell();
                                            tcCell.Text = rdr["Description"].ToString();
                                            trRow.Cells.Add(tcCell);
                
                                            tcCell = new TableCell();
                                            switch (rdr["ControlType"].ToString())
                                            {
                                                case "Text":
                                                case "Integer":
                                                case "Real":
                                                    TextBox ctrl1 = new TextBox();
                                                    ctrl1.ID = "tagCtrl" + countTagsControls.ToString();
                                                    ctrl1.Text = rdr["Value"].ToString ();
                                                    tcCell.Controls.Add(ctrl1);
                                                    ViewState["isTextBoxCreated"] = 1;
                                                    break;
                                                case "Combo Box":
                                                    break;
                                                case "Check Box":
                                                    break;
                                            }
                                            countTagsControls += 1;
                
                                            trRow.Cells.Add(tcCell);
                                            PO_Tags.Rows.Add(trRow);
                
                                        }
                                    }
                
                                    finally
                                    {
                                        if (conn != null)
                                        {
                                            conn.Close();
                                        }
                                        if (rdr != null)
                                        {
                                            rdr.Close();
                                        }
                                    }
                                }
                            }
                                
                        }
                
                        public void Create_XML()
                        {
                            SqlConnection conn = null;
                            SqlCommand cmd = null;
                            SqlDataReader rdr = null;
                
                            // Create a new XmlTextWriter instance
                            writer = new XmlTextWriter(Server.MapPath(@"XML\POInfo.xml"), Encoding.UTF8);
                
                            // Creating <POInfo> element
                            writer.WriteStartDocument();
                            writer.WriteStartElement("POInfo");
                
                            // Creating the <PO> element
                            writer.WriteStartElement("PO");
                
                            writer.WriteElementString("POID", txtPOID.Text);
                            writer.WriteElementString("LineID", lstLines.SelectedItem.ToString());
                            writer.WriteElementString("CatalogID", lstCatalogs.SelectedValue.ToString());
                            writer.WriteElementString("RequestedQty", txtRequestedQty1.Text + "." + txtRequestedQty2.Text);
                            writer.WriteElementString("LotID", txtLotID.Text);
                            writer.WriteElementString("Priority", txtPriority.Text);
                            writer.WriteElementString("UserID", Session["User"].ToString());
                
                            writer.WriteEndElement();
                            //  </PO>
                
                            // Creating <POTags> element
                            writer.WriteStartElement("POTags");
                            try
                            {
                                conn = new SqlConnection(conString);
                                conn.Open();
                
                                cmd = new SqlCommand("sp_BuildPODataTags", conn);
                                cmd.CommandType = CommandType.StoredProcedure;
                
                                cmd.Parameters.Add(new SqlParameter("@POID", txtPOID.Text));
                                cmd.Parameters.Add(new SqlParameter("@LineUID", lstLines.SelectedValue.ToString()));
                
                                rdr = cmd.ExecuteReader();
                
                                int i = 0;
                                String ctlValue = "";
                                while (rdr.Read())
                                {
                                    switch (rdr["ControlType"].ToString())
                                    {
                                        case "Text":
                                        case "Real":
                                        case "Integer":
                                            TextBox txtCtl = (TextBox)UpdatePanel1.FindControl("tagCtrl" + i);
                                            ctlValue = txtCtl.Text.ToString();
                                            break;
                                    }
                                    i += 1;
                
                                    writer.WriteStartElement("Tag");
                                    writer.WriteAttributeString("ID", rdr["TagDefId"].ToString());
                                    writer.WriteElementString("Value", ctlValue);
                                    writer.WriteEndElement();
                                }
                            }
                            finally
                            {
                                if (conn != null)
                                    conn.Close();
                                if (rdr != null)
                                    rdr.Close();
                            }
                            writer.WriteEndElement();
                            //  </POTags>
                
                            writer.WriteEndElement();
                            writer.WriteEndDocument();
                            writer.Close();
                            //  </POInfo>
                        }
                Thanks for your efforts.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by pisgasys
                  That's what I did, and it works:
                  ...
                  Thanks for your efforts.
                  I'm glad you got it working :)

                  Comment

                  Working...