HI I have requirement that
1) display given no.of textboxes dynamically and save to DB
2) if I change the number then new textboxes should append to UI
EX: TextBox AddButton
If I give 2 in textbox and click on add
Then 2 textboxes should appear. I filled some data in those textboxes. Now When I change the value 2 to 5 then 3 more textboxes should append(condition:old textboxes data should retain)
If the second value is less than or equal to first value then do nothing.
My code is
void Append()
{
string Data = string.Empty;
TextBox tb;
if (Convert.ToInt32(hdnCnt.Value) < Convert.ToInt32(txtNoofGames.Text))
{
for (int i = 0; i < Convert.ToInt16(txtNoofGames.Text); i++)
{
if (i <= Convert.ToInt32(hdnCnt.Value))
{
tb = (TextBox)Form.FindControl("txtGame1");
Data = tb.Text;
}
TextBox Newtb = new TextBox();
Newtb.ID = "txtGame" + i;
Form.Controls.Add(Newtb);
if (i <= Convert.ToInt32(hdnCnt.Value))
{
Newtb.Text = Data;
}
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (hdnCnt.Value != "")
Append();
hdnCnt.Value = txtNoofGames.Text;
for (int i = 0; i < Convert.ToInt16(txtNoofGames.Text); i++)
{
TextBox tb = new TextBox();
tb.ID = "txtGame" + i;
Form.Controls.Add(tb);
}
}
I am getting exception "object reference not set to an instance of object" at Data = tb.Text; in append method.
You didn't initialize it from the looks of it
TextBox tb = new TextBox();
Hope that helps,
Instead of Form.Controls.Add(tb);
Please try with Page.Form.Controls.Add(tb);
Related
I have 4-4 textboxes in two column and named them like
nfmin1, nfmin2,nfmin3,nfmin4 and nfmax1,nfmax2,nfmax3,nfmax4
what I want, when I click the submit button
I get all the values filled in nfmin1 to nfmin4 and nfmax1 to nfmax4
using loop
You can access form controls by name, in this way:
void SubmitButton_Click(object sender, EventArgs e)
{
List<String> nfminValues = new List<String>();
List<String> nfmaxValues = new List<String>();
for (int i = 1; i <= 4; i++)
{
TextBox current_nfmin = this.Controls["nfmin" + i.ToString()] as TextBox;
if (current_nfmin != null)
nfminValues.Add(current_nfmin.Text);
TextBox current_nfmax = this.Controls["nfmax" + i.ToString()] as TextBox;
if (current_nfmax != null)
nfmaxValues.Add(current_nfmax.Text);
}
}
NOTE: this is just an example I wrote on the fly, I put the content of each TextBox in two different lists but you can do anything you want with them.
I have used the below code to dynamically create textboxes by giving the total number of textboxes initially. After i enter values to these textboxes, how can i fetch the values from it. Like if i give count as 3, 3 textboxes will be created. Now, i enter data to each textbox. How can i read the values i entered into these texboxes.
int a = 1;
public Form1()
{
InitializeComponent();
}
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
txt.Top = a * 25;
txt.Left = 100;
txt.Name = "txt" + this.a.ToString();
txt.Text = "TextBox " + this.a.ToString();
a = a + 1;
return txt;
}
private void button1_Click(object sender, EventArgs e)
{
int i;
int count = Int16.Parse(counttxt.Text.ToString());
for (i = 1; i <= count; i++)
{
AddNewTextBox();
}
}
Hold a reference to the dynamically generated TextBox in a variable of type array or list.
Beside that if u want the value from TextBox that was named txt1
string value = this.Controls["txt1"].Text
string allTextBoxValues = "";
foreach (Control childc in Controls) {
if (childc is TextBox && childc.Name.Contains("txt"))
allTextBoxValues += ((TextBox)childc).Text + ",";
}
I am doing a crossword puzzle and i have 100 text boxes in a panel like this :
Every text box have an id of 00 - 99 since there is 100 of it .
First Row will have an id 00-09 , 2nd row will have an id of 10-19 and so on.
When user types something in some text box will be null and some text box will have values in it. How do I save values from a text box of a certain id to a database? For example the above image, HELP, text box id of 22 will have the value H , id of 23 will have the value of E , id of 24 will have value of L , id of 25 will have value of P.
I don't want to save the null values of the text box , I want to save values of the textboxes which are not null. I also need to take into account their textbox ids so that when I populate them back, I just have to insert them through ID .
I am new to C# , appreciate any help/advise/solutions on this.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
//hw.Write("<table>");
for (int i = 0; i <= 9; i++)
{
//hw.Write("<tr>");
for (int j = 0; j <= 9; j++)
{
TextBox tb = new TextBox();
tb.MaxLength = (1);
tb.Width = Unit.Pixel(40);
tb.Height = Unit.Pixel(40);
tb.ID = i.ToString() + j.ToString(); // giving each textbox a different id 00-99
Panel1.Controls.Add(tb);
}
Literal lc = new Literal();
lc.Text = "<br />";
Panel1.Controls.Add(lc);
}
}
protected void btnShow_Click(object sender, EventArgs e)
{
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text))
{
textBox.Style["visibility"] = "hidden";
}
// textBox.Enabled = false;
textBox.Text = "";
}
}
}
The proper way to do this is to wrap these textboxes inside a Repeater or Datalist controls. You can ready about these controls from here. This way when number of rows increase you will not have to change to your loop or hard-coded values.
As for your question to store values for a given Id, you can define row# and col# in your database and sort on row# and col#, this should work.
The easiest way is to make a 2D array (or List) of your TextBoxes. In where you create your TextBoxes:
List<List<TextBox>> textBoxList = new List<List<TextBox>>();
for (int i = 0; i <= 9; i++)
{
List<TextBox> textBoxRow = new List<TextBox>(); //this could be columns, not sure
for (int j = 0; j <= 9; j++)
{
TextBox tb = new TextBox();
....
textBoxRow.Add(tb);
}
...
textBoxList.Add(textBoxRow);
}
Now you can read/write to those array entries, such as:
string readValue = textBoxList[2][5].Text;
textBoxList[1][7].Text = "asdf";
ASP.NET C#.
Inside UpdatePanel we have TextBox with OnTextChanged="text_changed" method and Panel.
if number 3 was typed at textbox, 3 textboxes below will appear inside Panel with different IDs.
However when button outside updatepanel clicks, dynamically created textboxes not found error occured.
How to get values of dynamically created textboxes?
Creating textbox:
protected void text_changed(Object sender, EventArgs e)
{
int n = Int32.Parse(TextBox6.Text);
Table table = new Table();
for (int i = 0; i < n; i++)
{
TableRow trow = new TableRow();
table.Rows.Add(trow);
TableCell tcell = new TableCell();
tcell.Text = (i + 1).ToString();
TextBox tb = new TextBox();
tb.ID = "TB" + i.ToString();
tcell.Controls.Add(tb);
trow.Cells.Add(tcell);
}
Panel1.Controls.Add(table);
ButtonClick //get values from created textboxes:
int n = Int32.Parse(TextBox6.Text);
for (int i = 0; i < n; i++)
{
string title = ((TextBox)UpdatePanel1.FindControl("Panel1").FindControl("TB" + i.ToString())).Text; //here null pointer exception..
}
where are you generating your textboxes? if you're creating them in text_changed event, then on the next post back your going to run into pagelife cycle issues. you'd need to cache the fact that you created them, and recreate them in the OnInit phase of the page.
I have this code, which dynamically creates some textfields for me. k is taken from user btw.
for (int i = 0; i < k; i++)
{
TextBox t1 = new TextBox();
t1.Parent = groupBox2;
t1.Left = textBox2.Left;
t1.Top = textBox2.Top + (i + 1) * 40;
t1.Name = "text" + (i + 1);
t1.Enabled = true;
groupBox2.Controls.Add(t1);
}
What i want to do is, after this creating phase is done, when the user presses groupbox2's "OK" button, I want to take the created textfields' text properties, but so far I don't know how could this be done, since I gave textfields a name, I tried this but didn't work.
private void button3_Click(object sender, EventArgs e)
{
node1.name = textBox2.Text;
for (int i = 0; i < k; i++)
{
node1.array[i] = Convert.ToInt32("text"+(i+1).Text);
}
}
Any help would be nice, thanks.
Try this method:
private void button3_Click(object sender, EventArgs e)
{
node1.name = textBox2.Text;
for (int i = 0; i < k; i++)
{
TextBox txtBox = (TextBox)groupBox2.FindControl("text" + (i + 1));
if (txtBox != null)
{
node1.array[i] = txtBox.Text;
}
}
}
Loop through your text boxes in groupBox1 and get their names,Try this:
List<string> TextBoxesName=new List<string>();
foreach (Control item in groupBox1.Controls)
{
if (item is TextBox)
{
TextBoxesName.Add((item as TextBox).Text);
}
}
Set to your dynamic texboxes ID and than you can do groupBox2.FindControl("dynamic_texbox_id") to get your text box
Easiest solution is to put your listboxes in a collection of some sort
List<ListBox> listboxes = new List<ListBox>();
for (...)
{
...
listboxes.add(listbox);
}
Then you can refer back to them whenever you want
Or since you're adding them to a groupbox, why not go through that collection?