How to get text from dynamically created textboxes in ASP.NET - c#

I created dynamically some textboxes. They are created after click on one button(number of the textboxes depends on the user).
protected void Button1_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(TextBox2.Text);
Table tbl = new Table();
tbl.Width = Unit.Percentage(80);
TableRow tr;
TableCell tc;
TextBox txt;
CheckBox cbk;
DropDownList ddl;
Label lbl;
Button btn;
for (int j = 1; j <= i; j++)
{
tr = new TableRow();
tc = new TableCell();
tc.Width = Unit.Percentage(25);
lbl = new Label();
lbl.Text = "Pitanje:";
tc.Controls.Add(lbl);
tr.Cells.Add(tc);
tc.Width = Unit.Percentage(25);
txt = new TextBox();
txt.ID = "txt_p_" + j;
tc.Controls.Add(txt);
tr.Cells.Add(tc);
tc.Width = Unit.Percentage(25);
lbl = new Label();
lbl.Text = "Odgovori:";
tc.Controls.Add(lbl);
tr.Cells.Add(tc);
tc.Width = Unit.Percentage(25);
txt = new TextBox();
txt.ID = "txt_o_" + j;
tc.Controls.Add(txt);
tr.Cells.Add(tc);
tbl.Rows.Add(tr);
}
Panel1.Controls.Add(tbl);
}
now I need to get the text that is typed into that textboxes. I tried with something that I found on the internet but can't get it to work.
protected void SpremiPitanja(object sender, EventArgs e)
{
int i = Convert.ToInt32(TextBox2.Text);
for (int j = 1; j <= i; j++)
{
***************************************
}
}
any kind of help is welcome. if you need more information I will give them

A variable declared in a function is only visible in a function. You need to store the TextBoxes in a variable, that exists even when the code in the function has "finished". For more information search for scopes.
Here is a small sample that stores TextBoxes in a List that is visible in your class.
Another option would be to use eventhandlers. It depends on your scenario, which solution would be suited better. If you store the TextBoxes in a List, you can easily perform clean up code (for instance remove EventHandlers if required). You can obviously combine Approach 1 and 2. In that case you would store the created TextBox in a List (or any other collection), but you would still use the sender in the eventhandler to get a reference to the sending TextBox.
public partial class Form1 : Form
{
List<TextBox> textBoxes = new List<TextBox>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Approach 1: create and add textbox to list
TextBox createdTextbox = new TextBox();
textBoxes.Add(createdTextbox);
}
private void button2_Click(object sender, EventArgs e)
{
//use the textboxes from the list
foreach(TextBox t in textBoxes)
{
//do something with t
}
}
private void button3_Click(object sender, EventArgs e)
{
//Approach 2: use eventhandlers and don't store textbox in a list
TextBox createdTextbox = new TextBox();
createdTextbox.TextChanged += createdTextbox_TextChanged;
listBox1.Items.Add(createdTextbox);
}
void createdTextbox_TextChanged(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
if (t == null)
throw new ArgumentException("sender not of type TextBox", "sender");
//do something with t
}
}

You add textboxes the same way you do, this is my example (sorry it's vb.net):
Dim t As New TextBox With {.ID = "txt_123", .Text = "Vpiši"}
PlaceHolder1.Controls.Add(t)
t = New TextBox With {.ID = "txt_456", .Text = "Briši"}
PlaceHolder1.Controls.Add(t)
And then you iterate through controls in Placeholder (in my example):
Dim tItem As TextBox
Dim tValue As String = String.Empty
For Each c As Control In PlaceHolder1.Controls
If TypeOf c Is TextBox Then
tItem = c
tValue = tItem.Text.ToString
End If
Next
C# example added
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox t = new TextBox();
t.Text = "Vpiši";
t.ID = "txt_123";
PlaceHolder1.Controls.Add(t);
t = new TextBox();
t.Text = "Briši";
t.ID = "txt_456";
PlaceHolder1.Controls.Add(t);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox tItem;
String tValue;
foreach (Control c in PlaceHolder1.Controls)
{
if (c.GetType() == typeof(TextBox))
{
tItem = (TextBox)c;
tValue = tItem.Text;
}
}
}

Related

C# how do I make my label from an label array disappear when clicked on?

