| |

VerySource

 Forgot password?
 Register
Search
View: 656|Reply: 8

How to dynamically add and delete user controls and initialization issues, thank you

[Copy link]

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-19 19:30:02
| Show all posts |Read mode
There are two buttons btn_add and btn_dll in the index.aspx page
The requirement is to add a news.ascx control to the index.aspx page when clicking btn_add
Keep clicking to continue adding news.ascx control to index.aspx
When clicking btn_dll, delete the last added news.ascs control, and then click and delete the last added one.

Thank you
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-6-25 10:00:01
| Show all posts
private int ControlsCount
{
get
{
return ViewState["ControlsCount"] == null?0:(int)ViewState["ControlsCount"];
}

set
{
ViewState["ControlsCount"] = value;
}
}

private void Button1_Click(object sender, System.EventArgs e)
{
UserControl c = (UserControl) this.LoadControl("MyControls.ascx");
this.Panel1.Controls.Add(c);
this.ControlsCount ++;
}

private void Button2_Click(object sender, System.EventArgs e)
{
if(this.Panel1.Controls.Count >0)
{
this.Panel1.Controls.RemoveAt(this.Panel1.Controls.Count-1);
this.ControlsCount --;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
The

for(int i=0;i<this.ControlsCount;i++)
{
UserControl c = (UserControl) this.LoadControl("MyControls.ascx");
this.Panel1.Controls.Add(c);
}
The

}
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-6-25 10:30:02
| Show all posts
Button1 corresponds to btn_add
2 corresponds to btn_dll
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-6-25 22:45:01
| Show all posts
There is a problem:
I talk about my environment:
There is an add.ascx user control in my productadd.aspx page
User controls are used to add products
But now users require that multiple products can be added at one time, so I want to use dynamic loading of this add.ascx to add multiple products
After I used your code above: When add.ascx is added, if(!ispostback) in the load in add.ascx is executed
Although add.ascx is loaded for the first time, productadd.aspx is not loaded for the first time, so the code inside will not be executed
I can't initialize the content in my add.ascx
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-6-26 11:30:01
| Show all posts
Such as:
aspx
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp:Button id="Button2" runat="server" Text="Button"></asp:Button>
<asp:Panel id="Panel1" runat="server"></asp:Panel>
ascx:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
ascx.cs:
if(!IsPostBack)
{
  TextBox1.Text="dd";
}
That is, when ascx.cs is initialized, it will not refer to the line TextBox1.Text="dd"; the initial content of the added ascx cannot be given
Thank you
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-6-27 12:15:02
| Show all posts
In user controls.

public bool bFirst = false; //This variable does not save state

ascx.cs:
In the load

if(bFirst)
{
   TextBox1.Text="dd"; //dd is the database read
}


Then in the event of btn_add.

MyControls c = (MyControls) this.LoadControl("MyControls.ascx");
c.bFirst = true;
this.Panel1.Controls.Add(c);

Pay attention to the order. c.bFirst = true must be before the next sentence.
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-6-27 17:15:01
| Show all posts
It is not possible to initialize the user control now. My intention is to talk about my environment: My productadd.aspx page has an add.ascx user control user control to add products, but now the user requires that multiple products can be added at one time, so I want to use the dynamic loading of this add.ascx to add multiple products. I used your code above: When adding add.ascx, when executing the if(!ispostback) in the load in add.ascx, although add.ascx is It was loaded for the first time but productadd.aspx was not loaded for the first time, so the code inside would not be executed and the content in my add.ascx could not be initialized as the following code ascx: <asp:TextBox id="TextBox1" runat="server"></ asp:TextBox> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button> ascx.cs: load() {if(!IsPostBack) {TextBox1.Text="dd "; //dd is the database read}} btn_click() {update to the database} Thank you
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-6-28 07:45:01
| Show all posts
c.bFirst = true;
Error here no bFirst
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-6-28 10:00:01
| Show all posts
((myuc)c).bFirst=true;
Ok
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list