My requirement is to count the total number of TextBox and CheckBox present directly inside the form with the id="form1", when the user clicks on the Button 'btnGetCount'. Here is the code which I have tried but it doesn't count anything and counter remains at zero, though I have three TextBoxes and two CheckBoxes in the form. However, if I remove foreach loop and pass TextBox control = new TextBox(); instead of the present code then it counts the first TextBox and countTB returns the value as one.
protected void btnGetCount_Click(object sender, EventArgs e)
{
Control control = new Control();
int countCB = 0;
int countTB = 0;
foreach (Control c in this.Controls)
{
if (control.GetType() == typeof(CheckBox))
{
countCB++;
}
else if (control is TextBox)
{
countTB++;
}
}
Response.Write("No of TextBoxes: " + countTB);
Response.Write("<br>");
Response.Write("No of CheckBoxes: " + countCB);
}
You must recursively loop through other controls.
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:CheckBox ID="cb" runat="server"></asp:CheckBox>
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
var controls = form1.Controls;
var tbCount = 0;
var cbCount = 0;
CountControls(ref tbCount, controls, ref cbCount);
Response.Write(tbCount);
Response.Write(cbCount);
}
private static void CountControls(ref int tbCount, ControlCollection controls, ref int cbCount)
{
foreach (Control wc in controls)
{
if (wc is TextBox)
tbCount++;
else if (wc is CheckBox)
cbCount++;
else if(wc.Controls.Count > 0)
CountControls(ref tbCount, wc.Controls, ref cbCount);
}
}
It allows gives you zero since you are counting the type of your Control control which is not available change your code to :
protected void btnGetCount_Click(object sender, EventArgs e)
{
int countCB = 0;
int countTB = 0;
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(CheckBox))
{
countCB++;
}
else if (c.GetType()== typeof(TextBox))
{
countTB++;
}
}
Response.Write("No of TextBoxes: " + countTB);
Response.Write("<br>");
Response.Write("No of CheckBoxes: " + countCB);
}
I believe you have to be recursive, this.Controls will only return the controls that are its immediate children. If the TextBox is within a control group within there, you will need to see the containers controls as well.
See the answer to this other stackoverflow: How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?
EDIT:
I realize the answer is for WinForms, but the solution still applies.
Just by doing some minor change in the code suggested by Alyafey, pcnThird and Valamas I wrote this code which works.
protected void btnGetCount_Click(object sender, EventArgs e)
{
int countCB = 0;
int countTB = 0;
foreach (Control c in form1.Controls) //here is the minor change
{
if (c.GetType() == typeof(CheckBox))
{
countCB++;
}
else if (c.GetType()== typeof(TextBox))
{
countTB++;
}
}
Response.Write("No of TextBoxes: " + countTB);
Response.Write("<br>");
Response.Write("No of CheckBoxes: " + countCB);
}
Related
I started this question and was able to get an answer to my original question. Now the textbox gets removed but only the second time I click the remove button. Here is what I have tried
protected void btnRemoveTextBox_Click(object sender, EventArgs e)
{
foreach (Control control in PlaceHolder1.Controls)
{
var tb = new TextBox();
tb.ID = "Textbox" + counter;
if ((control.ID == tb.ID.ToString()) && (control.ID != null))
{
controlIdList.Remove(tb.ID);
ViewState["controlIdList"] = controlIdList;
}
}
}
When I step through using breakpoints and error debugging the code runs through twice without error however on the second time through it removes the button.
Because you created and added textboxes in LoadViewState method (earlier in the the page's life cycle), and here only remove an id from controlIdList but not from the control tree. Note: you do not need to create new TextBox instances in btnRemoveTextBox_Click method.
protected void btnRemoveTextBox_Click(object sender, EventArgs e)
{
foreach (Control control in PlaceHolder1.Controls)
{
string id = "Textbox" + counter;
if (control.ID == id)
{
controlIdList.Remove(id);
PlaceHolder1.Controls.Remove(control);
break;
}
}
}
I have a tab control where I can add custom controls made from a button and a label underneath. I want to add a search function to my project so that when a user type a name of a control, it will show all controls where the name (label) starts with the letters typed. Also typing in a textbox will do the job.. Is there an easy way to do this?
You can search controls within parent control collection:
foreach(Control c in ParentControl.Controls)
{
if(c.Name == "label1")
{
//add to your list
}
}
You can also check using StartsWith("stringVal")
if(c.Name.StartsWith("l"))
{
//add to your list
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (Control control in this.Controls)
{
// Skip, if the control is the used TextBox
if (control == textBox1) { continue; }
// Show all controls where name starts with inputed string
// (use ToLower(), so casing doesnt matter)
if (control.Name.ToLower().StartsWith(textBox1.Text.Trim().ToLower()))
{
control.Visible = true;
}
// Hide objects that doesn't match
else { control.Visible = false; }
}
}
This toggles the controls' visibility, and hide's all items, that doesn't match the given input. Also casing doesn't matter.
Done when adding the text to search in a textbox.
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (Control c in fl_panel.Controls)
{
if (c.Name.ToUpper().StartsWith(textBox1.Text.ToUpper().ToString()) && textBox1.Text != "")
{
Control[] ctrls = fl_panel.Controls.Find(textBox1.Text.ToString(), true);
c.Visible = true; // to restore previous matches if I delete some text
}
else if(textBox1.Text == "")
{
c.Visible = true;
}
else
{
c.Visible = false;
}
}
}
you can try this to find the controls that matches a text typed in the textbox, after that you can do whatever you want with it.
private void textBox1_TextChanged(object sender, EventArgs e)
{
var controlMatchesCriteria = from c in this.Controls.OfType<TextBox>()
where c.Name == textBox1.Text
select c;
}
I have a number of different tabs and within the tabs i have some radio buttons. I only want one radio button at a time within the tab pages to be selected. I have the following code
private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
TabControl.TabPageCollection pages = tabControl1.TabPages;
var rdoButtonName = sender as RadioButton;
foreach (TabPage page in pages)
{
foreach (Control item in page.Controls)
{
if (item is RadioButton)
{
if (rdoButtonName.Name == item.Name)
{
rdoButtonName.Checked = true;
}
else
{
rdoButtonName.Checked = false;
}
}
}
}
}
When i go to click on the radio buttons on the first tab page it does not allow me to change their checked status but it does on the other pages. Can anyone see anything wrong with the above code?
I see that you set Cheked value to the sender and not to other radio buttons...
private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
TabControl.TabPageCollection pages = tabControl1.TabPages;
var rdoButtonName = sender as RadioButton;
foreach (TabPage page in pages)
{
foreach (Control item in page.Controls)
{
if (item is RadioButton)
{
if (item.Name != rdoButtonName.Name)
{
item.Checked = false;
}
}
}
}
}
I think (better debug and check) that your problem is that each time you change one of your RadioButton state it will call this function over again and will make the unexpected results.
To fix that I would detach and reattach your event handler before changing the Checked state, like this:
private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
TabControl.TabPageCollection pages = tabControl1.TabPages;
var rdoButtonName = sender as RadioButton;
foreach (TabPage page in pages)
{
foreach (Control item in page.Controls)
{
if (item is RadioButton)
{
rdoButtonName.CheckedChanged -= new System.EventHandler(this.RadioButtonCheckedChanged);
if (rdoButtonName.Name == item.Name)
{
rdoButtonName.Checked = true;
}
else
{
rdoButtonName.Checked = false;
}
rdoButtonName.CheckedChanged += new System.EventHandler(this.RadioButtonCheckedChanged);
}
}
}
}
The problem is, that "CheckedChanged"-event can't be used in this kind of situation, and that is where the problem lies. The key is to use another event - "Click":
private void radioButton_Click(object sender, EventArgs e)
{
foreach (TabPage page in tabControl1.TabPages)
{
foreach (RadioButton radioButton in page.Controls)
{
if (radioButton != (RadioButton)sender) { radioButton.Checked = false; }
}
}
}
Change your RadioButtons to use this method in the "Click"-event and it'll work. I tested it myself.
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);
}
I was wondering, is there a way I can reset all the checkboxes, textboxes, numerics and other controls back to the default values without writing code for every control individually? This is the code I've tried, but doesn't seem to work:
for (int i = 0; i < this.Controls.Count; i++)
{
this.Controls[i].ResetText();
}
EDIT:
I've fixed it by manually setting the control values, sorry for all the trouble >.<.
Do as below create class and call it like this
Check : Reset all Controls (Textbox, ComboBox, CheckBox, ListBox) in a Windows Form using C#
private void button1_Click(object sender, EventArgs e)
{
Utilities.ResetAllControls(this);
}
public class Utilities
{
public static void ResetAllControls(Control form)
{
foreach (Control control in form.Controls)
{
if (control is TextBox)
{
TextBox textBox = (TextBox)control;
textBox.Text = null;
}
if (control is ComboBox)
{
ComboBox comboBox = (ComboBox)control;
if (comboBox.Items.Count > 0)
comboBox.SelectedIndex = 0;
}
if (control is CheckBox)
{
CheckBox checkBox = (CheckBox)control;
checkBox.Checked = false;
}
if (control is ListBox)
{
ListBox listBox = (ListBox)control;
listBox.ClearSelected();
}
}
}
}
You can create the form again and dispose the old one.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnReset_Click(object sender, EventArgs e)
{
Form1 NewForm = new Form1();
NewForm.Show();
this.Dispose(false);
}
}
foreach (Control field in container.Controls)
{
if (field is TextBox)
((TextBox)field).Clear();
else if (field is ComboBox)
((ComboBox)field).SelectedIndex=0;
else
dgView.DataSource = null;
ClearAllText(field);
}
Additional-> To clear the Child Controls
The below function would clear the nested(Child) controls also, wrap up in a class.
public static void ClearControl(Control control)
{
if (control is TextBox)
{
TextBox txtbox = (TextBox)control;
txtbox.Text = string.Empty;
}
else if (control is CheckBox)
{
CheckBox chkbox = (CheckBox)control;
chkbox.Checked = false;
}
else if (control is RadioButton)
{
RadioButton rdbtn = (RadioButton)control;
rdbtn.Checked = false;
}
else if (control is DateTimePicker)
{
DateTimePicker dtp = (DateTimePicker)control;
dtp.Value = DateTime.Now;
}
else if (control is ComboBox)
{
ComboBox cmb = (ComboBox)control;
if (cmb.DataSource != null)
{
cmb.SelectedItem = string.Empty;
cmb.SelectedValue = 0;
}
}
ClearErrors(control);
// repeat for combobox, listbox, checkbox and any other controls you want to clear
if (control.HasChildren)
{
foreach (Control child in control.Controls)
{
ClearControl(child);
}
}
}
If you have some panels or groupboxes reset fields should be recursive.
public class Utilities
{
public static void ResetAllControls(Control form)
{
foreach (Control control in form.Controls)
{
RecursiveResetForm(control);
}
}
private void RecursiveResetForm(Control control)
{
if (control.HasChildren)
{
foreach (Control subControl in control.Controls)
{
RecursiveResetForm(subControl);
}
}
switch (control.GetType().Name)
{
case "TextBox":
TextBox textBox = (TextBox)control;
textBox.Text = null;
break;
case "ComboBox":
ComboBox comboBox = (ComboBox)control;
if (comboBox.Items.Count > 0)
comboBox.SelectedIndex = 0;
break;
case "CheckBox":
CheckBox checkBox = (CheckBox)control;
checkBox.Checked = false;
break;
case "ListBox":
ListBox listBox = (ListBox)control;
listBox.ClearSelected();
break;
case "NumericUpDown":
NumericUpDown numericUpDown = (NumericUpDown)control;
numericUpDown.Value = 0;
break;
}
}
}
You can reset all controls of a certain type. Something like
foreach(TextBox tb in this.Controls.OfType<TextBox>().ToArray())
{
tb.Clear();
}
But you can't reset all controls at once
Quick answer, maybe it'll help:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
while (f2.DialogResult == DialogResult.Retry)
{
f2 = new Form2();
f2.ShowDialog();
}
}
and in Form2 (The 'settings' Form):
private void button1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Retry;
Close();
}
I recently had to do this for Textboxes and Checkboxes but using JavaScript ...
How to reset textbox and checkbox controls in an ASP.net document
Here is the code ...
<script src="http://code.jquery.com/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
function ResetForm() {
//get the all the Input type elements in the document
var AllInputsElements = document.getElementsByTagName('input');
var TotalInputs = AllInputsElements.length;
//we have to find the checkboxes and uncheck them
//note: <asp:checkbox renders to <input type="checkbox" after compiling, which is why we use 'input' above
for(var i=0;i< TotalInputs ; i++ )
{
if(AllInputsElements[i].type =='checkbox')
{
AllInputsElements[i].checked = false;
}
}
//reset all textbox controls
$('input[type=text], textarea').val('');
Page_ClientValidateReset();
return false;
}
//This function resets all the validation controls so that they don't "fire" up
//during a post-back.
function Page_ClientValidateReset() {
if (typeof (Page_Validators) != "undefined") {
for (var i = 0; i < Page_Validators.length; i++) {
var validator = Page_Validators[i];
validator.isvalid = true;
ValidatorUpdateDisplay(validator);
}
}
}
</script>
And call it with a button or any other method ...
<asp:button id="btnRESET" runat="server" onclientclick="return ResetForm();" text="RESET" width="100px"></asp:button>
If you don't use ValidationControls on your website, just remove all the code refering to it above and the call Page_ClientValidateReset();
I am sure you can expand it for any other control using the DOM. And since there is no post to the server, it's faster and no "flashing" either.
function setToggleInputsinPnl(pnlName) {
var domCount = pnlName.length;
for (var i = 0; i < domCount; i++) {
if (pnlName[i].type == 'text') {
pnlName[i].value = '';
} else if (pnlName[i].type == 'select-one') {
pnlName[i].value = '';
}
}
}
There is a very effective way to use to clear or reset Windows Form C# controls like TextBox, ComboBox, RadioButton, CheckBox, DateTimePicker etc.
private void btnClear_Click(object sender, EventArgs e)
{
Utilities.ClearAllControls(this);
}
internal class Utilities
{
internal static void ClearAllControls(Control control)
{
foreach (Control c in control.Controls)
{
if (c is TextBox)
{
((TextBox)c).Clear();
}
else if (c is ComboBox)
{
((ComboBox)c).SelectedIndex = -1;
}
else if (c is RadioButton)
{
((RadioButton)c).Checked = false;
}
else if (c is CheckBox)
{
((CheckBox)c).Checked = false;
}
else if (c is DateTimePicker)
{
((DateTimePicker)c).Value = DateTime.Now; // or null
}
}
}
}
To accomplish this with overall user experience in c# we can use one statement to clear them. Pretty straight forward so far, above is the code.