I think you are using a DataTable, are you not? Read about multiple tier applications. The business layer is one of those tiers (or layers; both words are commonly used). The idea is to have classes that represent the data stored in your data layer (usually
a database).
Read MSDN about data binding: http://msdn.microsoft.com/en-us/library/ms178366.aspx and http://support.microsoft.com/kb/307860. The documents will clear up the difference between Eval() and Bind() plus will give you sound foundations to better use data
binding.
On click of a button, the following server side code runs.
TextBox tb = new TextBox();
tb.ID = GetDynControlID(rid, "first_name");
pnlTab2.Controls.Add(tb);
The textbox is added to screen and all is well, but when I go to submit and save the contents...this textbox can't be found anywhere...I keep getting error that it does not exist.
I have tried every command under the sun to find that text box...but its always null
foreach (Control c in pnlTab2.Controls)
{
if (c is TextBox)
{
Response.Write(c.ID);
}
}
On click of a button, the following server side code runs.
It is OK to run this code in response of a button click, but I also said you MUST re-create all dynamically-added controls on every postback. Are you doing that? From your description you are not doing it.
I thought thats being done, when I clicked the 'Add New Contact', I the textboxes are created and added to the panel as shown above( the fields are now shown on the screen). So when submit is pressed and the page is processed I thought the code behind script
would then find the new fields that was just recently added to the page control.
Like I said very early in this thread: Dynamic controls are not easy. And no,
that is not being done when you click "Add New Contact". The clicking of Add New Contact is
1 postback, not every postback. Check back the first answers I gave you for the mechanism to re-create the controls on every postback.
That makes total sense and I have reference the initial posts on this, the important piece is in the page load.
private void Page_Load(object sender, EventArgs e)
{
DataTable dt = DataSource;
if (!IsPostBack) dt = ObtainTheDataFromYourFavoriteDataSource(); //Usually data read from a database.
//Now create the dynamic controls.
CreateDynControls(dt);
}
But I am having trouble understanding is the code above is generating the controls based on the data that in the tdatatable. But the new fields that I just added are not part of the the data that is coming from the datasource.
So my question is how do I take my new form fields and make them part of this CreateDynControls, is it possible to append them to the viewstate. - I have never really dont any 'state' type of work before but really hope we can get this dynamic controls piece
working.
But I am having trouble understanding is the code above is generating the controls based on the data that in the tdatatable. But the new fields that I just added are not part of the the data that is coming from the datasource.
Ah ok, that makes total sense. I didn't realize about that. Ok, so besides adding a new control on click of "Add New Contact", you must also add a new DataRow to the DataTable stored in the DataSource property, so that the new row is also serialized into
ViewState, so the next time there is a postback the new (blank) row is accounted for during the running of
CreateDynControls().
I don't know if I mention the following in this thread or not: I never use DataTable's or similar. I always program using a business tier (layer is also a common name). In that scenario, I would have a List<Contact> instead of a DataTable, and I would
simply add a new Contact object to the list in response to the Click event of "Add New Contact".
Marked as answer by Amy Peng - MSFT on Feb 27, 2013 08:59 AM
So based on my example how would I "you must also add a new DataRow to the DataTable stored in the DataSource property" and is at what point do I do this. If you could provide a little snipped it may help me follow a bit better as well.
You did mention that you typically use a business layer. I guess its a little late now since I gone down this road with what I've done so far to switch over to using what you would typically use?
carloslobos
0 Points
34 Posts
Re: Creating textboxes dynamically based on records in table
Feb 25, 2013 12:50 AM|LINK
Hmm, business layer, can you expand on that?
In my .aspx page I have my repeater that builds the text boxes and in the code behind I bind the data to the repeater.
So how far off am I from what you are saying?
With respect to your second comment....the updating of the properties should be automatic
How is it automatic?
webJose
Member
730 Points
186 Posts
Re: Creating textboxes dynamically based on records in table
Feb 25, 2013 01:33 AM|LINK
I think you are using a DataTable, are you not? Read about multiple tier applications. The business layer is one of those tiers (or layers; both words are commonly used). The idea is to have classes that represent the data stored in your data layer (usually a database).
Read MSDN about data binding: http://msdn.microsoft.com/en-us/library/ms178366.aspx and http://support.microsoft.com/kb/307860. The documents will clear up the difference between Eval() and Bind() plus will give you sound foundations to better use data binding.
carloslobos
0 Points
34 Posts
Re: Creating textboxes dynamically based on records in table
Feb 25, 2013 03:41 AM|LINK
Is there something about state that I am missing.
On click of a button, the following server side code runs.
The textbox is added to screen and all is well, but when I go to submit and save the contents...this textbox can't be found anywhere...I keep getting error that it does not exist.
I have tried every command under the sun to find that text box...but its always null
foreach (Control c in pnlTab2.Controls) { if (c is TextBox) { Response.Write(c.ID); } }webJose
Member
730 Points
186 Posts
Re: Creating textboxes dynamically based on records in table
Feb 25, 2013 12:38 PM|LINK
It is OK to run this code in response of a button click, but I also said you MUST re-create all dynamically-added controls on every postback. Are you doing that? From your description you are not doing it.
carloslobos
0 Points
34 Posts
Re: Creating textboxes dynamically based on records in table
Feb 25, 2013 12:59 PM|LINK
Ok, I think my head is going in a loop here.
I thought thats being done, when I clicked the 'Add New Contact', I the textboxes are created and added to the panel as shown above( the fields are now shown on the screen). So when submit is pressed and the page is processed I thought the code behind script would then find the new fields that was just recently added to the page control.
webJose
Member
730 Points
186 Posts
Re: Creating textboxes dynamically based on records in table
Feb 25, 2013 01:26 PM|LINK
Like I said very early in this thread: Dynamic controls are not easy. And no, that is not being done when you click "Add New Contact". The clicking of Add New Contact is 1 postback, not every postback. Check back the first answers I gave you for the mechanism to re-create the controls on every postback.
carloslobos
0 Points
34 Posts
Re: Creating textboxes dynamically based on records in table
Feb 25, 2013 01:42 PM|LINK
That makes total sense and I have reference the initial posts on this, the important piece is in the page load.
private void Page_Load(object sender, EventArgs e) { DataTable dt = DataSource; if (!IsPostBack) dt = ObtainTheDataFromYourFavoriteDataSource(); //Usually data read from a database. //Now create the dynamic controls. CreateDynControls(dt); }But I am having trouble understanding is the code above is generating the controls based on the data that in the tdatatable. But the new fields that I just added are not part of the the data that is coming from the datasource.
So my question is how do I take my new form fields and make them part of this CreateDynControls, is it possible to append them to the viewstate. - I have never really dont any 'state' type of work before but really hope we can get this dynamic controls piece working.
webJose
Member
730 Points
186 Posts
Re: Creating textboxes dynamically based on records in table
Feb 25, 2013 02:11 PM|LINK
Ah ok, that makes total sense. I didn't realize about that. Ok, so besides adding a new control on click of "Add New Contact", you must also add a new DataRow to the DataTable stored in the DataSource property, so that the new row is also serialized into ViewState, so the next time there is a postback the new (blank) row is accounted for during the running of CreateDynControls().
I don't know if I mention the following in this thread or not: I never use DataTable's or similar. I always program using a business tier (layer is also a common name). In that scenario, I would have a List<Contact> instead of a DataTable, and I would simply add a new Contact object to the list in response to the Click event of "Add New Contact".
carloslobos
0 Points
34 Posts
Re: Creating textboxes dynamically based on records in table
Feb 25, 2013 02:29 PM|LINK
Thanks for your attention on this.
So based on my example how would I "you must also add a new DataRow to the DataTable stored in the DataSource property" and is at what point do I do this. If you could provide a little snipped it may help me follow a bit better as well.
You did mention that you typically use a business layer. I guess its a little late now since I gone down this road with what I've done so far to switch over to using what you would typically use?
webJose
Member
730 Points
186 Posts
Re: Creating textboxes dynamically based on records in table
Feb 25, 2013 02:45 PM|LINK
If you Google "add a new DataRow to the DataTable", which is the exact wording you have there, you find this as the #1 hit.
Also from my previous response:
That means in the same Click event.
I won't start talking about n-tier applications here. This thread is a mess as it is now. Google up the topic.