John Sheedy
John Sheedy

Reputation: 287

Save Values of Dynamically created TextBoxes

guys i am creating dynamic TextBoxes everytime a button is clicked. but once i have as many text boxes as i want.. i want to save these value Database Table.. Please guide how to save it into DB

public void addmoreCustom_Click(object sender, EventArgs e)
{
    if (ViewState["addmoreEdu"] != null)
    {
        myCount = (int)ViewState["addmoreEdu"];
    }
    myCount++;
    ViewState["addmoreEdu"] = myCount;
    //dynamicTextBoxes = new TextBox[myCount];

    for (int i = 0; i < myCount; i++)
    {
        TextBox txtboxcustom = new TextBox();
        Literal newlit = new Literal();
        newlit.Text = "<br /><br />";
        txtboxcustom.ID = "txtBoxcustom" + i.ToString();
        myPlaceHolder.Controls.Add(txtboxcustom);
        myPlaceHolder.Controls.Add(newlit);
        dynamicTextBoxes = new TextBox[i];
    }
}

Upvotes: 1

Views: 4405

Answers (3)

Sagar Myakal
Sagar Myakal

Reputation: 1

To get values of dynamically created controls on postback you need to recreate those controls on Page_Init event Then view state of those controls will be loaded and you will get controls and there values.

public void Page_Init(object sender, EventArgs e)
{
addControls(myCount);
}

I hope this will resolve your problem Happy coding

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66399

Quick and dirty way would be to just iterate the Form collection looking for proper values:

if (Page.IsPostBack)
{
    string name = "txtBoxcustom";
    foreach (string key in Request.Form.Keys)
    {
        int index = key.IndexOf(name);
        if (index >= 0)
        {
            int num = Int32.Parse(key.Substring(index + name.Length));
            string value = Request.Form[key];
            //store value of txtBoxcustom with that number to database...
        }
    }
}

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460028

You have to recreate the dynamical controls in Page_Load at the latest, otherwise the ViewState is not loaded correctly. You can however add a new dynamical control in an event handler(which happens after page_load in the page's lifefycle).

So addmoreCustom_Click is too late for the recreation of all already created controls, but it's not tool late to add a new control or to read the Text.

So something like this should work(untested):

public void Page_Load(object sender, EventArgs e)
{
    if (ViewState["addmoreEdu"] != null)
    {
        myCount = (int)ViewState["addmoreEdu"];
    }

    addControls(myCount);
}

public void addmoreCustom_Click(object sender, EventArgs e)
{
    if (ViewState["addmoreEdu"] != null)
    {
        myCount = (int)ViewState["addmoreEdu"];
    }
    myCount++;
    ViewState["addmoreEdu"] = myCount;

    addControls(1);
}

private void addControls(int count)
{
    int txtCount = myPlaceHolder.Controls.OfType<TextBox>().Count();
    for (int i = 0; i < count; i++)
    {
        TextBox txtboxcustom = new TextBox();
        Literal newlit = new Literal();
        newlit.Text = "<br /><br />";
        txtboxcustom.ID = "txtBoxcustom" + txtCount.ToString();
        myPlaceHolder.Controls.Add(txtboxcustom);
        myPlaceHolder.Controls.Add(newlit);
    }
}

Just enumerate the PlaceHolder-Controls to find your TextBoxes or use Linq:

private void saveData()
{
    foreach (TextBox txt in myPlaceHolder.Controls.OfType<TextBox>())
    {
        string text = txt.Text;
        // ...
    }
}

Upvotes: 1

Related Questions