Comment posted by
Brett Spencer
on Wednesday, April 16, 2008 9:05 PM
|
|
I have used this code over and over to try and get the bugs out but no matter what I do, I can not get the page to retain the controls after clicking on the "cause postback" button. Also, I had to create an empty function for the buttonclick for button1 as I got an error first time around without doing so. After reading the "Understanding ASP.NET View State" reference that was mentioned. It seems to me that the controls should be rebuilt before the viewstate can load the saved information into them - somewhere in the page initialize stage.
I do not pretend to be an expert and if anything, I would call myself a beginner, so if I am missing some understanding to this process and the reason why all of the controls and information disapears when click on the "causes postback" button, I would appreciate a clearer discussion on exactly where the controls are being rebuilt as well as when the data (or state) is placed back into them.
|
Comment posted by
Brett Spencer
on Thursday, April 17, 2008 7:35 PM
|
|
I found the other error in this code...
Protected Overrides Sub LoadViewState(ByVal earlierState As Object)
MyBase.LoadViewState(earlierState)
If ViewState("dynamictable") Is Nothing Then
CreateDynamicTable()
End If
End Sub
This should say:
If Not ViewState("dynamictable") Is Nothing Then
CreateDynamicTable()
End If
This allows for the controls to be rebuilt and loads the saved information from viewstate back into the controls.
I tried to build the controls in OnInit() but like this articles states, it is very difficult to get the user input as a variable into the procedure in order to build the right amount of rows and columns.
|
Comment posted by
Suprotim Agarwal
on Friday, April 18, 2008 12:40 PM
|
|
Hi Brett..Thanks for the comment and pointing out the typo.You need to add the CreateDynamicTable() in the Page_Load(). I had missed it out while writing the article. I have now added the method. You do not have to change anything else.
|
Comment posted by
bhavana
on Wednesday, April 23, 2008 5:00 AM
|
|
I try to run this code in .net 2.0 with master page. but its not retaining postback value in dynamicaly created control.
|
Comment posted by
DataBasic
on Wednesday, May 14, 2008 10:30 AM
|
|
For master pages I added 'Handles Me.Load' to the Page_Load event as the following vb.net shows:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
|
Comment posted by
y2kstephen
on Sunday, May 18, 2008 12:30 AM
|
|
thx a lot! this is exactly what i am looking for!
but i got a question...
after generate the rows and input something
the textboxes always rollback to default value for the FIRST postback click
but its all fine for the second or more postback clicks, the changes remained
did i do something wrong?
|
Comment posted by
y2kstephen
on Sunday, May 18, 2008 12:36 AM
|
|
thx a lot! this is exactly what i am looking for!
but i got a question...
after generate the rows and input something
the textboxes always rollback to default value for the FIRST postback click
but its all fine for the second or more postback clicks, the changes remained
did i do something wrong?
|
Comment posted by
Suprotim Agarwal
on Monday, May 19, 2008 9:16 PM
|
|
Hi,
Well that shouldn't be happening. The sample posted takes care of that.
|
Comment posted by
Mike
on Friday, May 30, 2008 6:37 PM
|
|
y2kstephen: the problem is that "ProcessPostData Second Try" fires after the Page Load on postback and resets the viewstate tracked values for the controls created during page load. See http://yakkowarner.blogspot.com/2008/01/aspnet-dynamic-controls-and-viewstate.html
|
Comment posted by
Francois Ward
on Monday, June 2, 2008 1:06 PM
|
|
After spending much of the dawn of ASP.NET having to pull dynamically created controls, with all the issues that come with it (if you need viewstate, you have to create them in Init...but if you NEED viewstate, you need to make em on load, but then no viewstate!... exactly what this article tries to adress), I've finally come to the conclusion...
The best way to create controls dynamically on the page is to..not do it. Instead, make a custom CompositeControl, override CreateChildControl, have the properties of your control at the top level (with viewstate backing), and when a property change, set ChildControlsCreated to false, and you're set.
Fact is, -when- controls must be created or recreated "depends", and its what confuse people... what info to save, when to read it, when to create controls, what would be the consequences if I need to change the control structure... the CompositeControl base class handles all of that in a quite elegant way, and keeps your code behind cleaner. The first one is tricky to make, but once you get the philosophy behind it, everything purrs along...
|
Comment posted by
CR
on Monday, June 2, 2008 1:39 PM
|
|
Francois: That sounds interesting. Do you have any code examples of doing that?
|
Comment posted by
Maan
on Saturday, June 7, 2008 4:26 AM
|
|
Very cool. In each cell you had created textbox. What if we can give control to user to specify that what they require in a row? I think it will be more interesting...
|
Comment posted by
Ehsan Moezzi
on Monday, June 9, 2008 8:37 AM
|
|
Thank you to give it help
|
Comment posted by
vinitha
on Wednesday, June 11, 2008 8:05 AM
|
|
Hi, Good sample.
But this works only for the first time. when i again click the button the controls are not generated again. My requirement is to generate controls as many times as user clicks on the button. Please do help me in doing this.
Thanks in advance.
|
Comment posted by
6
on Tuesday, July 29, 2008 5:57 AM
|
|
a
|
Comment posted by
Zeeshan Raees
on Monday, August 18, 2008 5:32 AM
|
|
Nice Article thx buddy :)
|
Comment posted by
meeran
on Monday, September 15, 2008 4:50 PM
|
|
peace be upon u,
sir,
i have a doubt..
i need to add and remove table dynamically.and need to do cleint and server side validation for that.
my model is
https://20db.banner-admin.com/order/configure
how they do that.
i am a trainee in web design.i try to do like that.i not know how they did.pls teach that to me.
thanks
yours truely...
meeran...
|
Comment posted by
Faheem Sial
on Tuesday, September 16, 2008 5:40 AM
|
|
Hi,This code was realy so helpfull for me thanx dear, could u plz guide me to access these controls values i placed a DropDownList into cell how could i access its selected index.Regards
|
Comment posted by
Suprotim Agarwal
on Tuesday, September 16, 2008 9:14 AM
|
|
meeran: Are you referring to the code when you select the banners and click continue. If yes, then create a user control with the required fields and add the user control dynamically for the no. of banners selected. I choose user controls since the banner content is the same throughout which will result in reusability
Faheem: You would need to loop through the rows and column collection of the dynamically created table. You can use FindControl from the cell or try out the cell's controls collection. I hope you have addded the DDL with an ID.
|
Comment posted by
madankumar
on Thursday, September 25, 2008 4:36 AM
|
|
Create a Table Dynamically in ASP.NET 2.0 and read excel value and pass the values into table .After editing store it to DB. Get the values from DB and pass it table again.
|
Comment posted by
John K
on Friday, November 7, 2008 6:32 PM
|
|
This is a great article and it help me out so much. I am new at this and was wondering about how I can change the size of the text box's and also take the values that were inputed in the text and submit them to a SQL Database.
I know how to submit them to a SQL database, but just not what the values are. For example, textbox1.text would be the value entered in the text box.
|
Comment posted by
John K
on Friday, November 7, 2008 6:38 PM
|
|
This is a great article and it help me out so much. I am new at this and was wondering about how I can change the size of the text box's and also take the values that were inputed in the text and submit them to a SQL Database.
I know how to submit them to a SQL database, but just not what the values are. For example, textbox1.text would be the value entered in the text box.
|
Comment posted by
John K
on Monday, November 10, 2008 9:51 AM
|
|
This is a great article and it help me out so much. I am new at this and was wondering about how I can change the size of the text box's and also take the values that were inputed in the text and submit them to a SQL Database.
I know how to submit them to a SQL database, but just not what the values are. For example, textbox1.text would be the value entered in the text box.
|
Comment posted by
John K
on Monday, November 10, 2008 11:39 AM
|
|
This is a great article and it help me out so much. I am new at this and was wondering about how I can change the size of the text box's and also take the values that were inputed in the text and submit them to a SQL Database.
I know how to submit them to a SQL database, but just not what the values are. For example, textbox1.text would be the value entered in the text box.
|
Comment posted by
Tony
on Monday, November 10, 2008 12:38 PM
|
|
Hmm, well I was able to get this code working only after removing CreateDynamicTable() from Page_Load() and putting it in OnInit(), also had to set the ID for each TextBox otherwise the value wouldn't always appear in the correct TextBox upon the restore of the values.
|
Comment posted by
Suprotim Agarwal
on Tuesday, November 11, 2008 11:56 AM
|
|
John: There are multiple ways to update values to the db.
One could be by using ExecuteNonQuery on the command object or if you are using a datasource control like SQLDataSource to fetch records, you can use the same to update values from the textbox as well
<asp:SqlDataSource ID="SqlDataSource1" runat="server" InsertCommand="INSERT INTO Customers(Name, Age) VALUES ( @Param1 , @Param2 )"
ConnectionString="<%$ ConnectionStrings:YourConnStringName %>">
<InsertParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="" PropertyName="Text" Name="Param1" />
<asp:ControlParameter ControlID="TextBox2" DefaultValue="" PropertyName="Text" Name="Param2" />
</InsertParameters>
</asp:SqlDataSource>
Then on the button click, call SQLDataSource1.Insert()
Tony: I have used the Page_Load for a specific purpose and have also described it in the article. Thanks for your comment though.
|
Comment posted by
Manoj
on Monday, December 29, 2008 3:32 AM
|
|
How to save the values while entering the data in dynamic table
|
Comment posted by
Manoj
on Monday, December 29, 2008 4:53 AM
|
|
How to assign a label for place holder
|
Comment posted by
Manoj
on Wednesday, December 31, 2008 4:14 AM
|
|
How to get the value from placeholder and save into the database
|
Comment posted by
Rahul Shinde
on Tuesday, January 13, 2009 5:57 AM
|
|
it is great it will help me lots
|
Comment posted by
mathan
on Thursday, January 22, 2009 5:19 AM
|
|
if any one know plz send me
|
Comment posted by
prabhat22
on Wednesday, February 11, 2009 6:34 AM
|
|
hi
I have created table using above method.
but now iam facing problem to store the values to database.
how to accept the values from dyanmictable and send it to database.
|
Comment posted by
hardik patel
on Thursday, April 9, 2009 2:45 AM
|
|
I have used this code over and over to try and get the bugs out but no matter what I do, I can not get the page to retain the controls after clicking on the "cause postback" button. Also, I had to create an empty function for the buttonclick for button1 as I got an error first time around without doing
|
Comment posted by
Than Than Aye
on Thursday, April 30, 2009 5:23 AM
|
|
I can not run.Errors occur CreateDynamicTabel().
|
Comment posted by
Shrikrishna
on Friday, May 8, 2009 6:51 AM
|
|
I have used this code for generating table rows dinamically.
Now i want to retrive the table values and want to insert that values into the database. how can i get all the values when am clicking save button ? Help me plz....
|
Comment posted by
ynnorj
on Friday, June 12, 2009 4:10 AM
|
|
I also have a problem retrieving values per textbox. But the textbox created are not <asp:textbox> (or are they?);
and if I pull up the data from each textbox, I also need to assign each a different Id. But if I assign them IDs, the Ids wouldn't be recognized as well because they are set as local variables.
Aren't there no textbox arrays where we could just simply search for the variablename[index]? Thank you.
|
Comment posted by
Aarathy Kulathunkal
on Thursday, July 23, 2009 12:47 AM
|
|
It was very useful for me. I could create dynamic table...Thank you soooo much.
|
Comment posted by
DST
on Wednesday, August 5, 2009 2:54 PM
|
|
Hi, it's very useful and I was able to create a dynamic table & keeping it's earlier state using the 'LoadViewState'....But 'Page.PreviousPage' is not working when I call thru the LoadViewState. Pls check below code and let me know what I am doing wrong or way to fix it !!!
------------------------------------------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
CreateDynamicTable() '--- It's working fine at page load...
End If
end sub
----------------------------------------------------------------------------
Protected Overrides Sub LoadViewState(ByVal earlierState As Object)
MyBase.LoadViewState(earlierState)
If Not ViewState("dynamictable") Is Nothing Then
CreateDynamicTable() '--- It's failed to find a previous page...
End If
End Sub
--------------------------------------
Protected Sub CreateDynamicTable()
Dim previousPageTable as Table = CType(Page.PreviousPage.FindControl("TBL"), Table)
'It's not finding the previouspage when this method was called from 'LoadViewState'.
....
....
....
ViewState("dynamictable") = True
end sub
|
Comment posted by
Sinu
on Thursday, October 15, 2009 3:31 AM
|
|
How can i insert the data in the dynamic table to sql server table using c# please help thanx in advance
|
Comment posted by
msk
on Tuesday, October 27, 2009 12:57 PM
|
|
Hi, thanks for this article, can you please tell me how to reterive the data from each textbox , I want to store back in the database..
Thanks
MSK
|
Comment posted by
Bhavan
on Tuesday, November 24, 2009 1:32 AM
|
|
Hi, Thanks for the article. But as I am newbie, unable to run the codes successfully and getting the 'Button1_Click' is not a member of 'ASP.default_aspx' error message. I found that nothing is there in the article for button1_click event. Please provide the code so that the created table to display on the page.
Thanks, Bhavan
|
Comment posted by
Suprotim Agarwal
on Tuesday, November 24, 2009 4:36 AM
|
|
Bhavan: Just double click on the Button and it will generate the code behind for the button click. It's there to simply cause a postback.
|
Comment posted by
Bhavan
on Tuesday, November 24, 2009 6:28 AM
|
|
Hi Suprotim,
Thanks for your immediate reply.
But I didn't get any result while giving any values and clicking on generate table. Even nothing returned clicking on cause postback. Please suggest.
Bhavan
|
Comment posted by
chaithu
on Thursday, December 10, 2009 2:31 AM
|
|
Thanks for your immediate reply.
But I didn't get any result while giving any values and clicking on generate table. Even nothing returned clicking on cause postback. Please
|
Comment posted by
Luca
on Monday, December 14, 2009 11:59 AM
|
|
hi Suprotim, Thanks for the article, it's been really helpfull. Unfortunatelly i have the same trouble as y2kstephen : "the textboxes always rollback to default value for the FIRST postback click
but its all fine for the second or more postback clicks, the changes remained ". What should i do with it?? You answered him that the code should take care of that case too but i can't solve the problem.... Please suggest.
|
Comment posted by
vikky
on Thursday, January 28, 2010 5:06 AM
|
|
Good,but my question is that how to save dynamic generated table in to database.
|
Comment posted by
fazail
on Tuesday, February 2, 2010 4:50 AM
|
|
i have used it butr iit is uncompled
|
Comment posted by
Jafar
on Tuesday, February 2, 2010 7:02 AM
|
|
It’s a good program; however I am interested to find out how to transfer these tables into a database “as per previous commenter”.
|
Comment posted by
Dharmeshwari
on Wednesday, February 10, 2010 11:56 PM
|
|
This article is very good for understanding both the concept and as well as the implementation. I worked it out. Also i was struggling for this code in the net for long time... it helped me a lot... Thakn u so much....
|
Comment posted by
simflex
on Wednesday, March 10, 2010 1:30 PM
|
|
This is an absolute mastermind. I looked for this solution for a long time till now.
Thank you very, very much.
I just have one question though. If I don't want the text on the boxes after they are created like: RowNo:0 ColumnNo: 0, RowNo:0 ColumnNo: 1,RowNo:0 ColumnNo: 2..., how do I get rid of them?
Again, great work; thanks
|
Comment posted by
simflex
on Wednesday, March 10, 2010 2:07 PM
|
|
This is an absolute mastermind. I looked for this solution for a long time till now.
Thank you very, very much.
I just have one question though. If I don't want the text on the boxes after they are created like: RowNo:0 ColumnNo: 0, RowNo:0 ColumnNo: 1,RowNo:0 ColumnNo: 2..., how do I get rid of them?
Again, great work; thanks
|
Comment posted by
Simflex
on Tuesday, March 16, 2010 11:48 AM
|
|
Does anyone know how to *not* show those text on the boxes once the columns and rows are created?
As you know once the number of rows and columns are created, you see text such as RowNo:0 ColumnNo: 0; RowNo:0 ColumnNo: 1; RowNo:0 ColumnNo: 2, etc.
I want to either hide them or remove them from displaying on the cells.
Thanks a for quick reponse on this.
|
Comment posted by
Sudipto
on Wednesday, March 17, 2010 2:58 AM
|
|
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
this.Rows = Int32.Parse(TextBox1.Text);
//I am getting "System.FormatException: Input string was not in a correct format." on the above line
}
generate_table(); //my version of CreateDynamicTable()
}
I am using your code in a content page which has a page template.
|
Comment posted by
Naveen
on Tuesday, April 20, 2010 5:54 AM
|
|
How to get these text box values
|
Comment posted by
surzo4368
on Monday, May 3, 2010 4:18 AM
|
|
i try this but table not generated just page refresh. help...
|
Comment posted by
zhugw519
on Wednesday, May 12, 2010 3:35 AM
|
|
It's very useful to me,thanks a lot!
|
Comment posted by
jia
on Saturday, June 5, 2010 7:10 AM
|
|
I want to save the contents of a webpage in a database table by using a Htmleditor. Contents of htmleditor are saved in database in form of htmlcode. I am able to successfully save data in database but i am unable to retrieve the saved data back in form of designed webpage.
The data on this page is retrieve from database table field. Table has fields like id, title, detail. title field has title of a page, detail field has complete detail of a web page which is save in database in the form of htmlcode. The problem is that how a web page is designed from this, how I am show these contents in detail section of a asp.net masterpage.
I am using sqlserver and asp.net with vb not C#.
Anyone help me.
Thanks in advance
|
Comment posted by
tejender
on Friday, June 11, 2010 7:12 AM
|
|
nice
|
Comment posted by
Saif
on Wednesday, June 16, 2010 3:06 AM
|
|
You have done a nice work! thanks for sharing, i just wanted to know that how could i get the values of the text boxes after clicking the Cause Postback button suppose i inserted data into the text boxes????
Please i need your urgent help!
regards
|
Comment posted by
Abhinesh Rao
on Monday, June 28, 2010 6:14 PM
|
|
Hi Suprotim,
I just wanted to know how do I create the tables dynamically, when I enter a number either in the ROW or the COLUMN field it starts loading and then I am not able to enter value in the other field(ROW/COLUMN) which results in a null exception.
Thanks for your kind help
|
Comment posted by
Prashant
on Monday, July 19, 2010 6:59 AM
|
|
Hi Suprotim,
Thanks for this great article i wanted the exact code ,but i wasn't successful in getting the result as u said, i'm having problem maintaining the viewstate ,once clicked on cause post back the textboxes return to their default value.
Hope you reply soon
Thanks & Regards
|
Comment posted by
Camilo Torregroza
on Wednesday, July 21, 2010 4:53 PM
|
|
Hi thank you for your post.
I'm trying to add a dropdownlist, a textbox and a button the dropdownlist will display some products list, and the button it's for delete the entire row.
also I have a button to add new rows to the table but it's out of the table.
when I'll try to add more than twice a row i had an exception:
I'll appreciate if you can help me with this. thank you.
this is my code if you could help me.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ControlAgregarProducto.ascx.cs"
Inherits="ClienteWeb.Controls.ControlAgregarProducto" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<style type="text/css">
.style1
{
height: 23px;
width: 157px;
}
.style2
{
width: 157px;
}
.style3
{
height: 23px;
width: 104px;
}
.style4
{
width: 104px;
}
</style>
<%--<asp:UpdatePanel>
<ContentTemplate>--%>
<table style="width: 100%" title="Stems">
<tr>
<td class="style1">
</td>
<td class="style3">
</td>
<td style="height: 23px; width: 263px">
</td>
<td style="height: 23px; width: 263px">
</td>
</tr>
<tr>
<td class="style2">
Product
</td>
<td class="style4">
</td>
<td style="width: 263px">
Quantity
</td>
<td style="width: 263px">
</td>
</tr>
<tr>
<td colspan="4">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style4">
</td>
<td style="width: 263px">
</td>
<td style="width: 263px">
</td>
</tr>
<tr>
<td class="style2">
<strong>Total</strong>
</td>
<td class="style4">
</td>
<td style="width: 263px">
[<asp:Button ID="btnAdd" runat="server" BackColor="White" BorderColor="White" BorderStyle="None"
Text="Add" Font-Bold="True" Height="15px" OnClick="btnAdd_Click" />
]
</td>
<td style="width: 263px">
</td>
</tr>
</table>
<%-- </ContentTemplate>
</asp:UpdatePanel>--%>
___________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FF.ClienteWebBL;
namespace ClienteWeb.Controls
{
public partial class ControlAgregarProducto : System.Web.UI.UserControl
{
protected int Rows
{
get
{
return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
}
set
{
ViewState["Rows"] = value;
}
}
protected int Columns
{
get
{
return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
}
set
{
ViewState["Columns"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
// Run only once a postback has occured
if (!Page.IsPostBack)
{
//Set the Rows and Columns property with the value
//entered by the user in the respective textboxes
this.Rows = 1;
this.Columns = 3;
this.EnableViewState = true;
}
CreateDynamicTable();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
this.Rows += 1;
CreateDynamicTable();
}
private void CreateDynamicTable()
{
PlaceHolder1.Controls.Clear();
// Fetch the number of Rows and Columns for the table
// using the properties
int tblRows = Rows;
int tblCols = Columns;
// Create a Table and set its properties
Table tbl = new Table();
// Add the table to the placeholder control
PlaceHolder1.Controls.Add(tbl);
// Now iterate through the table and add your controls
ProductoFacade facadeProducto = new ProductoFacade();
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
TableCell tCell1 = new TableCell();
DropDownList ddlProducto = new DropDownList();
ddlProducto.DataSource = facadeProducto.GetEspecies();
ddlProducto.DataTextField = "Nombreingles";
ddlProducto.DataValueField = "Idespecie";
ddlProducto.DataBind();
tCell1.Controls.Add(ddlProducto);
TableCell tEmptyCell2 = new TableCell();
TableCell tCell3 = new TableCell();
TextBox txtQuantity = new TextBox();
TableCell tCell4 = new TableCell();
Button btnDeleteProducto = new Button();
//btnDeleteProducto.BorderStyle = BorderStyle.None;
//btnDeleteProducto.BorderColor = System.Drawing.Color.White;
//btnDeleteProducto.Text = "Delete";
//btnDeleteProducto.Click += new EventHandler(btnDeleteProducto_Click);
tCell3.Controls.AddAt(0,txtQuantity);
tCell4.Controls.AddAt(0,btnDeleteProducto);
tr.Cells.Add(tCell1);
tr.Cells.Add(tEmptyCell2);
tr.Cells.Add(tCell3);
tr.Cells.Add(tCell4);
tbl.Rows.Add(tr);
}
// This parameter helps determine in the LoadViewState event,
// whether to recreate the dynamic controls or not
ViewState["dynamictable"] = true;
}
void btnDeleteProducto_Click(object sender, EventArgs e)
{
Control boton = (Control)sender;
}
protected override void LoadViewState(object earlierState)
{
base.LoadViewState(earlierState);
if (ViewState["dynamictable"] == null)
CreateDynamicTable();
}
}
}
The exception is :
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
I hope you could help mw with this..
|
Comment posted by
gourav
on Friday, October 1, 2010 9:22 PM
|
|
hiiiii i am trying to connect a table from database to my web form,
can u please help me,
i am not able to display the table in the form
|
Comment posted by
DD
on Tuesday, October 19, 2010 5:09 AM
|
|
Hi, Can you tell me how to retrive textbox values if I am dynamically creating a table that contains a textbox in each cell( textbox are also dynamically created). The table is put in a placeholder control. I am using above createDynamicTable() for creating my table ? plz rply its urgent .....Thanks in advance.
|
Comment posted by
erum
on Friday, November 19, 2010 4:53 AM
|
|
i created div like this
Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
Dim index As Integer
For index = 1 To RadioButtonList1.SelectedValue
Dim newdiv As New HtmlGenericControl("div")
newdiv.ID = "mydiv" & index
'newdiv.Attributes.Add("id", "mydiv")
newdiv.EnableViewState = True
Dim ht As New HtmlTable()
ht.ID = "mytable" & index.ToString()
ht.EnableViewState = True
ht.Width = "100%"
ht.CellSpacing = "0"
ht.CellPadding = "0"
ht.Border = 0
Dim htr, htr1, htr2 As New HtmlTableRow()
Dim cell, htc, htc1, htc2, htc3, htc4, htc5, htc6, htc7, htc8 As New HtmlTableCell()
Dim name_tb As New TextBox()
Dim sal_ddl As New DropDownList
'validator
Dim name_req As New RequiredFieldValidator
Dim tel_req As New RequiredFieldValidator
Dim email_req As New RequiredFieldValidator
name_tb.ID = "name_tb" & index
sal_ddl.ID = "sal_ddl" & index
name_req.ID = "name_req" & index
tel_req.ID = "tel_req" & index
email_req.ID = "email_req" & index
name_tb.EnableViewState = True
sal_ddl.EnableViewState = True
tel_req.EnableViewState = True
sal_ddl.Items.Add("MR")
sal_ddl.Items.Add("Ms")
sal_ddl.Items.Add("Mrs")
Dim Tel_tb1 As New TextBox
Tel_tb1.ID = "Tel_tb1" & index
Tel_tb1.EnableViewState = True
name_tb.TextMode = TextBoxMode.SingleLine
Dim email_tb1 As New TextBox
email_tb1.ID = "email_tb1" & index
email_tb1.TextMode = TextBoxMode.SingleLine
htc.InnerText = "Name : "
htc.Width = "20%"
htc.Align = "Left"
htc1.Controls.Add(sal_ddl)
htc1.Width = "10%"
htc2.Width = "70%"
htc2.Align = "left"
htc2.Controls.Add(name_tb)
htc2.Controls.Add(name_req)
htr.Controls.Add(htc)
htr.Controls.Add(htc1)
htr.Controls.Add(htc2)
ht.Controls.Add(htr)
name_req.ControlToValidate = "name_tb" & index
name_req.Display = ValidatorDisplay.Dynamic
name_req.ErrorMessage = ("Please Enter name of attendenes")
'make 2nd row
htc3.InnerText = "Telephone : "
htc3.Width = "20%"
htc3.Align = "Left"
htc4.InnerHtml = "<strong></strong>"
htc4.Width = "10%"
htc5.Width = "70%"
htc5.Align = "left"
htc5.Controls.Add(Tel_tb1)
htc5.Controls.Add(tel_req)
htr1.Controls.Add(htc3)
htr1.Controls.Add(htc4)
htr1.Controls.Add(htc5)
ht.Controls.Add(htr1)
tel_req.ControlToValidate = "Tel_tb1" & index
tel_req.Display = ValidatorDisplay.Dynamic
tel_req.ErrorMessage = ("Please Enter Phone")
'make 3rd row
htc6.InnerText = "Email : "
htc6.Width = "20%"
htc6.Align = "Left"
htc7.InnerHtml = "<strong></strong>"
htc7.Width = "10%"
htc8.Width = "70%"
htc8.Align = "left"
htc8.Controls.Add(email_tb1)
htc8.Controls.Add(email_req)
htr2.Controls.Add(htc6)
htr2.Controls.Add(htc7)
htr2.Controls.Add(htc8)
ht.Controls.Add(htr2)
email_req.ControlToValidate = "email_tb1" & index
email_req.Display = ValidatorDisplay.Dynamic
email_req.ErrorMessage = ("Please Enter Email")
'Add a blank rows
cell.ColSpan = 3
cell.InnerHtml = "<br/>"
Dim row As New HtmlTableRow
row.Cells.Add(cell)
ht.Rows.Add(row)
newdiv.Controls.Add(ht)
newdiv.Attributes.Add("table", "ht")
newdiv.Attributes.Add("Style", "border-bottom :#ccc solid 0px")
newdiv.Attributes.Add("style", "height:23px")
newdiv.Attributes.Add("style", "padding-left:20px")
newdiv.EnableViewState = True
ViewState("control") = "newdiv"
Me.PlaceHolder1.Controls.Add(newdiv)
PlaceHolder1.EnableViewState = True
Next
ViewState("count") = RadioButtonList1.SelectedValue
'ViewState("control") = Me.PlaceHolder1
'End Sub
End Sub
i created controls and div dynamically on post back in loop,and then i need to get the controls and div on submit button
i put all these controls and div in placeholders while creating them.
submit button()
for i =0 i< 3 ;i++
// i got placeholder that i have on .aspx page
// need help here for div and dynamic controls
next
end sub
|
Comment posted by
Lalit Sharma
on Monday, November 29, 2010 6:07 AM
|
|
It was a good article and thanks for sharing.
|
Comment posted by
Mark Fowler
on Tuesday, January 4, 2011 12:56 AM
|
|
I am not sure why are developers facing an error. Just d/l the source code and run it. I was able to test the example successfully! Oh Devs!
|
Comment posted by
B.Sharmila
on Friday, January 7, 2011 10:12 PM
|
|
My project is i've to get the column names from the textbox and on the click of the button have to create table dynamically and store in the database after tat edition,deletion should be possible.Please help me for tis and mail me soon.
|
Comment posted by
B.Sharmila
on Friday, January 7, 2011 10:14 PM
|
|
how to create textbox on click of the button dynamically in vb.net
|
Comment posted by
B.Sharmila
on Friday, January 7, 2011 10:17 PM
|
|
get the table name in a textbox and display that table structure(column name and datatype) from the database in the concole using swings.
|
Comment posted by
Manushi
on Thursday, January 20, 2011 4:25 AM
|
|
In my project , there is an xml file with 2 tables each one is having 96 columns.Now I need to dynamically generate a control to display exact number of rows and columns populating data from an xml file in C#.Please look on this.Thanks in advance.
|
Comment posted by
sivakumar
on Friday, January 28, 2011 6:53 AM
|
|
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BTNCLOSE" EventName="click" />
<%--<asp:AsyncPostBackTrigger ControlID="NEWROW" Eventname="click"/>--%>
</Triggers>
in my project i am using asp.net. for dynamically add a row in a table i can add the rows. but thats not getting refresh. if i reopened the table the new row is appearing. when i using this triggers the row is not added. please look on this. Thanks in advance.
|
Comment posted by
archana
on Friday, April 29, 2011 2:05 AM
|
|
// Create column 2
TableCell td2 = new TableCell();
CalendarExtender CL2 = new CalendarExtender();
CL2.ID = "Call1";
//TextBox _text = new TextBox();
//_text.ID = "txt1";
// Add control to the table cell
td2.Controls.Add(CL2);
TableCell td3 = new TableCell();
DropDownList _ddl=new DropDownList();
_ddl.ID = "ddlPrime";
_ddl.Width=120;
// Add control to the table cell
td3.Controls.Add(_ddl);
TableCell td4 = new TableCell();
DropDownList _ddl1 = new DropDownList();
_ddl1.ID = "ddlOfficer";
_ddl1.Width = 120;
// Add control to the table cell
td4.Controls.Add(_ddl1);
TableCell td5 = new TableCell();
DropDownList _ddl2 = new DropDownList();
_ddl2.ID = "ddlRanking";
_ddl2.Width = 120;
_ddl2.Items.Add("Low");
_ddl2.Items.Add("Medium");
_ddl2.Items.Add("Hot");
// Add control to the table cell
td5.Controls.Add(_ddl2);
// Add cell to the row
tr.Cells.Add(td1);
tr.Cells.Add(td2);
tr.Cells.Add(td3);
tr.Cells.Add(td4);
tr.Cells.Add(td5);
// Add row to the table.
ttt.Rows.Add(tr);
}
|
Comment posted by
kamiran
on Thursday, May 12, 2011 3:16 AM
|
|
Hi.. nice article. i was wondering if you could help with paging for the table. i am using a similar code and i am facing
a problem when it is many rows to create. i am using the tabcontainer and when i have many rows the page is getting ugly.. ;-)
|
Comment posted by
kamiran
on Thursday, May 12, 2011 4:19 AM
|
|
Hi.. nice article. i was wondering if you could help with paging for the table. i am using a similar code and i am facing
a problem when it is many rows to create. i am using the tabcontainer and when i have many rows the page is getting ugly.. ;-)
|
Comment posted by
jeya
on Tuesday, June 7, 2011 1:55 AM
|
|
how to store values in dynamically create textbox in the database usig c# coding.
|
Comment posted by
srinivasan
on Monday, August 22, 2011 2:41 AM
|
|
as of now ok.. good working i ned how to insert the values in the textbox to the database
|
Comment posted by
Mohd Shoeb
on Saturday, November 19, 2011 6:55 AM
|
|
these all examples are good but not completing the actual requirement...
The requirement is that
First create dynamic columns in gridview and add in gridviews cells textbox dynamically then onpagepostback get the value of textbox that is written by user on pageload
if any one is able to do that or have any code so please mail me on mohammad.shoeb@deft.in because i am in trouble this time. I have already spent 3 days on it but each time failed
Thanx a lot u all......
|
Comment posted by
Shaun
on Saturday, November 19, 2011 2:11 PM
|
|
Thanks! Helped me plenty!
|
Comment posted by
Steve
on Sunday, November 20, 2011 8:51 PM
|
|
Hi
I've modified your code to enable me to initially set up the table to display one row and then I can add a row. This works fine. I can also click a Post button to cause a refresh that retains the values upon refresh. But if I enter a value into one of the textboxes and then hit the add button to add a row, the table add a new row but doesn't retain the value. Code is:
<div>
<div>
Rows: <asp:TextBox ID="txtRows" runat="server" Width="30px"> </asp:TextBox> <br />
Cols: <asp:TextBox ID="txtCols" runat="server" Width="30px"></asp:TextBox>
<br />
<br />
<asp:Button ID="AddRow" runat="server" Text="Add Row" />
<asp:Button ID="DeleteRow" runat="server" Text = "Delete Row" /> <br /> <br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<br />
<br />
</div>
</div>
<asp:Button ID="btnPost" runat="server" Text="Cause Postback" />
Public Class _Default
Inherits System.Web.UI.Page
' Rows property to hold the Rows in the ViewState
Protected Property Rows() As Integer
Get
If Not ViewState("Rows") Is Nothing Then
Return CInt(Fix(ViewState("Rows")))
Else
Return 0
End If
End Get
Set(ByVal value As Integer)
ViewState("Rows") = value
End Set
End Property
' Columns property to hold the Columns in the ViewState
Protected Property Columns() As Integer
Get
If Not ViewState("Columns") Is Nothing Then
Return CInt(Fix(ViewState("Columns")))
Else
Return 0
End If
End Get
Set(ByVal value As Integer)
ViewState("Columns") = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
if Not Page.IsPostBack
Me.Rows = 1
Me.Columns = 1
End If
CreateDynamicTable()
End Sub
Private Sub CreateDynamicTable()
Dim tblRows As Integer
Dim tblCols As Integer
PlaceHolder1.Controls.Clear()
' Fetch the number of Rows and Columns for the table using the properties
tblRows = Rows
tblCols = Columns
Dim tbl As New Table
tbl.ID = "Table1"
PlaceHolder1.Controls.Add(tbl)
for i As Integer = 0 to tblRows - 1
Dim row As New TableRow
for j As Integer = 0 to tblCols - 1
Dim cell As New TableCell
Dim element1 As New Label
element1.ID = "Blank_Label_" + CStr(i) + "Col_" + CStr(j)
element1.Text = ""
element1.Width = "94"
Dim element2 As New TextBox
element2.ID = "From_TextBoxRow_" + CStr(i) + "Col_" + CStr(j)
element2.Width = "150"
Dim element3 As New Label
element3.ID = "To_Label_" + CStr(i) + "Col_" + CStr(j)
element3.Text = " to "
Dim element4 As New TextBox
element4.ID = "To_TextBoxRow_" + CStr(i) + "Col_" + CStr(j)
element4.Width = "150"
Dim element5 As New CheckBox
element5.ID = "Delete_TextBoxRow_" + CStr(i) + "Col_" + CStr(j)
element5.Checked = False
cell.Controls.Add(element1)
cell.Controls.Add(element2)
cell.Controls.Add(element3)
cell.Controls.Add(element4)
cell.Controls.Add(element5)
row.Cells.Add(cell)
tbl.Rows.Add(row)
Next
Next
' This parameter helps determine in the LoadViewState event,
' whether to recreate the dynamic controls or not
ViewState("dynamictable") = True
End Sub
Protected Sub AddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRow.Click
Me.Rows = Me.Rows + 1
CreateDynamicTable()
End Sub
' Check the ViewState flag to determine whether to rebuild your table again
Protected Overrides Sub LoadViewState(ByVal earlierState As Object)
MyBase.LoadViewState(earlierState)
If ViewState("dynamictable") Is Nothing Then
CreateDynamicTable()
End If
End Sub
End Class
Can you help resolve?
Thanks
|
Comment posted by
MAnu
on Wednesday, January 11, 2012 5:02 AM
|
|
Excellent work . Thanks a lot. :)
|
Comment posted by
Krish
on Wednesday, June 13, 2012 2:27 AM
|
|
Hi All,
Can i access dynamic control value after postback which is in placeholder ?? need yr help
|
Comment posted by
dddddddd
on Monday, July 2, 2012 4:05 PM
|
|
mm
|
Comment posted by
RNE
on Tuesday, August 21, 2012 1:25 PM
|
|
Very helpful. Many thanks!
|
Comment posted by
Tamanna Mehrotra
on Sunday, August 26, 2012 7:31 AM
|
|
m very thankful to u that ur code has solved my very big problem....thank u very much.
|
Comment posted by
Fernando R. Strijeski
on Thursday, September 13, 2012 6:17 AM
|
|
Hi,
The same problem that: Krish.
"Can i access dynamic control value after postback which is in placeholder ?? need yr help"
Well, I put in page just object asp table, with "EnableViewState = True"
but I insert the rows and cells dinamically by a Button, using AJAX.
The problem: In other button, this objects aren't recognized.
What I do, pls???
|
Comment posted by
sudhakar
on Thursday, March 14, 2013 11:34 PM
|
|
nice information it useful to any one easyly understanding thank u so much
|
Comment posted by
sudhakar
on Thursday, March 14, 2013 11:39 PM
|
|
i want code in asp.net the runtime textboxes will disply and chekboxes also then user click the chek box the textboxes will be highlet otherwise invisible(after click the chekboxe the text will be visible)
pls send the code as early as possible thanking u sir
|
Comment posted by
vipasha
on Tuesday, July 16, 2013 1:15 AM
|
|
It is a great article and very well explained. I also want to know how to bind data from SQL Server database table into this dynamically generated table.
I actually want to use this concept to display data from database.
|
Comment posted by
Manoj Bawane
on Sunday, September 22, 2013 10:38 AM
|
|
fantastic article... thanks Suprotim,
But can we create this dynamic table into specific div control?
how can div referenced to .cs file? I tried but failed. plz suggest some suggestions..
|
Comment posted by
AIS
on Thursday, July 3, 2014 8:37 PM
|
|
Does anybody now how to get the text data from the control. ? I try with row(x).findcontrol(xcontrol) and it does works.
|
|