I have a web application asp.net.
I am creating 54 text boxes dynamically in Page_Load
Here is the code
protected void Page_Load(object sender, EventArgs e)
{
for(i = 0, i<54, i++)
{
Textbox TestTextbox = new texbox();
TestTextBox.ID = "Reg" + i ;
TestTextBox.Attributes.add("runat","server");
TestTextBoxAttributes.Add("AutoPostBack", "true");
//display to a table called table1 created in the aspx page
}
}
On the page I have a button, called button1, and a on click event called "OnClickEvent", i want to capture the ID and the values of all the textboxes.
I have used Page.Controls.Count and I get only 1, the table that I have added to the aspx page, I get the IDs by using Request.Form but I`m not getting the values.
I am adding all the text boxes to a Table that I have created in the aspx file.
You can loop through the controls and check if they are of type TextBox:
for(int i = 0, i<54, i++)) {
var control = Page.FindControl("Reg" + i);
//get the value of the control
}
You are not adding the TextBox to the Page, so you cannot find it anyway. Second, adding runat=server and AutoPostBack=true as a string would not work either. (not to mention your snippet is full of errors)
//a loop uses ';', not ','
for (int i = 0; i < 54; i++)
{
//declare a new dynamic textbox = CaSe SeNsItIvE
TextBox TestTextbox = new TextBox();
TestTextbox.ID = "Reg" + i;
//if you want to add attibutes you do it like this
TestTextbox.AutoPostBack = true;
TestTextbox.TextChanged += TestTextbox_TextChanged;
//add the textbox to the page
PlaceHolder1.Controls.Add(TestTextbox);
}
And if you want to loop all the controls you can do something like this
//loop all the controls that were added to the placeholder
foreach (Control control in PlaceHolder1.Controls)
{
//is it a textbox
if (control is TextBox)
{
//cast the control back to a textbox to access it's properties
TextBox tb = control as TextBox;
string id = tb.ID;
}
}
Related
When I click on an item I would like to fill my TextBox with numbers from a column from Grid2 after Grid1 is clicked. Right now if I click on an item in Grid1 it will then run a procedure that will fill Grid2 with data. Here is an example of the current functionality with picture attached, I click an item from Grid1 and it has 3 results, my textbox will still display as an empty textbox. Then I click another item in Grid1 and now my textbox will display the 3 results from the previously clicked item. How can I display the correct numbers in my textbox after an item is selected in Grid1.
I have tried a few different methods including:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
divDetails.Visible = true;
RadTextBox1.Text = "";
for (int i = 0; i < Grid_Product_List_Details.Items.Count; i++)
{
RadTextBox1.Text += Grid_Product_List_Details.Items[i].GetDataKeyValue("number").ToString() + "\n";
}
}
and:
protected void Grid_Product_List_Details_PreRender(object sender, EventArgs e)
{
RadGrid grid = (RadGrid)sender;
if (grid.Items.Count > 0)
{
RadTextBox1.Text = "";
}
for (int i = 0; i < grid.Items.Count; i++)
{
RadTextBox1.Text += grid.Items[i].GetDataKeyValue("number").ToString() + "\n";
}
RadTextBox1.DataBind();
}
and this:
protected void Grid_Product_List_Header_SelectedIndexChanged(object sender, EventArgs e)
{
RadTextBox1.Text = "";
for (int i = 0; i < Grid_Product_List_Details.Items.Count; i++)
{
RadTextBox1.Text += Grid_Product_List_Details.Items[i].GetDataKeyValue("number").ToString() + "\n";
}
}
But those aren't working. Any suggestions?
Try to change your code a little bit.
RadTextBox1.Text = "";
foreach (GridDataItem dataItem in Grid_Product_List_Details.Items)
{
RadTextBox1.Text += dataItem.GetDataKeyValue("number").ToString() + "\n";
}
RadTextBox1.DataBind();
This should work though I suspect your earlier code should work too.
I ended up creating a procedure which fills the textbox after an item is clicked so the most recent data appears.
Try the ItemDataBound event of the second grid. You will have access to all of its data so you can put that in the RadTextBox. I suspect you provide a data source to the second grid in the SelectedIndexChanged of the first, so its Rebind() method should be called and then you will get the ItemDataBound fired for each of its rows.
Also, if you are using AJAX - disable it. It is possible that something does not get properly updated with the partial rendering. If things work fine with full postbacks, examine the responses and see why the textbox is not included in the first request so you can know how to tweak your AJAX setup.
I have on my winform an usercontrol and I create multiple usercontrols at every button click(at runtime).My usercontrol has an textbox. Also,on winform I have a simple textbox . I want ,when I select an usercontrol,the text from the dynamical textbox to appear also in the simple textbox. In my code it says that the textbox from usercontrol is not in the current context. My code:
private void Gettext()
{
int i = 0;
Control[] txt = Controls.Find("txtBox" + i.ToString(), true);//here I search for the dynamical textbox
foreach (Control c in panel1.Controls)
{
if (c is UserControl1)
{
if (((UserControl)c).Selected)
txtSimple.Text= txtBox[0].Text ;
}
i++;
}
I don't know if I understood your question correctly:
The structure of your form looks something like this:
Your form has a Panel panel1 that has many UserControls of the type UserControl1, created on runtime, and one TextBox txtSimple.
Every UserControl has a TextBox named ["txtBox" + i]
on select you want to synchronize texts of txtSimple and TextBox of selected UserControl
Then:
int i=0;
foreach (Control c in panel1.Controls)
{
if (c is UserControl1)
{
if (((UserControl)c).Selected)
{
TextBox dynTxtBox = (TextBox)c.Controls["txtBox" + i];
txtSimple.Text= dynTxtBoxe.Text;
}
}
i++;
}
If you can't find your TextBox this way, it probably means that its name is not set correctly.
Also, if you have only one TextBox on your UserControl then there's normally no need to name it in such a specific way (I mean from your code I assumed you have txtBox0 on your first user control, txtBox1 on your second and so on). You can simply name it "txtBox", then access it like this:
txtSimple.Text = selectedUserControl.Controls["txtBox"].Text;
Control names are unique in a Controls collection of a Control, UserControl and Form.
Control[] txt = ...
txtSimple.Text= txtBox[0].Text ;
May be replace txtBox[0].Text to txt[0].Text ?
Well for a start
Control[] txt = Panel1.Controls.Find("txtBox" + i.ToString(), true)
Then
foreach (Control c in txt) // txt???
{
UserControl1 uc = c as UserControl1;
if (uc != null)
{
if (uc.Selected) txtSimple.Text= uc.Text ;
}
}
Then if if you are are testing for UserControl1, you should also cast to UserControl1 not UserControl
UserControl1 is an extremely bad name for it..
I'm not even going to mention the assumption that all controls have a name starting with txtBox and that no other controls have...
And the entire thing dies if more than one control is selected when it runs.
you need to have a Selected event on your UserControl.
//in UserControl
public event EventHandler Selected;
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
if(Selected!=null)
Selected(this,null);
}
now subscribe to Selected event of UserControl when you dynamically create it. Like this:
UserControl control = new UserControl();
control.Selected += myControl_Selected;
private void myControl_Selected(object sender, EventArgs e)
{
UserControl control = (UserControl)sender;
textBox2.Text = control.Text;
}
I hope this helps.
I am working on a Project in C#, i need to insert text into textfields which are more then 250,
i stored the data in an array of string , now i have to insert data from array into these 250 textboxes in sequence like
textbox1.Text=StringArray[1];
textbox2.Text=StringArray[2];
. .
. .
. .
textbox250.Text=StringArray[250];
i google it no positive results ,
i did code to clear text from all the textboxes, i.e
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).Clear();
else
func(control.Controls);
};
func(Controls);
i tried to insert text like this
Action<Control.ControlCollection> func = null;
int i=0;
func = (controls) =>
{
foreach (Control control in controls)
{
if (control is TextBox)
(control as TextBox).Text = result_set[i++].ToString();
else
func(control.Controls);
}
};
func(Controls);
but got an exception of type 'System.IndexOutOfRangeException'.
The error is because you access a member outside of the array. It could be because you have other textboxes on the page which are found by the loop and then your array index runs out of range. Maybe you could do something like this:
for(int i = 1; i <= StringArray.Length; i++)
{
// I don't know which technology you use, it might be a different method to find
Control control = controlCollection.FindByName("Textbox" + i.ToString();
if (control is TextBox)
(control as TextBox).Text = StringArray[i];
}
You can add an attribute "index" to every textbox on your page with the index of the array, and attach a function to the event "OnInit" in order to insert the text. An example:
PAGE:
[asp:TextBox ID="TextBox1" runat="server" OnInit="setText" index="1"][/asp:TextBox]
[asp:TextBox ID="TextBox2" runat="server" OnInit="setText" index="2"][/asp:TextBox]
...
CODE BEHIND:
public void setText(object sender, System.EventArgs e) {
TextBox tbx;
tbx = sender;
tbx.Text = StringArray[sender.attributes["index"]];
}
Hope this helps!
Dynamic 10 textbox create all text value how to access in button click event in windows form application
The most simple way to do this is create a list to keep textbox's references.
List<TextBox> textBoxList = new List<TextBox>();
for (int index = 0; index < 10; index++)
{
var textBox = new TextBox();
textBoxList.Add(textBox);
// do the rest of work.
}
You can get its reference inside click event handler like below.
// inside button's click event.
foreach (var textBox in textBoxList)
{
// get text and do the work.
}
The simplest way assign something to the tag property that help you to identify the textbox. For example a number or an enum value.
Then casting the click event sender to a text box and look in the tag which one is it.
TextBox txt = new TextBox();
txt.Text = "ABC";
this.Controls.Add(txt);
private void btnOk_Click(object sender, EventArgs e)
{
foreach (Control ctl in this.Controls)
{
if (ctl.GetType() == typeof(TextBox))
MessageBox.Show(ctl.Text);
}
}
You can create an array of 10 text boxes dynamically place all the text boxes
You can access the text value based on the array values (0-9) of it
I am building a page with asp.net. I have a form with a table that contains TextBoxes and a submit button. When the form is submitted, I want to grab all the text that was entered into the TextBoxes and operate on them. To do this, I have the following method:
protected void Button1_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (Control c in this.Controls)
{
if (c.GetType().Name == "TextBox")
{
TextBox tb = (TextBox)c;
sb.AppendLine(tb.Text);
}
}
Label1.Text = sb.ToString();
}
The problem with this is that the controls apparently doesn't include any of my textboxes. When I iterate through the controls and print out their names, the only one I get is "site_master." (I also tried Controls and Page.Controls instead of this.Controls).
Is there something wrong with my iterator? Is there another way in which I could iterate through all of the textboxes in the table or page? What is the best way to accomplish this?
Would it be too much to build a List<Textbox>, given you know all your textbox controls?
List<Textbox> txtBoxes = new List<Textbox>();
txtBoxes.Add(tb1);
txtBoxes.Add(tb2);
//etc..
Then you have a nice list to work with
If I knew the controls were all in a given containing control, I would simply poll the controls of that control. For example, this.Form.Controls. However, if they could be nested within other child controls, then you could recursively explore the depths from a common outer container.
private IEnumerable<T> FindControls<T>(Control parent) where T : Control
{
foreach (Control control in parent.Controls)
{
if (control is T)
yield return (T)control;
foreach (T item in FindControls<T>(control))
yield return item;
}
}
So this would allow you to retrieve all TextBox children.
List<TextBox> textBoxes = this.FindControls<TextBox>(this).ToList();
string output = string.Join(",", textBoxes.Select(tb => tb.Text));
I'm going to assume that you are using web forms ASP.NET. Typically you declare your controls on the aspx page using something similar to
<asp:TextBox ID="someId" runat="server/>
If you have done this then in your code behind your should just be able to reference the variable someId and the property Text to get/set the text in the control.
If you are building the controls dynamically on the server you should be able to stick them in a list and iterate through it. Make sure you are creating the controls and adding them to the table during the correct part of the page lifecycle. When you add them to a cell in the table you could also keep a reference to the control in a list and just enumerate through that list in your event handler.
Maybe something along the lines of (I didn't compile this so there are probably issues):
public class MyPage: Page
{
private List<TextBox> TxtBoxes = new List<TextBox>();
//registered for the preinit on the page....
public void PreInitHandler(object sender, EventArgs e)
{
for(var i = 0; i < 2; i++)
{
var txtBox = new TextBox{Id = textBox+i};
//...add cell to table and add txtBox Control
TxtBoxes.Add(txtBox);
}
}
}