My code makes 5 labels appear with a random .Left location, you can see it.
I want the particular label to disappear when I click on it, but I don't know how to tell it to my click void.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label [] kubeliai = new Label [5];
int poz = 100;
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < kubeliai.Length; i++)
{
kubeliai[i] = new Label();
Controls.Add(kubeliai[i]);
Random pos = new Random();
kubeliai[i].Top = 50;
kubeliai[i].Left = poz;
poz += pos.Next(50, 200);
kubeliai[i].BackColor = Color.Red;
kubeliai[i].Height = 20;
kubeliai[i].Width = 20;
kubeliai[i].Click += new EventHandler(kubelio_clickas);
}
}
void kubelio_clickas (object sender, EventArgs e)
{
}
}
The instance of "clicked" label is in sender parameter:
void kubelio_clickas (object sender, EventArgs e)
{
Label clickedLabel = sender as Label;
if (clickedLabel != null) {
clickedLabel.Visible = false;
}
}
Because in .NET Event Handlers by default use object as type of sender you have to cast it to Label first.
I want the particular label to disappear when I click on it
Just set the label's .Visible property to false:
void kubelio_clickas (object sender, EventArgs e)
{
if (sender is Label)
((Label)sender).Visible = false;
}
The object sender is a reference to the object which fired the event. So basically, the sender is the object you are looking for.
You just need to set it invisible:
((Label)sender).Visible = false;

Accessing and checking values of dynamic controls ASP.NET

I have a scenario. following is the code:
Home.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (!String.IsNullOrEmpty(txtbox_query.Text.Trim()))
{
if (isTrue)
{
// To do statements
}
else
{
List<RequestAndResponse.Parameter> parameters = request.getParameter(txtbox_query.Text.Trim(), sourcePath, parameterValue);
Session["Data"] = parameters;
Response.Redirect("Result.aspx",false);
}
}
}
catch (Exception error)
{
Response.Write(error.Message);
}
}
Result.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
parameters = (List<RequestAndResponse.Parameter>)Session["Data"];
ContentPlaceHolder content = (ContentPlaceHolder)this.Form.FindControl("MainContent");
for (int j = 1; j <= _arrViewState; j++)
{
string _id = j.ToString();
TextBox txtfname = new TextBox();
txtfname.ID = "TextBox_" + _id + "_";
txtfname.Width = 160;
txtfname.Text = parameters[(j - 1)].Value.ToUpper();
txtfname.Attributes.Add("style", "color:#015D84;font-weight:bold;font-size:12px;padding:10px;");
txtfname.EnableViewState = true;
content.Controls.Add(txtfname);
content.Controls.Add(new LiteralControl("<br/>"));
}
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Submit";
btnSubmit.Click += new System.EventHandler(btnSubmit_click);
btnSubmit.Enabled = false;
content.Controls.Add(btnSubmit);
}
protected void btnSubmit_click(object sender, EventArgs e)
{
// How to find the dynamically created textbox
}
Now How to find the dynamically created controls
I know the basic like:
Form.FindControl("TextBox ID");
But here i dont know the textbox id and also i even dont know how many textbox will be their as it totally depends on user input i.e. from 2 TO N textboxes
What i want is on bttn_Click i will fetch the text from all the textboxes
How will i achieve this.
Also i want to check if all Textbox is empty or not on bttn_Click
Enumerate controls as follows
protected void btnSubmit_click(object sender, EventArgs e)
{
ContentPlaceHolder content = (ContentPlaceHolder)this.Form.FindControl("MainContent");
foreach (Control c in content.Controls)
{
if (c is TextBox)
{
TextBox txt = (TextBox)c;
// do something, e.g. Response.Write(txt.Text);
}
}
}

get values from dynamically added textboxes asp.net c#

