0

I have a user control (.ascx) added to my application:

<uc1:pomedsrow runat="server" id="POMedsRow" />

And here is html and logic

<asp:Panel ID="Panel1" runat="server">
    How many PO Meds do you wish to order?
    <asp:TextBox ID="txtReqPONum" runat="server" />
    <asp:LinkButton ID="lbnAddPOMeds" runat="server" Text="Go" 
                    OnClick="lbnAddPOMeds_Click"/>
</asp:Panel>

<asp:Panel ID="pnlPOMeds" Visible="false" runat="server">
    <table border="1">
    <tr>
        <td><p>PO Meds</p></td>
        <td><p>Min/Max</p></td>
        <td><p>Amount to Order</p></td>
    </tr>
    <uc1:pomedsrow runat="server" id="POMedsRow" />
    </table>
    <br />
</asp:Panel> 

protected void lbnAddPOMeds_Click(object sender, EventArgs e)
{
    int ReqPO = Convert.ToInt32(txtReqPONum.Text);
    int n = ReqPO;
    for (int i = 0; i < n; i++)
    {
        Control pomedsrow = new Control();
        //Assigning the textbox ID name 
        pomedsrow.ID = "txtPOAmount" + "" + ViewState["num"] + i;
        this.Form.Controls.Add(pomedsrow);
    }

}

But when I click the link button nothing happens. Am I not calling the custom control correctly?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Skullomania
  • 2,225
  • 2
  • 29
  • 65

1 Answers1

0

You are not adding your control properly. Try this:

protected void lbnAddPOMeds_Click(object sender, EventArgs e)
{
    TextBox txtReqPONum = (TextBox) Panel1.FindControl("txtReqPONum");
        int ReqPO = 0;
        if (txtReqPONum != null && int.TryParse(txtReqPONum.Text, out ReqPO) )
        {            
            int n = ReqPO;
            for (int i = 0; i < n; i++)
            {
                UserControl myControl = (UserControl)Page.LoadControl("~/pomedsrow.ascx");//(UserControl)Page.LoadControl("Your control path/pomedsrow.ascx");
                //Assigning the textbox ID name 
                myControl.ID = "txtPOAmount" + "" + ViewState["num"] + i;
                Panel1.Controls.Add(myControl);
            }
        }

}
afzalulh
  • 7,925
  • 2
  • 26
  • 37