Click here to Skip to main content
15,904,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
i m creating some text (around 10) boxes dynamically with the help of a loop and create the id with the help of loop


now i want to store the text of the text boxes with the help of a button click



how can i do that
Posted

Hi Pradeep,

You an achieve this by using a string array. You must declare the array as static and re-read the array at page_load if in case you need to use up the values stored from those textboxes.

If you need more on that, share what you've tried yet.

Hope this helps.

Happy Coding !!
 
Share this answer
 
Comments
Pradeep_kaushik 30-Aug-12 5:39am    
i m using this code for creating text boxes

for (int i = 1; i <= a; i++)
{
int c = b + i;
Label lbl = new Label();
lbl.ID = Convert.ToString(b + i);
lbl.Text = "flat no."+ c;
lbl.Width = 70;



Label lbl1 = new Label();
lbl1.ID = "lbl1" + i+100;
lbl1.Text = "Size(Sq.ft)";
lbl1.Width = 70;

pnlInfo.Controls.Add(new LiteralControl("   "));

TextBox txt = new TextBox();
txt.ID = "txt" + i;
txt.Width = 70;


pnlInfo.Controls.Add(new LiteralControl("   "));

Label lbl2 = new Label();
lbl2.ID = "lbl2" + i+1000;
lbl2.Text = "type";
lbl2.Width = 70;

pnlInfo.Controls.Add(new LiteralControl("   "));

TextBox txt1 = new TextBox();
txt1.ID = "text1" + i;
txt1.Width = 70;

pnlInfo.Controls.Add(lbl);
pnlInfo.Controls.Add(new LiteralControl("  "));
pnlInfo.Controls.Add(lbl1);
pnlInfo.Controls.Add(new LiteralControl("  "));
pnlInfo.Controls.Add(txt);
pnlInfo.Controls.Add(new LiteralControl("  "));
pnlInfo.Controls.Add(lbl2);
pnlInfo.Controls.Add(new LiteralControl("  "));
pnlInfo.Controls.Add(txt1);

pnlInfo.Controls.Add(new LiteralControl("<br />"));


Button2.Visible = true;




and this for updating data


protected void Button2_Click(object sender, EventArgs e)
{
int b=Convert.ToInt32(DropDownList3.SelectedItem.Text);
b=b*100;
int a = Convert.ToInt32(Label2.Text);
for (int i = 1; i <= a; i++)
{
TextBox txtsize = (TextBox)pnlInfo.FindControl((Convert.ToString("txt" + i)));

TextBox txttype = (TextBox)pnlInfo.FindControl("text1"+i);


SqlCommand cmd = new SqlCommand("update flatconfig set size='" + txtsize.Text + "',type='" + txttype.Text + "' where town='" + DropDownList1.SelectedItem.Text + "' and tower='" + DropDownList4.SelectedItem.Text + "' and floor='" + DropDownList3.SelectedItem.Text + "' and flatno='" + 1 + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Put your function in page load event so every time it will loaded.

Make sure to create dynamic id for each text box control.

Then use .findcontrol method to get the object of the control and then you can get the value.

Suppose the textbox inside the panel control then use

string value = Panel1.FindControl("id").text.ToString();
 
Share this answer
 
Comments
Vipul J Patel 30-Aug-12 5:39am    
Let me know if you have any question.
Pradeep_kaushik 30-Aug-12 5:43am    
can you explain it with the help of my code
A few questions I think will clarify your question:

1. exactly what do mean by "id:" the TextBox's 'Name property ?

2. text in the TextBoxes is going to be changing: correct ?

3. when you say "store the text of the text boxes:" do you mean:

    a. to create a string containing all text of all the TextBoxes ? I'd assume not.

    b. then, assuming you want to store each TextBox's text somewhere:

        1. where is that somewhere: a file, an internal data structure on the server, or ?

        2. what form of storage: a List<string> : or ?


... edit : realized the solution I presented was far too elaborate for this simple case ...
List<TextBox> TBList = new List<TextBox>();
//
TextBox newTB;
//
// create TextBoxes: add each new instance to the List<TextBox>
/
for (int i = 0; i < 10; i++)
{
    newTB = new TextBox { 
        Name = "TB_" + i.ToString(), 
        Text = "some text for item " + i.ToString() };

    TBList.Add(newTB);
}
//
// then at any future time you can access the current Text in any TextBox by index:
string tb4TextByIndex = TBList[4].Text;
... end edit ...

best, Bill
 
Share this answer
 
v3
Simple solution which I ever know : Create controls inside Page_PreInit Event

Because you are loosing ViewState that's why you need to store the controls data somewhere, if I am not wrong.
Page_PreInit event will render your controls easily and store the ViewState of your controls.



--Amit
 
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