as suggested in the title i have in which i can insert how many textboxes i want to add to a placeholder. i can add the textboxes just fine the problem is i cant get the values inserted on those dynamically added textboxes. here's my code
the purpose of this piece of code is to whenever the textbox in which i can introduce the number of textboxes i want. it creates and adds them to the placeholder in my page.
public void txtExtra_TextChanged(object sender, EventArgs e)
{
for (a = 1; a <= int.Parse(txtExtra.Text); a++)
{
TextBox txt = new TextBox();
txt.ID = "txtquestion" + a;
pholder.Controls.Add(txt);
}
}
this is the code of the button that will submit and response.write the values inserted in all those textboxes.
protected void btnConfirm_Click(object sender, EventArgs e)
{
foreach (Control ctr in pholder.Controls)
{
if (ctr is TextBox)
{
string value = ((TextBox)ctr).Text;
Response.Write(value);
}
}
}
i've been searching online and i've been getting answers that this code is fine and it should work but it doesnt. if you guys see anything wrong or have any suggestion that can solve my problem i'd really appreciate it
You are almost there.
Problem
You need to reload those dynamically created textboxes on post back. Otherwise, they will become null, and you won't be able to find it.
In order to do that, you need to save those dynamically TextBoxes Ids in persistent location such as View State or Session State.
Screen Shot
ASPX
Number of TextBoxes: <asp:TextBox runat="server" ID="CounterTextBox"
OnTextChanged="CounterTextBox_TextChanged" AutoPostBack="True" /><br/>
<asp:PlaceHolder runat="server" ID="TextBoxPlaceHolder" /><br/>
<asp:Button runat="server" ID="ConfirmButton" Text="Confirm"
OnClick="ConfirmButton_Click" /><br/>
Result: <asp:Literal runat="server" ID="ResultLiteral"/>
Code Behind
private List<string> TextBoxIdCollection
{
get
{
var collection = ViewState["TextBoxIdCollection"] as List<string>;
return collection ?? new List<string>();
}
set { ViewState["TextBoxIdCollection"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
foreach (string textboxId in TextBoxIdCollection)
{
var textbox = new TextBox {ID = textboxId};
TextBoxPlaceHolder.Controls.Add(textbox);
}
}
protected void CounterTextBox_TextChanged(object sender, EventArgs e)
{
var collection = new List<string>();
int total;
if (Int32.TryParse(CounterTextBox.Text, out total))
{
for (int i = 1; i <= total; i++)
{
var textbox = new TextBox { ID = "QuestionTextBox" + i };
// Collect this textbox id
collection.Add(textbox.ID);
TextBoxPlaceHolder.Controls.Add(textbox);
}
TextBoxIdCollection= collection;
}
}
protected void ConfirmButton_Click(object sender, EventArgs e)
{
foreach (Control ctr in TextBoxPlaceHolder.Controls)
{
if (ctr is TextBox)
{
string value = ((TextBox)ctr).Text;
ResultLiteral.Text += value;
}
}
}
You are actually creating textboxes with property Text set to default = ""; So you need set txt.Text property for example:
public void txtExtra_TextChanged(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(txtExtra.Text); a++)
{
TextBox txt = new TextBox();
txt.ID = "txtquestion" + a;
txt.Text = "Some text"; // Set some text here
pholder.Controls.Add(txt);
}
}
EDIT:
After that you can store your values into the list:
private static List<string> values = new List<string>();
protected void btnConfirm_Click(object sender, EventArgs e)
{
foreach (Control ctr in pholder.Controls)
{
if (ctr is TextBox)
{
string value = ((TextBox)ctr).Text;
values.Add(value); // add values here
}
}
}
EDIT:
Here is your values:
EDIT:
For super mega better understanding:
Create one more textbox txtOutput then add button GetDataFromTextBoxesAndPutItBelow and create an event for that button `Click'. Event code:
protected void btnGetData_Click(object sender, EventArgs e)
{
for (int i = 0; i < values.Count; i++)
txtOutput.Text += "Value from txtquestion1: " + values[i] + " ";
}
Screenshot looks:
for (int i = 0; i < dataTable.Rows.Count; i++)
{
int comment_id = Convert.ToInt32(dataTable.Rows[i]["comment_id"]);
string created_by_name = dataTable.Rows[i]["created_by_name"].ToString();
string created_at = dataTable.Rows[i]["created_at"].ToString();
string comment = dataTable.Rows[i]["comment"].ToString();
HtmlGenericControl divComment = new HtmlGenericControl("div"); //This is root object of comment.Other objects like textbox,button,etc added into this object.
//divComment.Attributes.Add("class", "div_post_display");
divComment.Attributes.Add("id", comment_id.ToString());
/* Comment by */
HtmlGenericControl lblCommentBy = new HtmlGenericControl("label");
//lblCommentBy.Attributes.Add("class", "divauthor");
lblCommentBy.InnerText = "" + created_by_name + " (" + created_at + ")";
/* Comment body */
HtmlGenericControl pComment = new HtmlGenericControl("p");
//lblCommentBy.Attributes.Add("class", "divauthor");
pComment.InnerText = comment;
divComment.Controls.Add(lblCommentBy);
divComment.Controls.Add(pComment);
if (Session["user_id"] != null)
{
if (Session["user_level"].ToString() == "1") //Admin can reply for comment
{
/* Reply Form */
TextBox txtReply = new TextBox(); //Create object dynamacaly
txtReply.ID = "txtReply_"+comment_id;
txtReply.Attributes.Add("class", "form-control"); //Add css class
txtReply.Width = 400;
divComment.Controls.Add(txtReply); //Add obj to root object(div)
Button btnReply = new Button(); //Create object dynamacaly
btnReply.Text = "Reply"; //Set button text
btnReply.Attributes.Add("class", "btn btn-sm btn-success"); //Add css class
btnReply.Click += btnReply_Click;
btnReply.CommandArgument = comment_id.ToString();
divComment.Controls.Add(btnReply); //Add obj to root object(div)
HtmlGenericControl br = new HtmlGenericControl("br"); //Create object dynamacaly
divComment.Controls.Add(br); //new line
}
}
pnlShowComments.Controls.Add(divComment);
}

Get values of dynamically created controls (comboboxes)

I've got panel on which by default are two comboboxes and one "+" button which creates two new combo boxes bellow the first one, I can create multiple (n) rows with two combo boxes and everything is working, I just can't figure out how to get values of those boxes?
Here's code for creating (adding) controls
private void btnCreateFilter_Click(object sender, EventArgs e)
{
y += comboBoxHeight;
ComboBox cb = new ComboBox();
cb.Location = new Point(x, y);
cb.Size = new Size(121, 21);
panelFiltri.Controls.Add(cb);
yDrugi += comboBoxHeight;
ComboBox cbSql = new ComboBox();
cbSql.Location = new Point(xDrugi, yDrugi);
cbSql.Size = new Size(121, 21);
panelFiltri.Controls.Add(cbSql);
btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y + 25);
btnSaveFilter.Location = new Point(btnSaveFilter.Location.X, btnSaveFilter.Location.Y + 25);
}
And here's code where I'm lost:
private void btnSaveFilter_Click(object sender, EventArgs e)
{
int i;
foreach (Control s in panelFiltri.Controls)
{
//GOT LOST
}
}
You can get the text in the ComboBox as
private void btnSaveFilter_Click(object sender, EventArgs e)
{
foreach (Control control in panelFiltri.Controls)
{
if (control is ComboBox)
{
string valueInComboBox = control.Text;
// Do something with this value
}
}
}
I don't really know what you're trying to achieve... Maybe this will help you along...
private void btnSaveFilter_Click(object sender, EventArgs e)
{
foreach (ComboBox comboBox in panelFiltri.Controls)
{
var itemCollection = comboBox.Items;
int itemCount = itemCollection.Count; // which is 0 in your case
}
}

Save Values of Dynamically created TextBoxes

guys i am creating dynamic TextBoxes everytime a button is clicked. but once i have as many text boxes as i want.. i want to save these value Database Table.. Please guide how to save it into DB
public void addmoreCustom_Click(object sender, EventArgs e)
{
if (ViewState["addmoreEdu"] != null)
{
myCount = (int)ViewState["addmoreEdu"];
}
myCount++;
ViewState["addmoreEdu"] = myCount;
//dynamicTextBoxes = new TextBox[myCount];
for (int i = 0; i < myCount; i++)
{
TextBox txtboxcustom = new TextBox();
Literal newlit = new Literal();
newlit.Text = "<br /><br />";
txtboxcustom.ID = "txtBoxcustom" + i.ToString();
myPlaceHolder.Controls.Add(txtboxcustom);
myPlaceHolder.Controls.Add(newlit);
dynamicTextBoxes = new TextBox[i];
}
}
You have to recreate the dynamical controls in Page_Load at the latest, otherwise the ViewState is not loaded correctly. You can however add a new dynamical control in an event handler(which happens after page_load in the page's lifefycle).
So addmoreCustom_Click is too late for the recreation of all already created controls, but it's not tool late to add a new control or to read the Text.
So something like this should work(untested):
public void Page_Load(object sender, EventArgs e)
{
if (ViewState["addmoreEdu"] != null)
{
myCount = (int)ViewState["addmoreEdu"];
}
addControls(myCount);
}
public void addmoreCustom_Click(object sender, EventArgs e)
{
if (ViewState["addmoreEdu"] != null)
{
myCount = (int)ViewState["addmoreEdu"];
}
myCount++;
ViewState["addmoreEdu"] = myCount;
addControls(1);
}
private void addControls(int count)
{
int txtCount = myPlaceHolder.Controls.OfType<TextBox>().Count();
for (int i = 0; i < count; i++)
{
TextBox txtboxcustom = new TextBox();
Literal newlit = new Literal();
newlit.Text = "<br /><br />";
txtboxcustom.ID = "txtBoxcustom" + txtCount.ToString();
myPlaceHolder.Controls.Add(txtboxcustom);
myPlaceHolder.Controls.Add(newlit);
}
}
Just enumerate the PlaceHolder-Controls to find your TextBoxes or use Linq:
private void saveData()
{
foreach (TextBox txt in myPlaceHolder.Controls.OfType<TextBox>())
{
string text = txt.Text;
// ...
}
}
Quick and dirty way would be to just iterate the Form collection looking for proper values:
if (Page.IsPostBack)
{
string name = "txtBoxcustom";
foreach (string key in Request.Form.Keys)
{
int index = key.IndexOf(name);
if (index >= 0)
{
int num = Int32.Parse(key.Substring(index + name.Length));
string value = Request.Form[key];
//store value of txtBoxcustom with that number to database...
}
}
}
To get values of dynamically created controls on postback you need to recreate those controls on Page_Init event
Then view state of those controls will be loaded and you will get controls and there values.
public void Page_Init(object sender, EventArgs e)
{
addControls(myCount);
}
I hope this will resolve your problem
Happy coding

Categories