Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
We have one ASP.net Web page. Based on the dropdown category selection, controls should be populated in the page. Controls vary from textbox, dropdownlist, chekbox list and so on. When save is clicked, these dynamically created controls values should be saved to the database.
The present issue is when you click insert, these controls are vanished in the postback and I am not able to get the values of these controls.
Prasad
Posted

1 solution

Try creating the Controls OnInit event.

Then you can overcome the control vanishing problem.

Provided you a sample code :

C#
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    if (ddlList.selectedValue = "your value")
    {
        CreateControls();
    }
    else
    {
        pnlContainer.Controls.Clear();
    }
}


CreateControls method will be used to create controls on your page.

Hope this helps.
 
Share this answer
 
Comments
PrasadRaoMangalore 1-Jun-12 9:16am    
Thank you Rajesh. I created :
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (ddlCategory.SelectedIndex > 0 )
{
CreateControls();
}

}

but this wont work.
PrasadRaoMangalore 2-Jun-12 1:33am    
We have many controls in web page. When we select a particular category, controls related to that will be displayed. There are many controls for each category. Also, there are many categories. So we dont know for what controls are displayed for which category. So, we thought Javascript will be useful as it will save the values when we enter the same in controls, which are dynamically displayed after selecting the categories. Can we achieve this functionality using javascript by saving the control values in client side.
We, need a general solution of saving the dynamic control value, when control id and name is unknown and also when there are multiple controls.
Rajesh Kariyavula 3-Jun-12 3:34am    
You can save the dynamic control values in JavaScript using JSON Objects and AJAX. But getting the dynamic control IDs in javascript is bit tricky.

If you have 10 TextBoxes, give IDs for each of them like textBox_1 and textBox_2...textBox_10.

In the the JavaScipt you can write a for loop and the get the controls like

document.getElementById('textBox_' + i);

But if you are using a master page or a div then all your Controls ID's will be preceded by then ContentPlaceHolder Id or DIV id. So the above approach will not work in this case. If you have the same problem, please let me know, I will provide a solution tomorrow.

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