Click here to Skip to main content
15,851,514 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am creating dynamic textboxes based on table's row count. After Click on submit button the entered values in dynamic textboxes will be used in further process.

Problem is on runtime Textbox are created but the Textbox are not accessible for process. TextBox Creation code


C#
protected void btnSearch_Click(object sender, EventArgs e)
   {
       DataTable clientSeries = new DataTable();
       DataTable dtexistseries = new DataTable();
       int clientID = 0;
       if (!string.IsNullOrEmpty(absClientDetail.SelectedValue))
       {

           if (int.TryParse(absClientDetail.SelectedValue, out clientID))
           {

               clientSeries = DiscStructureAdvances.LoadClientRelatedDiscountStru(clientID);
               clientSeries.DefaultView.Sort = "seriesid ASC";

               dtexistseries = Serieses.LoadSeriesIdAndName();
               dtexistseries.DefaultView.Sort = "seriesid ASC";

               int seriescount = dtexistseries.Rows.Count;
               int clientSeriescount =  clientSeries.Rows.Count;
               txtnames = string.Empty;
               for (int i = 0; i < seriescount; i++)
               {
                   TextBox TxtBoxU = new TextBox();
                   Label lblU = new Label();
                   string txtboxname = "txt_" + Convert.ToString(dtexistseries.Rows[i]["seriesid"]);
                   TxtBoxU.ID = txtboxname;

                   txtnames += txtboxname;
                   txtnames += ",";

                   lblU.ID = "LabelU" + Convert.ToString(dtexistseries.Rows[i]["seriesname"]);
                   lblU.Text = "Series:" + Convert.ToString(dtexistseries.Rows[i]["seriesname"]); ;

                   DataView dv = new DataView(clientSeries);
                   dv.RowFilter = "seriesname = '" + Convert.ToString(dtexistseries.Rows[i]["seriesname"]) + "' AND seriesid = " + Convert.ToInt16(dtexistseries.Rows[i]["seriesid"]) + "";

                   DataTable dt = dv.ToTable();
                   if (dt.Rows.Count > 0)
                   {
                       TxtBoxU.Text = Convert.ToDecimal(dt.Rows[0]["discount"]).ToString();
                   }
                   else
                   {
                       TxtBoxU.Text = string.Empty;
                   }

                   TxtBoxU.ClientIDMode = System.Web.UI.ClientIDMode.Static;
                   TxtBoxU.ReadOnly = true;


                   PlaceHolder1.Controls.Add(lblU);
                   PlaceHolder1.Controls.Add(TxtBoxU);

               }
           }
       }
   }





Now the textBox Id are already saved ,in string.


C#
protected void btnSave_Click(object sender, EventArgs e)
 {
     string[] individualseries = txtnames.Split(',');

     string valuetoupdate;

     TextBox t1 = PlaceHolder1.FindControl(individualseries[1]) as TextBox;

     //for placeholder
     // Get a reference to the master page
     MasterPage ctl00 = FindControl("ctl00") as MasterPage;
     // Get a reference to the ContentPlaceHolder
     ContentPlaceHolder MainContent = ctl00.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;

     //now pointing to the panel
     PlaceHolder tempPanel = MainContent.FindControl("PlaceHolder1") as PlaceHolder;

     TextBox AgeTextBox = tempPanel.FindControl(individualseries[0]) as TextBox;

     //TextBox textbox = this.Master.FindControl("ContentPlaceHolder1").FindControl("Panel1").FindControl(individualseries[0]) as TextBox;// not working
     foreach (string series in individualseries)
     {
         //TextBox txt1 = (TextBox)Panel1.FindControl(series);
         TextBox txt = (TextBox)tempPanel.FindControl(series);
         decimal discount = 0;
         if ((txt.Text != string.Empty) && (string.IsNullOrEmpty(txt.Text)) && (string.IsNullOrWhiteSpace(txt.Text)))
         {
             if(decimal.TryParse(txt.Text, out discount))
             {
                 valuetoupdate += txt.Text;
             }
         }
     }
 }



