using controls created in c# page forward an aspx page webform

marc-antoine yonga 81 Reputation points
2023-06-05T11:57:47.7366667+00:00
sirs,

my problem is about the creation of controls in c# page using forward an aspx page as dynamically controls.

the textbox and button controls created had sent to the aspx page and the showing is ok. but the problem is that when i want to used these dynamically controls on aspx page, the controls disapear, these controls are not valid and unfunctionnals.

how can i use and valid these dynamics controls created in c# page forward the aspx page.

the code is the following :

-- aspx page :
<%@ Page Language="jicixipi" AutoEventWireup="true" transition_code_file="persistant" codefile="WF_MS_jicixipi_testpro_interconnexions_va_te_faire_foutre.aspx.cs" inherits="jicixipi.WF_MS_jicixipi_testpro_interconnexions_va_te_faire_foutre" %>
<div>
<asp:button id="bt_1" runat="server" Text="push_variable" onclick="click_bt_1" />&nbsp;<asp:button id="bt_3" runat="server" Text="push_variable_confirm" onclick="click_bt_3" />
<br />
<asp:table id="table_1" runat="server">
</asp:table>
<br />
<textarea id="tea_1" runat="server"></textarea>
</div>

-- c# page :
public partial class WF_MS_jicixipi_testpro_interconnexions_va_te_faire_foutre : System.Web.UI.Page
    {
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TextBox tb_1 = new TextBox();
TextBox tb_2 = new TextBox();
//Button bt_2 = new Button();
int IntChamps;

protected void page_load (object sender, EventArgs e)
{
bt_1.Click += new EventHandler (click_bt_1);
bt_3.Click += new EventHandler (click_bt_3);
}

protected void click_bt_1 (object sender, EventArgs e)
{
IntChamps = 1;
if (IntChamps == 1)
{
cell1.Controls.Add(tb_1);
cell2.Controls.Add(tb_2);
row.Controls.Add(cell1);
row.Controls.Add(cell2);
}
table_1.Controls.Add(row);
}

protected void click_bt_3 (object sender, EventArgs e)
{
tea_1.Value = tb_1.Text + " // " + tb_2.Text;                             // théoriquement, :(, ça marche !!!!!!!!!!!!!!!!!!!!!!!!!!!!
}

thanks for your answer.

marc-antoine.
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,596 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,404 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,601 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 28,826 Reputation points Microsoft Vendor
    2023-06-06T05:45:16.3033333+00:00

    Hi @marc-antoine yonga,

    If your purpose is just to pass tb_1.Text and tb_2.Text to tea_1.

    Then it is necessary to realize the dynamic creation of controls in page_load.

    You can't do it with two buttons, they are actually two separate functions because of postback issues.

      <div>
                <asp:Button ID="bt_1" runat="server" Text="push_variable"/>&nbsp;<asp:Button ID="bt_3" runat="server" Text="push_variable_confirm" OnClick="click_bt_3" />
                <br />
                <asp:Table ID="table_1" runat="server">
                </asp:Table>
    
                <br />
                <textarea id="tea_1" runat="server"></textarea>
            </div>
    
     TableRow row = new TableRow();
            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            TextBox tb_1 = new TextBox();
            TextBox tb_2 = new TextBox();
            //Button bt_2 = new Button();
            int IntChamps;
    
            protected void page_load(object sender, EventArgs e)
            {
                tea_1.Visible= false;
                bt_3.Click += new EventHandler(click_bt_3);
                IntChamps = 1;
                if (IntChamps == 1)
                {
                    cell1.Controls.Add(tb_1);
                    cell2.Controls.Add(tb_2);
                    row.Controls.Add(cell1);
                    row.Controls.Add(cell2);
                }
                table_1.Controls.Add(row);
            }
    
    
    
            protected void click_bt_3(object sender, EventArgs e)
            {
                table_1.Controls.Remove(row);
                tea_1.Visible = true;
    
                tea_1.Value = tb_1.Text + " // " + tb_2.Text;                           // théoriquement, :(, ça marche !!!!!!!!!!!!!!!!!!!!!!!!!!!!
            }
    

    3

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. AgaveJoe 27,576 Reputation points
    2023-06-05T13:07:51.2633333+00:00

    but the problem is that when i want to used these dynamically controls on aspx page, the controls disappear,

    Correct, this is the expected results.

    Dynamically created server controls do not take part in ASP.NET Web Forms state management. It is up to the developer to design and write code to track what controls should exist on every post back and recreate the controls.

    I recommend using the data bound controls that come with Web Forms. It looks like you are creating a table. A Repeater or GridView are alternative approaches. Please see the official documentation.

    GridView Class

    Repeater Class

    0 comments No comments