Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
For each button click i want to create text boxes and there corresponding labels dynamically.Please see below code.

C#
    <table id="tblPropertyCustom"  runat="server" enableviewstate="true"></table>



    private const string FIELDCOUNT = "fieldcount";
private int FieldCount {
    get { return (int)(ViewState[FIELDCOUNT] ?? Select_All); }
    set { ViewState[FIELDCOUNT] = value; }
}
     protected void btnAddField_click( Object sender, EventArgs e ) {

    CheckBox chkNewField = new CheckBox();
    chkNewField.ID      = "chkNewField"+ FieldCount.ToString();
    chkNewField.Checked = true;

    Label LblNewLabel   = new Label();
    LblNewLabel.ID = "lblNewLabel" + FieldCount.ToString();
    LblNewLabel.Text    = "New Lable";

    TextBox TxtNewLabel = new TextBox();
    TxtNewLabel.ID = "TxtNewLabel" + FieldCount.ToString();

    Label LblNewValue   = new Label();
    LblNewValue.ID = "lblNewValue" + FieldCount.ToString();
    LblNewValue.Text = "New Value";

    TextBox TxtNewValue = new TextBox();
    TxtNewValue.ID = "TxtNewValue" + FieldCount.ToString();

    HtmlTableRow  tRow   = new HtmlTableRow ();

    HtmlTableCell tCell1 = new HtmlTableCell("th");
    HtmlTableCell tCell2 = new HtmlTableCell("th");
    tCell2.Attributes.Add( "class", "medium" );
    HtmlTableCell tCell3 = new HtmlTableCell("th");
    tCell3.Attributes.Add( "class", "medium" );
    HtmlTableCell tCell4 = new HtmlTableCell();
    HtmlTableCell tCell5 = new HtmlTableCell("th");
    tCell5.Attributes.Add( "class", "medium" );
    HtmlTableCell tCell6 = new HtmlTableCell("th");
    tCell6.Attributes.Add( "class", "medium" );

    tCell1.Controls.Add(chkNewField);
    tCell2.Controls.Add(LblNewLabel);
    tCell3.Controls.Add(TxtNewLabel);
    tCell4.Controls.Add( new LiteralControl( "" ) );
    tCell5.Controls.Add( LblNewValue );
    tCell6.Controls.Add( TxtNewValue );

    tRow.Cells.Add(tCell1);
    tRow.Cells.Add(tCell2);
    tRow.Cells.Add(tCell3);
    tRow.Cells.Add(tCell4);
    tRow.Cells.Add(tCell5);
    tRow.Cells.Add(tCell6);

    tblPropertyCustom.Rows.Add(tRow );
    FieldCount++;
}


Its showing only one row of labels and fields. On next click the row values will replace by next counted labels and fields.Please help me(I can see only 1 row).

Thanks in advance :)
Manu v Nath
Posted
Updated 3-Jun-13 3:30am
v2
Comments
Sunasara Imdadhusen 3-Jun-13 9:02am    
What would you like to do?

Hello Manu,

The problem your are facing is due to the fact that Object references to dynamically added controls are lost during a postback because they have no handle in the codebehind. The below mentioned articles one available right here on CP will help you implement this correctly.


Regards,
 
Share this answer
 
v2
Comments
Ankur\m/ 3-Jun-13 9:27am    
I didn't know that... actually never tried.
5 for the correct answer!
I also think what he is doing doesn't require a postback and should be done on the client side. The best web applications today try to exploit client side processing as much as they can.
Prasad Khandekar 3-Jun-13 9:49am    
You are absolutely right Ankur, what he is trying to do can be easily done on client side alone. There is no real need to use a postback unless of course there is something more required. Thank's for sharing the client side script link as well.
Its showing only one row of labels and fields. On next click the row values will replace by next counted labels and fields.Please help me(I can see only 1 row).

tblPropertyCustom is a server side Table control, right? It doesn't retain the previous content which means either the viewstate is not enabled for it or you are clearing it somewhere. Also can you tell me if the value of FieldCount next time you click on the button? Does it show updated value or is it the same as last one?

Also, are you sure you want to do it on server side? Because I do not the need. Everything you are doing here can be done on client side as well using JavaScript. This link should help - add textbox dynamically javascript[^].
 
Share this answer
 
use
ASP.NET
<asp:placeholder id="placeHolderTable" runat="server"></asp:placeholder>
in your aspx page.
And on click event of the add button use the following code:
C#
int FieldCount = 0;
if (ViewState["FieldCount"] != null)
{
    FieldCount = (int)ViewState["FieldCount"];
}

Table tbl = new Table();
if (Session["DynamicTable"] != null)
{
    tbl = (Table)Session["DynamicTable"];
}

CheckBox chkNewField = new CheckBox();
chkNewField.ID = "chkNewField" + FieldCount.ToString();
chkNewField.Checked = true;

Label LblNewLabel = new Label();
LblNewLabel.ID = "lblNewLabel" + FieldCount.ToString();
LblNewLabel.Text = "New Lable";

TextBox TxtNewLabel = new TextBox();
TxtNewLabel.ID = "TxtNewLabel" + FieldCount.ToString();

Label LblNewValue = new Label();
LblNewValue.ID = "lblNewValue" + FieldCount.ToString();
LblNewValue.Text = "New Value";

TextBox TxtNewValue = new TextBox();
TxtNewValue.ID = "TxtNewValue" + FieldCount.ToString();

TableRow tRow = new TableRow();

TableCell tCell1 = new TableCell();
TableCell tCell2 = new TableCell();
tCell2.Attributes.Add("class", "medium");
TableCell tCell3 = new TableCell();
tCell3.Attributes.Add("class", "medium");
TableCell tCell4 = new TableCell();
TableCell tCell5 = new TableCell();
tCell5.Attributes.Add("class", "medium");
TableCell tCell6 = new TableCell();
tCell6.Attributes.Add("class", "medium");

tCell1.Controls.Add(chkNewField);
tCell2.Controls.Add(LblNewLabel);
tCell3.Controls.Add(TxtNewLabel);
tCell4.Controls.Add(new LiteralControl(""));
tCell5.Controls.Add(LblNewValue);
tCell6.Controls.Add(TxtNewValue);

tRow.Cells.Add(tCell1);
tRow.Cells.Add(tCell2);
tRow.Cells.Add(tCell3);
tRow.Cells.Add(tCell4);
tRow.Cells.Add(tCell5);
tRow.Cells.Add(tCell6);

tbl.Rows.Add(tRow);
placeHolderTable.Controls.Remove(tbl);
placeHolderTable.Controls.Add(tbl);
Session["DynamicTable"] = tbl;
FieldCount++;
ViewState["FieldCount"] = FieldCount;
 
Share this answer
 
v2
Comments
Manu V Nath 4-Jun-13 0:55am    
Thanks. Code is working fine :)
Zafar Sultan 4-Jun-13 3:13am    
welcome
Manu V Nath 20-Jun-13 7:25am    
Hi Zafar. How i can get CheckBox checked Text box values from your code. I tried below code its not working.

protected void BtnPublish_click( object sender, EventArgs e ) {
TextBox tb = (TextBox)placeHolderTable .FindControl( "TxtNewLabel1" );
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900