Wednesday, May 11, 2011

Placeholder dynamic controls

So, I had to build on what I started yesterday.

Now I have a spiffy code sample for all the times when I want to add dynamic controls to placeholders..

The sample below adds 5 rows to a table with all kinds of funky controls each having its own properties set separately....

Here it goes ....drumroll !!!!!!

Front End


 <table class="sofT" style="width: 100%">
        <tr>
            <td class="SubClass">
                Course #
            </td>
            <td class="SubClass">
                Course Title
            </td>
            <td class="SubClass">
                Hours
            </td>
            <td class="SubClass">
                Grade
            </td>
            <td class="SubClass">
                Grade Pt.
            </td>
        </tr>
 <tr>
            <td>
                <asp:Label ID="lblCrNo5" runat="server"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="tbCrNo5" runat="server" Width="300"></asp:TextBox>
            </td>
            <td>
                <asp:TextBox ID="tbHrs5" runat="server" Width="30" Text="0.00"></asp:TextBox>
            </td>
            <td>
                <asp:DropDownList ID="ddlHrs5" runat="server">
                </asp:DropDownList>
            </td>
            <td>
                <asp:TextBox ID="tbGPA5" runat="server" Width="30" Text="0.00" Enabled="false"></asp:TextBox>
            </td>
        </tr>
        <asp:PlaceHolder ID="plCols" runat="server"></asp:PlaceHolder>
    </table>
 <asp:Label ID="lblCounter" runat="server" Visible="false"></asp:Label>
  <hr />
          <asp:Button ID="btnAdd" runat="server" CssClass="btn" Text="Add More Classes"
                    onclick="btnAdd_Click" />



And in the backend 



  protected void Page_Load(object sender, EventArgs e)
    {
        if (Cache["tt"] != null)
        {
            Panel tt = Cache["tt"] as Panel;
            plCols.Controls.Add(tt);


        }
}


 protected void btnAdd_Click(object sender, EventArgs e)


    {
            Panel tt = new Panel();
     
        int courseCounter = Convert.ToInt32(lblCounter.Text);


//this is a fix needed else 5 gets repeated twice.... 
        if (courseCounter == 5)
            courseCounter = 6;


        int numlabels = courseCounter + 5;


//this is getting the data for dropdownlist 
        DataTable ddlTable = createDdlTable();


        for (; courseCounter < numlabels; courseCounter++)
        {


            DropDownList ddl = new DropDownList();


            // Set the label's Text and ID properties.
            ddl.DataSource = ddlTable;
            ddl.DataTextField =  "hrsToShow";
            ddl.DataValueField = "hrsToShow";
            ddl.ID = "ddlHrs" + courseCounter;
            ddl.DataBind();
           
            Label lbl = new Label();
            lbl.ID = "lblCrNo"+courseCounter;
            lbl.Text = courseCounter.ToString();


            TextBox tb = new TextBox();
            tb.ID="tbCrNo"+courseCounter;
            tb.Width= 300;


            TextBox tb1 = new TextBox();
            tb1.ID="tbHrs"+courseCounter;
            tb1.Width= 30;
            tb1.Text = "0.00";


            TextBox tb2 = new TextBox();
            tb2.ID="tbGPA"+courseCounter;
            tb2.Width= 30;
            tb2.Text = "0.00";
            tb2.Enabled=false;


      //I know all this can be single Add... divided it for clarity !!
            tt.Controls.Add(new LiteralControl(""));
            tt.Controls.Add(lbl);
            tt.Controls.Add(new LiteralControl(""));
            tt.Controls.Add(tb);
            tt.Controls.Add(new LiteralControl(""));
            tt.Controls.Add(tb1);
            tt.Controls.Add(new LiteralControl(""));
            tt.Controls.Add(ddl);
            tt.Controls.Add(new LiteralControl(""));
            tt.Controls.Add(tb2);
            tt.Controls.Add(new LiteralControl(""));
            plCols.Controls.Add(tt);
            Cache["tt"] = tt;
        }


        ddlTable.Dispose();
        lblCounter.Text = numlabels.ToString();
    }



And as Disney says ...THATS ALL,  FOLKS !!


Hope to add the processing of dynamic controls thus added to tomorrow's post

No comments: