I have to dynamically generate three textfields placed inside a panel.This text fields are displayed only when the approval column of three approvers are checked .
If any of the approver is not checked then the panel wont be displaying.
I want to dynamically generate the textfileds on a button click and want to save the data to the database.
You could use the Visible property of the panel to control whether it is displayed on the page.
And what is the approver you mentioned, is it CheckBox? If this is the case, you could determine and set whether the panel is displayed in the OnCheckedChanged event.
If you want to dynamically generate controls, you could choose where to place them, use something like:
Page.FindControl(position).Controls.Add(control);
Or you could define the control in advance and then use the display panel method to display it.
For more details, please refer to the following code:
Code behind:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked && CheckBox2.Checked && CheckBox3.Checked)
{
pnlPartnerDetails.Visible = true;
}
else
{
pnlPartnerDetails.Visible = false;
}
}
protected void imgBtnSalesChannelHead_Click(object sender, ImageClickEventArgs e)
{
//save data to database
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ToString()))
{
......
}
//generate the TextBox
TextBox box = new TextBox();
box.Attributes.Add("ID", "text1");
Page.FindControl("TextField1").Controls.Add(box);
}
Result:
If I misunderstood what you meant, please let me know.
Hope this can help you.
Best regards,
Xudong Peng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
I cannot hardcode the no:of textbox,if the user wants to add more than 5 textboxes ,then this wont work.
and also i want to generate the entire textboxes inside the panel,which means when i click on the add button it should generate the three textboxes - Partner Name,Partner Email,Partner Designation.
You don't need to hard code number to 5. That's why I have added it as a variable. You can get a number of textboxes from any sources. I believe you need a set of three text boxes and you need to set to be dynamic. Logic follows the same process-
It should be similar, instead of looping through and creating control, you need to add a set of controls on the click of a button. You can use the same List<YourDataClass> to hold all the dynamically added controls. Remember to add regenerate the controls
from init as dynamically created control will live through every postback
The code I have shared is the working code. You can run my application by cloning from git. I want to believe that you can copy the code and make the necessary changes to add to your project.
1)Whenever i change the for loop intialisation value to 1,the fields are not generating on the first click.When i click for the second time fields are generating.
i had made the changes by editing your working code only,my doubt is
private void GenerateControls()
{
int numberOfControls = Convert.ToInt32(Session["ControlGenerated"]);
container.Controls.Clear();
for (int i = 1; i < numberOfControls; i++)
{
}
}
in this for loop i have changed the value of int i=1,clicking for first time it wont generate the textfields,but when i click for the second time it will generate.
if i revert my change that is int i = 0,everything works fine.first click itself it will generate the textfileds with id 0.
and in your code ,the page first load with only Addnew and Getvalues link.
So i have changed it to my requirement.In my page at first it will have a panel with three textfields .then i click on the button it will add another panel.This is exactly what i need.
and in your code there is no option to remove the controls. thats what i asked as my second doubt.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Could you please help me to resolve this - whenever i add details in the textfields and click the add new button ,the previously entered details are removed.
Member
12 Points
140 Posts
Dynamic Generation of Text Fields and Saving the Values to database
Jul 15, 2020 02:10 AM|teenajohn1989|LINK
Hi,
I have to dynamically generate three textfields placed inside a panel.This text fields are displayed only when the approval column of three approvers are checked .
If any of the approver is not checked then the panel wont be displaying.
I want to dynamically generate the textfileds on a button click and want to save the data to the database.
Appreciate help on this.
This is the aspx code for panel.
Contributor
2120 Points
676 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 15, 2020 03:49 AM|XuDong Peng|LINK
Hi teenajohn1989,
You could use the Visible property of the panel to control whether it is displayed on the page.
And what is the approver you mentioned, is it CheckBox? If this is the case, you could determine and set whether the panel is displayed in the OnCheckedChanged event.
If you want to dynamically generate controls, you could choose where to place them, use something like:
Or you could define the control in advance and then use the display panel method to display it.
For more details, please refer to the following code:
Page code: <body> <form id="form1" runat="server"> <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"/>:approver1<br /> <asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"/>:approver2<br /> <asp:CheckBox ID="CheckBox3" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"/>:approver3<br /> <div> <asp:Panel ID="pnlPartnerDetails" runat="server" Visible="false"> <table> <!--Partners Name--> <tr> <td class="auto-style9" style="text-align: left;">Partners Name</td> <td class="auto-style12"> <asp:TextBox ID="txtPartnerName" ValidationGroup="editCustomer" runat="server"></asp:TextBox> </td> <td style="text-align: left;"> <asp:ImageButton ID="imgBtnSalesChannelHead" ImageUrl="~/images/plus.png" runat="server" OnClick="imgBtnSalesChannelHead_Click"/> </td> <td style="text-align: left;" runat="server" id="TextField1"></td> </tr> <!--Partners Email--> <tr> <td class="auto-style9" style="text-align: left;">Partners Email</td> <td class="auto-style12"> <asp:TextBox ID="txtPartnerEmail" ValidationGroup="editCustomer" runat="server"></asp:TextBox> </td> <td></td> <td style="text-align: left;"></td> </tr> <!--Partners Designation--> <tr> <td class="auto-style9" style="text-align: left;">Partners Designation</td> <td class="auto-style12"> <asp:TextBox ID="txtPartnersDesignation" ValidationGroup="editCustomer" runat="server"></asp:TextBox> </td> <td></td> <td style="text-align: left;"></td> </tr> </table> </asp:Panel> </div> </form> </body>
Code behind: protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { if (CheckBox1.Checked && CheckBox2.Checked && CheckBox3.Checked) { pnlPartnerDetails.Visible = true; } else { pnlPartnerDetails.Visible = false; } } protected void imgBtnSalesChannelHead_Click(object sender, ImageClickEventArgs e) { //save data to database using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ToString())) { ...... } //generate the TextBox TextBox box = new TextBox(); box.Attributes.Add("ID", "text1"); Page.FindControl("TextField1").Controls.Add(box); }
Result:
If I misunderstood what you meant, please let me know.
Hope this can help you.
Best regards,
Xudong Peng
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 15, 2020 02:34 PM|teenajohn1989|LINK
Hi XuDong Peng,
Thanks for your reply.
I managed to display the textfields based on the approval.What i need is , i want to create three textfields on a single click.
that means replicating all the three textfields inside the panel.
i want to generate as many textfields,not just one .
Hope it is clear.Thanks in advance.
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 15, 2020 02:45 PM|asteranup|LINK
you can simply try-
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 15, 2020 03:15 PM|teenajohn1989|LINK
Hi asteranup,
I cannot hardcode the no:of textbox,if the user wants to add more than 5 textboxes ,then this wont work.
and also i want to generate the entire textboxes inside the panel,which means when i click on the add button it should generate the three textboxes - Partner Name,Partner Email,Partner Designation.
Thanks in advance
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 15, 2020 07:11 PM|asteranup|LINK
You don't need to hard code number to 5. That's why I have added it as a variable. You can get a number of textboxes from any sources. I believe you need a set of three text boxes and you need to set to be dynamic. Logic follows the same process-
You can check working code here-
https://github.com/anupdg/dotnetsamples/blob/master/WebFormSamples/WebFormSamples/2169001-Dynamic%2BGeneration%2Bof%2BText%2BFields%2Band%2BSaving%2Bthe%2BValues%2Bto%2Bdatabase.aspx.cs
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 16, 2020 08:14 AM|teenajohn1989|LINK
Hi asteranup,
Thanks for your response.
I tried your code.It works !But my requirement is different.
Please check this screenshot
There wont be any sources to get the no:of textboxes to be generated. When i click on the plus button it has to generate the next set.
Hope this is clear.
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 16, 2020 09:06 AM|asteranup|LINK
It should be similar, instead of looping through and creating control, you need to add a set of controls on the click of a button. You can use the same List<YourDataClass> to hold all the dynamically added controls. Remember to add regenerate the controls from init as dynamically created control will live through every postback
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 16, 2020 09:56 AM|teenajohn1989|LINK
Hi asteranup,
Im didnt get what you are saying.Could you please explain a bit more,i'm new to this technology.
How i can set this txtNimberOfGroup.Text value.Because this value is using for the loop.
Thanks in advance!
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 16, 2020 11:10 AM|asteranup|LINK
What I mean is this-
https://github.com/anupdg/dotnetsamples/blob/master/WebFormSamples/WebFormSamples/2169001-Dynamic%2BGeneration%2Bof%2BText%2BFields%2Band%2BSaving%2Bthe%2BValues%2Bto%2Bdatabase1.aspx.cs
You can run and see.
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 16, 2020 11:23 AM|teenajohn1989|LINK
Hi asteranup,
I tried out ,when i click on the add button,the whole content is gone and its dispalying empty page.
Here is my code,i made some changes.
Here is my aspx.cs file
Couldnt figure out why its displaying a blank page on button click.
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 16, 2020 12:05 PM|asteranup|LINK
The code I have shared is the working code. You can run my application by cloning from git. I want to believe that you can copy the code and make the necessary changes to add to your project.
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 17, 2020 04:41 AM|teenajohn1989|LINK
Hi asteranup,
I managed to get it done.
i have few doubts ,
1)Whenever i change the for loop intialisation value to 1,the fields are not generating on the first click.When i click for the second time fields are generating.
Webform1.aspx
Webform1.aspx.cs
2)how can i remove the control dynamically? Please see this screenshot
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 17, 2020 04:52 AM|asteranup|LINK
Hi,
I have already shared a working code. Please clone the repo to your local, run and debug, you can easily find out what you are missing.
https://github.com/anupdg/dotnetsamples/blob/master/WebFormSamples/WebFormSamples/2169001-Dynamic%2BGeneration%2Bof%2BText%2BFields%2Band%2BSaving%2Bthe%2BValues%2Bto%2Bdatabase1.aspx.cs
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 17, 2020 06:01 AM|teenajohn1989|LINK
Hi asteranup,
Yes,your code is working.
i had made the changes by editing your working code only,my doubt is
in this for loop i have changed the value of int i=1,clicking for first time it wont generate the textfields,but when i click for the second time it will generate.
if i revert my change that is int i = 0,everything works fine.first click itself it will generate the textfileds with id 0.
and in your code ,the page first load with only Addnew and Getvalues link.
So i have changed it to my requirement.In my page at first it will have a panel with three textfields .then i click on the button it will add another panel.This is exactly what i need.
and in your code there is no option to remove the controls. thats what i asked as my second doubt.
Thanks for your kind and quick responses
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 17, 2020 06:31 AM|asteranup|LINK
Don't change the index. Instead of that add the following code-
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 17, 2020 07:57 AM|teenajohn1989|LINK
Hi asteranup,
Thank you so much :) . Finally i got it done. :)
One more help i need,for deleting the dynamic text field generated i have wriiten the following code
But when i click on the remove button.its deleting the entire textfields generated dynamically.
Could you please help me to figure out the issue?
Contributor
2120 Points
676 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 17, 2020 09:34 AM|XuDong Peng|LINK
Hi teenajhn1989,
According to your description above, I created a simple example to meet your needs.
For more details, please refer to the following code:
Result:
Best regards,
Xudong Peng
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 17, 2020 09:49 AM|teenajohn1989|LINK
Hi XuDong Peng,
Thanks for your reply .
In your demo,its not deleting the correct index.
when you click delete button for the second row,its deleting third row.
and i want to implement this without using jquery .
Thanks again for your quick response .
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 17, 2020 11:12 AM|asteranup|LINK
You can add link button also as a part of each rows. Done some changes on the existing code you can check below-
https://github.com/anupdg/dotnetsamples/blob/master/WebFormSamples/WebFormSamples/2169001-Dynamic%2BGeneration%2Bof%2BText%2BFields%2Band%2BSaving%2Bthe%2BValues%2Bto%2Bdatabase1.aspx.cs
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 18, 2020 04:28 PM|teenajohn1989|LINK
Hi asteranup,
Thanks for your reply.The delete button is working fine.
But i have one more issue,when the page loads,it loads with the generated textfields with deletebutton,even if i didnt click the add new button.
Please see this screenshot
And whenever i add details in the textfields and click the add new button ,the previously entered details are removed.
my sincere thanks for your effort.
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 20, 2020 07:11 AM|teenajohn1989|LINK
Hi astreanup,
Could you please help me to resolve this - whenever i add details in the textfields and click the add new button ,the previously entered details are removed.
Thanks in advance
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 20, 2020 05:41 PM|asteranup|LINK
Change -
to-
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 20, 2020 06:08 PM|asteranup|LINK
As subsequent postback data gets lost for a dynamic control. You need to retain data (in session) while adding new control-
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 21, 2020 02:28 AM|teenajohn1989|LINK
Hi asteranup,
Thank you so much ,I have one request,can you please make a tutorial for this in your blog.It would be more helpful for beginners like me.
I came to know about dictionary concepts from your coding only ,thank you so much for all the help.
Member
12 Points
140 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 21, 2020 02:31 AM|teenajohn1989|LINK
Hi ateranup,
i have changed the code as you mentioned,
Change -
to-
but getting this error System.InvalidOperationException: 'Sequence contains no elements' when i click on the add button.
Thanks in Advance!
All-Star
20953 Points
4984 Posts
Re: Dynamic Generation of Text Fields and Saving the Values to database
Jul 21, 2020 09:46 AM|asteranup|LINK
Please debug and compare.
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn