4

I have a user control(UserControl1) on my page and one Add more button.Clicking on Add More Button add the same user control again.When i click on user control first time,it adds second User Control,but on second time it doesnot add other.I think the control gets lost. I am adding a control like this on link button click:-

protected void lnkadd_Click(object sender, EventArgs e)
{
    HtmlTableCell tCell = new HtmlTableCell();
    UserControl uc = (UserControl)Page.LoadControl("~/Controls/DirectionalPricingCtrl.ascx");
    tCell.Controls.Add(uc);
    tablerow.Cells.Add(tCell);
}

I think i am doing something wrong and i should add user controls in respect of page life cycle,but how? Can somebody please guide me or provide some useful links? What approach i should follow when i have to add a user control each time when i click on a Add button and then later retrieve all the values and save it to DB?

user3379116
  • 67
  • 1
  • 3
  • 8

3 Answers3

5

I prefer javascript / ajax solution but I think there is nothing wrong with your code.

I did small example. Here is same solution as you have. Advantage is that control is loaded only in case of click.

public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLink_Click(object sender, EventArgs e)
        {
            var uc = (UserControl)Page.LoadControl("~/WebUserControl1.ascx");
            pnl.Controls.Add(uc);            
        }
    }

Here is example where user control is loaded in Page_Load event and in case of click (btnLink_Click) user control is added to panel. It works same as your solution but user control can be loaded (processed in memory not redered) even if is not needed.

public partial class Default : System.Web.UI.Page
    {
        UserControl uc; 
        protected void Page_Load(object sender, EventArgs e)
        {
           if(IsPostBack) // This condition is not needed but we know that click is always postback
            uc = (UserControl)Page.LoadControl("~/WebUserControl1.ascx");
        }

        protected void btnLink_Click(object sender, EventArgs e)
        {

            pnl.Controls.Add(uc);            
        }
    }

Here is solution which I prefer, it's based on visible property. In case that user control is not visible it's not rendered to output. Sure it's not very practival in case of table with lot of cells as a control container instead of panel.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<%@ Register Src="~/WebUserControl1.ascx" TagName="ucrCtrl" TagPrefix="ctr"  %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btnLink" runat="server" Text="Add" OnClick="btnLink_Click" />
        <asp:Panel runat="server" ID="pnl">
            <ctr:ucrCtrl runat="server" ID="usrCtrl" Visible="false" />
        </asp:Panel>
    </div>
    </form>
</body>
</html>

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLink_Click(object sender, EventArgs e)
        {
            usrCtrl.Visible = true;     
        }
    }
}
Jaroslav Kubacek
  • 1,387
  • 18
  • 26
0

Your code to add controls programmatically is right. Try doing,

tablerow.Cells.Add(tCell);

instead of adding it to the controls. this looks like a control rendering issue.

Raja Nadar
  • 9,409
  • 2
  • 32
  • 41
0

You thought right, your controls get lost due to a missing procedure which should handles post backs. Whenever you define a variable of a control-class such as HtmlTableCell, you should handle all procedures required for saving and loading data from posted page by yourself, instead of using predefined ones offered by asp.net run-time engine.

It's better to save and load those controls in some storage places during Postback such as ViewState on each post-back on PageLoad, then, handle adding/removing extra controls in lnkadd_Click method.

user3379116
  • 67
  • 1
  • 3
  • 8
Ali Dehghan
  • 462
  • 3
  • 6