I have created a panel in my page view (tour.aspx file).
Now I want to access it in my class file (add_tour.cs file).
This is my panel:
<asp:Panel ID="itinerary_panel" runat="server"></asp:Panel>
This is my code behind tour.aspx file:
add_tour tour_obj = new add_tour();
int days_count = 2;
tour_obj.textbox_generator(days_count);
And this code is in add_tour.cs file:
public void textbox_generator(int days_count)
{
}
Now how to access the panel from aspx file?
Please help.
There's no need to actually add the text boxes to the panel from this class.
public List<TextBox> textbox_generator(int days_count)
{
var textBoxes = new List<TextBox>();
for(int i = 0; i < days_count; i++)
{
txt_desc = new TextBox();
txt_desc.ID = "txt_desc" + i.ToString();
txt_desc.CssClass = "form-control";
txt_desc.Attributes.Add("placeholder", "Enter day " + i + " description");
txt_desc.TextMode = TextBoxMode.MultiLine;
textBoxes.Add(txt_desc);
}
return textBoxes;
}
Then change your code behind to:
add_tour tour_obj = new add_tour();
int days_count = 2;
var textBoxes = tour_obj.textbox_generator(days_count);
foreach(var textBox in textBoxes)
{
itinerary_panel.Controls.Add(textBox);
}
Note that you need to be careful where you add these controls in the page lifecycle. See Microsoft documentation.
This keeps your textbox_generator from needing to know anything about the specific page using it.
Also, you should really align your naming conventions with C# standards. Use PascalCasing. textbox_generator should be TextBoxGenerator etc. And you can probably make textbox_generator into a static method if it doesn't need to access any fields or properties of its class.
If you really wanted your other class to itself add the controls directly to the panel, then you would just pass a reference to the panel from the code behind to the class.
public void textbox_generator(int days_count, Panel panel)
{
for(int i = 0; i < days_count; i++)
{
txt_desc = new TextBox();
txt_desc.ID = "txt_desc" + i.ToString();
txt_desc.CssClass = "form-control";
txt_desc.Attributes.Add("placeholder", "Enter day " + i + " description");
txt_desc.TextMode = TextBoxMode.MultiLine;
panel.Controls.Add(txt_desc);
}
}
and call this this way from your code behind:
add_tour tour_obj = new add_tour();
int days_count = 2;
var textBoxes = tour_obj.textbox_generator(days_count, itinerary_panel);
This works because itinerary_panel actually is a reference to the panel. See Passing Objects By Reference or Value in C#. However, it's often a bad idea to have a method modify the state in that manner.
Related
In my program i'm currently working on programmatically adding a variety of form objects in C#. For example i'm trying to create a certain group of panels, which contain the data I wish to show. When loading the form I have a file which contains all of the data I wish to load in, the loading itself works fine but when loading I need to create a variety of form labels, panels and images to display all of the necessary data and as such I need to make these panels and labels all with a seperate name, but programmatically.
for (int i=0; i<fileLength; i++)
{
Panel pnl = New Panel();
pnl.Name = "pnl1"+i;
//Set the other properties here
}
What i'm trying to do if use an iterator to append the current iteration to the end of the name. Am I going the right way about it? Or should I be doing a different method?
You cannot change variable/object name at runtime. If you want to write code against the object than you need to keep a reference to it. In your case you have changed the Name property of Panel but still you have to use it as pnl, not as pnl0 or pnl1.
The other way for doing this would be to use a Dictionary with key as the name as you assign and value as the object itself. This will help you in accessing your controls using its name that you have assigned to it.
Dictionary<string, Panel> panels = new Dictionary<string, Panel>();
for (i = 0; i <= 10; i++) {
Panel pnl = new Panel();
panels.Add("pnl" + i.ToString(), pnl);
}
//write code against panels
panels("pnl0").Width = 100;
For accessing within loop:
foreach (string pnlName in panels.Keys) {
panels(pnlName).Visible = true;
}
for (i = 0; i <= 10; i++) {
panels("pnl" + i).Visible = true;
}
I don't know whether it is clear. I mean a form has an input textbox and a button. If I input 5 in the textbox and click on the button, the form will add 5 labels...
The question is I don't know it is 5 or 4 or 3……before the code is running and the input.
I don't know how to add the labels and how to define or get their names in order to use them later in the code.
I am just learning windows applications development with VS using C#....
And also this is my first ask in stackoverflow please forgive me if it is not clear. Is there anybody can help me?
let's split your entire problem into few steps of understanding:
What basically down the line, you are asking, is to how to add controls dynamically in a winform, in your case the control is label, so wrap your label creating logic in a function like below:
protected Label CreateLabel(string Id, string text)
{
Label lbl = new Label();
lbl.Name = Id;
lbl.Text = text;
return lbl;
}
Now you need to add as many labels as the number entered in a given textBox and upon a button click, so possibly something like below in button's click event:
protected void button_Clicked(object sender, EventArgs e)
{
//make sure nothing invalid string comes here
int counter = Convert.ToInt32(txtCount.text);
for(int i=0;i<counter;i++)
{
var lbl = CreateLabel("rand"+i, "Label" +i);
container.Controls.Add(lbl);//container can be your form
}
}
Now the basic problem in winforms you will face, will be about the positioning of these dynamically added labels. The most simple way to go about it is to add your labels to winforms FlowLayoutPanel. It automatically aligns the controls. There are other layout controls available aswell. so do this :
drag and drop a FlowLayoutPanel on your form and give it the name "container", rest assured
For example:
for(var i=0; i<N; i++ ) {
var l= new Label();
l.Text = "some name #" + i.ToString();
l.Width = 200;
l.Location = new Point(30, 20);
parent.Controls.Add(l);
}
You can use this as:
Label[] arrLabel;
int num = 0;
int.TryParse(textBox1.Text, out num);
arrLabel = new Label[num];
for (int i = 0; i < num; i++)
{
arrLabel[i] = new Label();
arrLabel[i].Text = "Label #" + (i+1);
arrLabel[i].Width = 20;
arrLabel[i].Location = new Point(30+10*(i+1), 20);
this.Controls.Add(arrLabel[i]);
}
First off, I've seen a ton of posts for this same question but what I don't understand is when somebody gives an answer about "recreating the controls to page init" or ... I have the code to dynamically CREATE the text boxes but I'm not sure what else I need to add. I don't completely understand the page life cycle of asp.net web apps. I've googled this and I dont know if I'm incompetent or if all of the answers given are for people with more understanding than me.
PLEASE provide an example of what you explain.
Basically The user enteres a # into the textbox for how many "favorite books" they want to save into the database, he/she clicks the generate button.
that # of rows will populate with two textboxes, one for title and one for author. Then I would have a button they click that would save the textbox values into the database.
I know it's a simple exercise but I'm new to asp.net and it's just an exercise I came up by myself that I'm trying to learn. I'm open to new design for this but the one thing I prefer not to do is create the textboxes statically. Thanks! <3
this is the asp.net code I have
<form id="form1" runat="server">
<div>
How many favorite books do you have ?
<asp:TextBox ID="TextBox1" runat="server" Width="50px"></asp:TextBox>
<br />
<asp:Button ID="btnBookQty" runat="server" Text="GenerateBooks" OnClick="btnBookQty_Click" />
<br />
<br />
<asp:Panel ID="pnlBooks" runat="server"></asp:Panel>
</div>
</form>
and my c# code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class databasetest_panels_favBookWebsite : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnBookQty_Click(object sender, EventArgs e)
{
int count = Convert.ToInt32(TextBox1.Text);
for (int i = 1; i <= count; i++)
{
TextBox tb = new TextBox();
TextBox tb2 = new TextBox();
tb.Text = "Book " + i.ToString() + " Title";
tb2.Text = "Book " + i.ToString() + " Author";
tb.ID = "TextBoxTitle" + i.ToString();
tb2.ID = "TextBoxAuthor" + i.ToString();
pnlBooks.Controls.Add(tb);
pnlBooks.Controls.Add(tb2);
pnlBooks.Controls.Add(new LiteralControl("<br />"));
}
}
}
You don't really need to keep track of your programmatically or dynamically added controls. It's true that somehow someone has to keep track of them, but it's not you who should be doing so.
Understanding the Page Life-cycle in ASP.NET Web Forms is a must for any ASP.NET developer. Sooner or later you'll run into this sort of problems that would be greatly simplified if you really understood the underlying mechanics of the page.
Each time a request is made to the server, the whole page must be assembled from scratch. You can read (please do) on the web the many steps or stages that make up the Page Life-cycle for an ASP.NET page, but for you to have a rough idea on how this works:
When the request is made, the .aspx file is parsed and a memory source code created from it. This stage is known as the Instantiation stage, and the page's control hierarchy is created at this point.
After that, page goes through the Initialization phase, in which the page's Init event is fired. This is "the place" to add controls dynamically.
LoadViewState phase comes next. At this point, the information of the controls that are part of the page's control hierarchy get their "state" updated to make them return to the state they were before the postback. (This stage doesn't happen on the first time the page is accessed, it's a postback-only stage).
LoadPostData phase is when the data that has been posted to the server (by submitting the form) is loaded into controls. This stage is barely known to beginner ASP.NET developers, because they assume that all "state preservation" that is automatically enforced by ASP.NET engine comes from the magical Viewstate.
NOTE: If you are really serious about this, you can learn A LOT from this guy here: Understanding ASP.NET View State
What you need to remember from all the above now is that: in order for your dynamically generated controls to "have" their data "glued" together into the control's state after submitting the form by clicking this button Then I would have a button they click that would save the textbox values into the database., you need to add the controls to the page at each round-trip to the server.
The recommended way of doing so is in the Page_Init, because it comes before the LoadViewsate and LoadPostData stages where the control's state is populated.
In your case though, you don't know how many controls to add until the user fills that information on the first form submission. So, you need to find a way to add the controls to the page each time the page loads after the user entered the number of desired controls.
NOTE: You could get away with adding the controls on the btnBookQty_Click and have their data preserved correctly, because ASP.NET "plays catch-up" on the controls, but that's beyond the scope and purpose of this answer.
Add a private field to act as a boolean flag and to indicate the number of controls to add.
Create a private method that add the controls into the page, taking as argument the number of controls to add.
Call that method from within the Page_Init event handler, only if the flag dictates that some fields must be added.
In btnBookQty's click event handler set the flag to the number provided by the user of the page, and...
Call the method to create the dynamically generated controls from within btnBookQty_Click.
Here's a template code of what you need. Notice how HowManyControls property is stored in the Session to "remember" that value across postbacks:
private int HowManyControls {
get
{
if (Session["HowManyControls"] == null) return 0;
else return (int)Session["HowManyControls"];
}
set
{
Session["HowManyControls"] = value;
}
}
protected void Page_Init(object sender, EventArgs e)
{
if (Page.Ispostback && HowManyControls > 0)
{
//generate the controls dynamically
GenerateControls(HowManyControls);
}
}
protected void btnBookQty_Click(object sender, EventArgs e)
{
//get the number of controls to generate dynamically from the user posted values
HowManyControls = Convert.ToInt32(TextBox1.Text);
//generate the controls dynamically
GenerateControls(HowManyControls);
}
protected void btnSaveToDatabase_Click(object sender, EventArgs e)
{
//iterate on the control's collection in pnlBook object.
for (int i = 1; i <= HowManyControls; i++)
{
//save those value to database accessing to the control's properties as you'd regularly do:
TextBox tb = (TextBox)pnlBooks.FindControl("TextBoxTitle" + i.ToString());
TextBox tb2 = (TextBox)pnlBooks.FindControl("TextBoxAuthor" + i.ToString();
//store these values:
tb.Text;
tb2.Text;
}
}
private void GenerateControls(int count)
{
if (count == 0) { return; }
for (int i = 1; i <= count; i++)
{
TextBox tb = new TextBox();
TextBox tb2 = new TextBox();
tb.Text = "Book " + i.ToString() + " Title";
tb2.Text = "Book " + i.ToString() + " Author";
tb.ID = "TextBoxTitle" + i.ToString();
tb2.ID = "TextBoxAuthor" + i.ToString();
pnlBooks.Controls.Add(tb);
pnlBooks.Controls.Add(tb2);
pnlBooks.Controls.Add(new LiteralControl("<br />"));
}
}
EDIT
I had forgotten that ViewState is not available during Page_Init. I've now modified the answer to use Session instead.
When somebody says that "You need to keep track of the controls you create or recreate them" it means that you need to store them between postbacks.
In ASP.NET persistance can means Session variables, ViewStates and other means
See this link http://msdn.microsoft.com/en-us/magazine/cc300437.aspx
Create a list of the textboxes that you created and store it on a Session
private void CreateOrLoadTextBoxes(int numTextBoxes)
{
List<TextBox> lstControls ;
//if its the first time the controls need to be created
if(Session["lstTitleControls"] == null)
{
lstTbTitles = new List<TextBox>(numTextBoxes) ;
lstTbAuthors = new List<TextBox>(numTextBoxes) ;
//create the controls for Book Titles
for (int i = 1; i <= numTextBoxes; i++)
{
TextBox tb = new TextBox();
tb.Text = "Book " + i.ToString() + " Title";
tb.ID = "TextBoxTitle" + i.ToString();
lstTbTitles.Add(tb) ;
}
//Create the controls for Author
for (int i = 1; i <= numTextBoxes; i++)
{
TextBox tb2 = new TextBox();
tb2.Text = "Book " + i.ToString() + " Author";
tb2.ID = "TextBoxAuthor" + i.ToString();
lstTbAuthors.Add(tb2) ;
}
//store the created controls on ViewState asociated with the key "lstTitleControls" and "lstAuthorControls"
// each time you store or access a ViewState you a serialization or deserialization happens which is expensive/heavy
Session["lstTitleControls"] = lstTbTitles ;
Session["lstAuthorControls"] = lstTbAuthors ;
}
else
{
//restore the list of controls from the ViewState using the same key
lstTbTitles = (List<TextBox>) Session["lstTitleControls"];
lstTbAuthors = (List<TextBox>) Session["lstAuthorControls"];
numTextBoxes = lstTbTitles.Count() ;
}
//at this moment lstTbTitles and lstTbAuthors has a list of the controls that were just created or recovered from the ViewState
//now add the controls to the page
for (int i = 1; i <= numTextBoxes; i++)
{
pnlBooks.Controls.Add(lstTbTitles[i]);
pnlBooks.Controls.Add(lstTbAuthors[i]);
pnlBooks.Controls.Add(new LiteralControl("<br />"));
}
}
protected void Page_Load(object sender, EventArgs e)
{
CreateOrLoadTextBoxes(10) ;
}
As you have noticed I am calling the CreateOrLoadTextBoxes with a fixed value of 10
Is up to you to chane this code to take the value from the text box and call this as needed
I need to retrieve the text from a textbox within a custom control. The custom control is part of an ArrayList so there can be multiple custom controls displayed on a Form. How do I access the text from a single textbox within one of the controls in the ArrayList?
The code below shows how I'm creating the dynamic custom control:
ArrayList assessmentInfo = new ArrayList();
int length = (int)moduleInfoLevel6.numericUpDownModuleAssessmentNum.Value;
for (int i = 0; i < length; i++)
{
assessmentInfo.Add(new AssessmentInfo());
System.Drawing.Point p = new System.Drawing.Point(10, 160 + i * 32);
(assessmentInfo[i] as AssessmentInfo).Location = p;
(assessmentInfo[i] as AssessmentInfo).Size = new System.Drawing.Size(440, 32);
tabPageLevel6.Controls.Add((assessmentInfo[i] as AssessmentInfo));
}
Here are screenshots showing how the custom control is displayed:
assessmentInfo custom control
http://i.imgur.com/9qwCL.jpg
How the custom control is displayed on the form
http://i.imgur.com/DFJYV.jpg
you could add a name to each control
something like:
(assessmentInfo[i] as AssessmentInfo).Name = "assessmentInfo" + i.ToString();
then you could access it in the following way
foreach (object control in tabPageLevel6.Controls)
{
if (control is AssessmentInfo)
{
if ((control as AssessmentInfo).Name == "assessmentInfo1")
// do something with the control
MessageBox.Show((control as AssessmentInfo).Name);
}
}
I am creating an application where a user will input grades and the program will output the weighted average. On load, it will ask for the number of categories for the assignments. The program will then dynamically create textboxes for the user to input information. The problem is that I can not figure out how to read the text that is inputed after I create the textboxes. Here is my code:
TextBox txtbx = new TextBox();
txtbx.Text = "";
txtbx.Name = "txtbx1";
txtbx.Location = new Point(10, 10);
txtbx.Height = 20;
txtbx.Width = 50;
Controls.Add(txtbx);
How can I change this code so I can find the current text in the box when the user submits?
If you are dynamically generating controls then obviously you won't be able to have a field for each one. But if you are trying to access the Controls collection for a named control, the ControlCollection can be indexed by name. After adding the text box with the specified name, you can simply do:
TextBox txtbx = (TextBox)Controls["txtbx1"];
You could use the FindControl method of the Page class.
This method takes a parameter which is the TextBox's ID, which you have to set upon creation:
txtbx.ID = "txtbx1";
Then you can select it:
TextBox txtbx1 = (TextBox)FindControl("txtbx1");
and use it.
Edit: Since the initial question added that he is refering to Windows Forms, my reply above is off-topic.
In Windows Forms, you should simply use a class member variable instead of a local variable. E.g.:
public partial class MyForm
{
...
private TextBox txtbx;
...
private void createControls()
{
txtbx = new TextBox();
txtbx.Text = "";
txtbx.Name = "txtbx1";
txtbx.Location = new Point(10, 10);
txtbx.Height = 20;
txtbx.Width = 50;
Controls.Add(txtbx);
}
private void someOtherFunction()
{
// Do something other with the created text box.
txtbx.Text = "abc";
}
}
This code for the Dynamically Add Textbox On Button Click
int count = 1;
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
txt.Top = count * 25;
txt.Left = 100;
txt.Text = "TextBox " + this.count.ToString();
count = count + 1;
return txt;
}
private void Onbutton_Click(object sender, EventArgs e)
{
//Call the method AddNewTextBox that uses for Dynamically create Textbox
AddNewTextBox();
}
I hope this code will help you .
Thank You
Happy Coding:)
Keep a list of references of all text boxes on the form. Add the textBox reference to the list when you create them dynamically.
Then you can simply iterate through all text boxes in the list when you want to read their text.
Make sure that you name the text boxes as per their related category names. Then you can also Find the control in the list by their names.
class MyForm : Form
{
IList<TextBox> _textBoxes = new List<TextBox>();
private void AddTextBox(string categoryName){
var myTextBox = new TextBox();
myTextBox .Name = categoryName + "txtbx";
// set other properties and add to Form.Controls collection
_textBoxes.Add(myTextBox);
}
private TextBox FindTextBox(string categoryName)
{
return _textBoxes.Where( t => t.Name.StartsWith(categoryName)).FirstOrDefault();
}
}
All you need to do is set up an OnClick listener for your submit button and have it do something like this
private void OnSubmit(object sender, EventArgs args)
{
string yourText = txtbx.Text;
}
You'll have to keep a reference to the text box after you create it. yourText will contain the value you need. Hope this helps