JordanBlackler
asked on
How to Save Dynamically created controls
Hello all -
This is on the web using C#.
When a user clicks the Add button it creates 3 labels and 3 textboxes. The most they would click the Add button would be 12 times, anyway...
So, they click the add button, then they would enter text into the 3 textboxes. When the click the Add button again it keeps the data that was previously entered.
So when that's all done, they hit the save button, which saves the values to a SharePoint List.
(I know the ID's of each control.)
When they go back to the SharePoint list to edit the document, is there a way to get those dynamic fields back?
Here is my code that deals with creating the fields:
protected void btnAdd_Click(object sender, EventArgs e)
{
txtTotalAmount.Focus();
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
count++;
ViewState["count"] = count;
CreateControls();
}
private void CreateControls()
{
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
while (PlaceHolder1.Controls.Cou nt < count * 10)
{
//Create Table
Literal tableInfo = new Literal();
tableInfo.Text = "<table>";
PlaceHolder1.Controls.Add( tableInfo) ;
tableInfo = new Literal();
tableInfo.Text = "<tr><td>";
PlaceHolder1.Controls.Add( tableInfo) ;
// -----INVOICE-----
//Create First Label
Label _lblInvoice = new Label();
PlaceHolder1.Controls.Add( _lblInvoic e);
_lblInvoice.ID = "lbl_Invoice" + PlaceHolder1.Controls.Coun t.ToString ();
_lblInvoice.Text = "Invoice #:";
//Create First TextBox
TextBox _txtInvoice = new TextBox();
PlaceHolder1.Controls.Add( _txtInvoic e);
_txtInvoice.ID = "txt_Invoice" + PlaceHolder1.Controls.Coun t.ToString ();
_txtInvoice.Text = "";
_txtInvoice.Height = 18;
_txtInvoice.Width = 100;
// -----AMOUNT-----
//Create Second Label
Label _lblAmount = new Label();
PlaceHolder1.Controls.Add( _lblAmount );
_lblAmount.ID = "lbl_Amount" + PlaceHolder1.Controls.Coun t.ToString ();
_lblAmount.Text = "Amount:";
//Create Second TextBox
TextBox _txtAmount = new TextBox();
PlaceHolder1.Controls.Add( _txtAmount );
_txtAmount.ID = "txt_Amount" + PlaceHolder1.Controls.Coun t.ToString ();
_txtAmount.Text = "";
_txtAmount.Height = 18;
_txtAmount.Width = 100;
// -----DATE-----
//Create Third Label
Label _lblDateRecieved = new Label();
PlaceHolder1.Controls.Add( _lblDateRe cieved);
_lblDateRecieved.ID = "lbl_DateRecieved" + PlaceHolder1.Controls.Coun t.ToString ();
_lblDateRecieved.Text = "Date:";
//Create Third TextBox
TextBox _txtDateRecieved = new TextBox();
PlaceHolder1.Controls.Add( _txtDateRe cieved);
_txtDateRecieved.ID = "txt_DateRecieved" + PlaceHolder1.Controls.Coun t.ToString ();
_txtDateRecieved.Text = "";
_txtDateRecieved.Height = 18;
_txtDateRecieved.Width = 100;
//Close Table
tableInfo = new Literal();
tableInfo.Text = "</td></tr>";
PlaceHolder1.Controls.Add( tableInfo) ;
tableInfo = new Literal();
tableInfo.Text = "</table>";
PlaceHolder1.Controls.Add( tableInfo) ;
}
}
protected void Page_Load(object sender, EventArgs e)
{
CreateControls();
}
Thanks for any info!!
This is on the web using C#.
When a user clicks the Add button it creates 3 labels and 3 textboxes. The most they would click the Add button would be 12 times, anyway...
So, they click the add button, then they would enter text into the 3 textboxes. When the click the Add button again it keeps the data that was previously entered.
So when that's all done, they hit the save button, which saves the values to a SharePoint List.
(I know the ID's of each control.)
When they go back to the SharePoint list to edit the document, is there a way to get those dynamic fields back?
Here is my code that deals with creating the fields:
protected void btnAdd_Click(object sender, EventArgs e)
{
txtTotalAmount.Focus();
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
count++;
ViewState["count"] = count;
CreateControls();
}
private void CreateControls()
{
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
while (PlaceHolder1.Controls.Cou
{
//Create Table
Literal tableInfo = new Literal();
tableInfo.Text = "<table>";
PlaceHolder1.Controls.Add(
tableInfo = new Literal();
tableInfo.Text = "<tr><td>";
PlaceHolder1.Controls.Add(
// -----INVOICE-----
//Create First Label
Label _lblInvoice = new Label();
PlaceHolder1.Controls.Add(
_lblInvoice.ID = "lbl_Invoice" + PlaceHolder1.Controls.Coun
_lblInvoice.Text = "Invoice #:";
//Create First TextBox
TextBox _txtInvoice = new TextBox();
PlaceHolder1.Controls.Add(
_txtInvoice.ID = "txt_Invoice" + PlaceHolder1.Controls.Coun
_txtInvoice.Text = "";
_txtInvoice.Height = 18;
_txtInvoice.Width = 100;
// -----AMOUNT-----
//Create Second Label
Label _lblAmount = new Label();
PlaceHolder1.Controls.Add(
_lblAmount.ID = "lbl_Amount" + PlaceHolder1.Controls.Coun
_lblAmount.Text = "Amount:";
//Create Second TextBox
TextBox _txtAmount = new TextBox();
PlaceHolder1.Controls.Add(
_txtAmount.ID = "txt_Amount" + PlaceHolder1.Controls.Coun
_txtAmount.Text = "";
_txtAmount.Height = 18;
_txtAmount.Width = 100;
// -----DATE-----
//Create Third Label
Label _lblDateRecieved = new Label();
PlaceHolder1.Controls.Add(
_lblDateRecieved.ID = "lbl_DateRecieved" + PlaceHolder1.Controls.Coun
_lblDateRecieved.Text = "Date:";
//Create Third TextBox
TextBox _txtDateRecieved = new TextBox();
PlaceHolder1.Controls.Add(
_txtDateRecieved.ID = "txt_DateRecieved" + PlaceHolder1.Controls.Coun
_txtDateRecieved.Text = "";
_txtDateRecieved.Height = 18;
_txtDateRecieved.Width = 100;
//Close Table
tableInfo = new Literal();
tableInfo.Text = "</td></tr>";
PlaceHolder1.Controls.Add(
tableInfo = new Literal();
tableInfo.Text = "</table>";
PlaceHolder1.Controls.Add(
}
}
protected void Page_Load(object sender, EventArgs e)
{
CreateControls();
}
Thanks for any info!!
when you say go back to the sharepoint list do you mean, you want to render an asp page which interprets the data in the SP List? or do you want to actually do something when you visit the list in sharePoint?
ASKER
Sorry about that.
When they go to the sharepoint list to find there document, there is a link that brings them back to the the aspx page that they filled out.
ie: http://myserver/Testing/Testingaspx?ID=10
I use the ID field in sharepoint to get the current row of the document and then i load the data back into the aspx page. But i'm in the process of trying to figure out how to load the dynamic fields when they go back to the aspx page.
When they go to the sharepoint list to find there document, there is a link that brings them back to the the aspx page that they filled out.
ie: http://myserver/Testing/Testingaspx?ID=10
I use the ID field in sharepoint to get the current row of the document and then i load the data back into the aspx page. But i'm in the process of trying to figure out how to load the dynamic fields when they go back to the aspx page.
ASKER
It looks like i figured it out.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So what i did was:
i checked to see what columns in sharepoint contain data from the dynamic controls, i then rendered those controls again and populated it with the data.
Here is part of the code. This code only runs once when the page loads. It is only if the Add button was hit twice. There might be a nicer way to do it, but until i find a nicer way this will have to do.
String strTB = listItem["Amount2"].ToStri ng();
String strTB2 = listItem["Amount3"].ToStri ng();
if (strTB != "" & strTB2 != "")
{
//Creates the controls
btnAdd_Click(btnAdd, new System.EventArgs());
//Creates the controls
btnAdd_Click(btnAdd, new System.EventArgs());
}
for (int i = 0; i < PlaceHolder1.Controls.Coun t; i++)
{
System.Web.UI.WebControls. TextBox TB = (TextBox)PlaceHolder1.Cont rols[i].Fi ndControl( "txt_Amoun t6");
System.Web.UI.WebControls. TextBox TB2 = (TextBox)PlaceHolder1.Cont rols[i].Fi ndControl( "txt_Amoun t16");
strTB = listItem["Amount2"].ToStri ng();
strTB2 = listItem["Amount3"].ToStri ng();
//Sets the values of the dynamic fields
TB.Text = strTB;
TB2.Text = strTB2;
}
i checked to see what columns in sharepoint contain data from the dynamic controls, i then rendered those controls again and populated it with the data.
Here is part of the code. This code only runs once when the page loads. It is only if the Add button was hit twice. There might be a nicer way to do it, but until i find a nicer way this will have to do.
String strTB = listItem["Amount2"].ToStri
String strTB2 = listItem["Amount3"].ToStri
if (strTB != "" & strTB2 != "")
{
//Creates the controls
btnAdd_Click(btnAdd, new System.EventArgs());
//Creates the controls
btnAdd_Click(btnAdd, new System.EventArgs());
}
for (int i = 0; i < PlaceHolder1.Controls.Coun
{
System.Web.UI.WebControls.
System.Web.UI.WebControls.
strTB = listItem["Amount2"].ToStri
strTB2 = listItem["Amount3"].ToStri
//Sets the values of the dynamic fields
TB.Text = strTB;
TB2.Text = strTB2;
}
Hey, If it works!
It doesn't seem too bad.
It doesn't seem too bad.