Please suggest some solution
thank you
Posted
Updated 12-Dec-15 22:44pm
v2
Comments
Sergey Alexandrovich Kryukov 12-Dec-15 3:21am    
TextBox? Which one? Full type name, please. Is it ASP.NET, or something else? (I can guess, but better specify everything explicitly, especially if you really need help.)
What exactly you have to do with those TextBox instances?
—SA
Richard MacCutchan 12-Dec-15 5:00am    
What exactly is the problem?
BillWoodruff 12-Dec-15 6:52am    
Please tag as ASP.NET

Why don't you create a Dictionary ... outside the for loop ... and use it to preserve the references to the Controls you create at run-time ? Using 'FindControl to get them later, and casting them back into Type 'TextBox, etc., is an expensive operation.

This code is a real waste of time:
string[] individualseries = txtnames.Split(',');

PlaceHolder tempPanel = MainContent.FindControl("PlaceHolder1") as PlaceHolder;

TextBox AgeTextBox = tempPanel.FindControl(individualseries[0]) as TextBox;

This is not a waste of time:
Dictionary<string,TextBox> SeriesToTextBox = new DictionaryDictionary<string,TextBox>

// add the instances of TextBoxU to the Dictionary in the for loop:
SeriesToTextBoxes.Add(txtboxname, TxtBoxU);

// then, later, when you need to access the TextBox instances' Text Property:
foreach (string series in individualseries)
{
    string txt = SeriesToTextBox[series].Text;
}
 
Share this answer
 
v4
hello ,thank you for replies and suggestion.
It is good to get response nowadays from the people.
I had tried to give you all infromation as possible.

1>This is a simple ASP.NET website and programming language is c#, with WAMP SERVER as backend

2>Main problem is the pointing to the dynamic created simple textboxes having dynamic Id
as per the series name and the value is series discount which is entered by use in past.
The textbox values might be entered or modified by user.

Main issue is retriving the Dynamic created TextBox and text entered.
I had used the code as follwed

<pre lang="c#">
TextBox TxtBoxU = new TextBox();
string txtboxname = "txt_" + Convert.ToString(dtexistseries.Rows[i]["seriesid"]);
TxtBoxU.ID = txtboxname;

Now for the textbox values are entered with coding
TxtBoxU.Text = Convert.ToDecimal(dt.Rows[0]["discount"]).ToString();

Now added to the panel named by the code
PlaceHolder1.Controls.Add(TxtBoxU);
The previous entered valued are shown in dymanic created textbox. Code is working till creating
dynamic TextBox and the Values.

this shows the entered or empty values.
Now the main problem is to locate the dynamic created textbox and value retrived, for further inserting
in table the structure is ("clientid","seriesid","Discount"")
proposed code to insert the data
C#
foreach (string series in individualseries)
       {
           TextBox txt1 = (TextBox)Panel1.FindControl(series);
           decimal discount =0;
           decimal.TryParse(txt1.Text.Trim(),out discount);
           tbinsertdiscount.Append("("+ Convert.ToInt16(cd.ClientID) +","+ Convert.ToInt16(series.Split('_')[1]) + "," + discount + ")");
       }

Main problem is how to retrive the textbox Id and values which might be changed or unchanged.

proposed solution by user BillWoodruff , was implemented as
// outsode the page's functions inclution page_load and button click and userdefined functins
//to make it accesable throughout page
public static Dictionary<string,TextBox> SeriesToTextBox = null ;

//in button search

<pre lang="c#">
protected void btnSearch_Click(object sender, EventArgs e)
{
SeriesToTextBox = new Dictionary<string,TextBox>() ;

//in for loop
SeriesToTextBox.Add(TxtBoxU.Text, TxtBoxU); -----> wrroe is shown ""An item with the same key has already been added. type ArgumentException "

Please suggest some solutions.
thanks.
 
Share this answer
 

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