Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I used to create 2 text boxes dynamically on the 'create' link button. But if i go to (or Click)next static controls, when that dynamic created textbox and its value will not be visible...why?

I want to keep that dynamic textboxes and its value throughout the page or till i click on 'Submit' button.

Can anyone suggest to solve this issue? please!!

Thank you...
Posted
Comments
Yuriy Loginov 11-Jul-13 17:33pm    
I believe you can get the value of your text box like this:
Request.Form["id_of_your_textbox"]

1 solution

Your dynamically created controls will lost in post back. Keep the control in a session. Yor are not specified your code. Lets try below sample code and let me know


C#
protected void btnAddField_click( Object sender, EventArgs e ) {
        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
Sugu Thomas 12-Jul-13 0:27am    
Ok, Manu, i will check it....Thank you for the response.... Actually i forgot to put my code.. I placed a place holder for these 2 texboxes....
Manu V Nath 12-Jul-13 0:31am    
Try the code that i given it will work for some extend
Sugu Thomas 12-Jul-13 19:39pm    
Thank you once again for supporting... Your code is working and i enabled the viewstate = true for my dynamic label and textbox...

here is my full code:

The `lnkcat` is the linkbutton for Category and `lnksubcat` is the link button for Sub-category:

public partial class Products : System.Web.UI.Page
{
public static Label lblCat,lblSubcat;
public static TextBox txtCat, txtSubCat;

string _Cat;// stores new Sub cat
string _SubCat;

protected void lnkCat_Click(object sender, EventArgs e)
{

Panel2.Visible = false;
Panel1.Visible = true;

lblCat = new Label();
lblCat.Text = "Enter new Category: ";
PHcat.Controls.Add(lblCat);
lblCat.EnableViewState = true;

txtCat = new TextBox();
_Cat = txtCat.Text;
PhtxtCat.Controls.Add(txtCat);

}

protected void lnkSubCat_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
Panel2.Visible = true;

lblSubcat = new Label();
lblSubcat.Text = "Enter new Sub-Category: ";
PHsubCat.Controls.Add(lblSubcat);

txtSubCat = new TextBox();
_SubCat = txtSubCat.Text;
PhtxtSubCat.Controls.Add(txtSubCat);
}
}

Here my problem again is when i click on 'Create Category' link button (lnkCat_Click) dynamically its label and textbox is displayed for category and i have another link button for creating 'SuB-category' and when i click (lnkSubCat_Click) on that button for 'Create Sub category' THE FIRST DYNAMIC CREATED (FOR CATEGORY) LABEL AND TEXTBOX ARE REMOVED. HERE I WANT TO HOLD THAT DYNAMIC CREATED(CAT AND SUB-CAT) SHOULD BE DISPLAYED TILL I CLICK ON SUBMIT BUTTON ON THE SAME PAGE!

I have two create link button to create cat and sub-cat!

CAN U PLEASE HELP ON THIS MATTER? AND IS THIS POSSIBLE?

THANK YOU...EXPECTING A REPLY!

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