I have a page where I create many dynamic controls, the controls are deeply nested. I have no idea how to maintain/persist their value/state. The controls are created upon dropdown value changed event:
for(int i = 0; i < n; i++)
{
// Here I create radio button and add them
RadioButton rd = new RadioButton();
rd.id = "rd" + i;
for(int j = 0; j < 5; j++)
{
// Here I create textbox and add them to page
TextBox tbx = new TextBox();
tbx.id = "tbx" + j + i;
}
}
Related
for(int i = 1; i < 5;i++)
{
label[i].InnerText = info.books[0].title;
}
I want something like this to access id's of 5 server controls in loop.
label1,label2,.....,label5. It is a code behind file.
If you really wanted you can use FindControl
for (int i = 1; i < 5; i++)
{
Label lbl = Page.FindControl("Label" + i) as Label;
lbl.Text = info.books[0].title;
}
I have a tableLayoutPanel called tlpMaster. It has 4 columns and 8 rows. in 3 of the columns and each row, it has another tableLayoutPanel. so thats 3 inner ones. Inside those I have checkboxes that I need to be able to dispose. Here is what I got so far but it seems to be disposing the tableLayoutPanels and not the checkboxes.
for (int i = 0; i < 8; i++)
{
for (int j = 1; j < 4; j++)
{
//loop throught the table layout panels and dispose
Control tlpTemp = tlpMaster.GetControlFromPosition(j, i);
while (tlpTemp.Controls.Count > 0)
{
tlpTemp.Controls[0].Dispose();
}
}
}
What am I doing wrong here?
Figured it out
for (int i = 0; i < 8; i++)
{
for (int j = 1; j < 4; j++)
{
//loop throught the table layout panels and dispose
Control tlpTemp = tlpMaster.GetControlFromPosition(j, i);
foreach(Control ctrl in tlpTemp.Controls)
{
while (ctrl.Controls.Count > 0)
{
ctrl.Controls[0].Dispose();
}
}
}
}
My question is: When I click some Checkbox, how can I get the current checkbox control's index from DataGridView
Here is my snick code
dataGridView2.RowCount = 5;
dataGridView2.ColumnCount = 4;
for (int i = 0; i < dataGridView2.ColumnCount; i++)
{
for (int j = 0; j < dataGridView2.RowCount; j++)
{
box = new CheckBox();
box.Text = "MyDate";
//box.Size = new System.Drawing.Size(15, 15);
dataGridView2.Controls.Add(box);
Rectangle rec = dataGridView2.GetCellDisplayRectangle(i, j, true);
box.Left = rec.Left;
box.Top = rec.Top;
}
}
}
It looks like that you try adding pure CheckBoxes to your DataGridView without using a DataGridViewCheckBoxColumn, the solution for this approach is simple like this:
for (int i = 0; i < dataGridView2.ColumnCount; i++)
{
for (int j = 0; j < dataGridView2.RowCount; j++)
{
box = new CheckBox();
box.Text = "MyDate";
//box.Size = new System.Drawing.Size(15, 15);
dataGridView2.Controls.Add(box);
Rectangle rec = dataGridView2.GetCellDisplayRectangle(i, j, true);
box.Left = rec.Left;
box.Top = rec.Top;
//Added code
box.Tag = new Point(i,j);
box.Click += CheckBoxesClicked;
}
}
private void CheckBoxesClicked(object sender, EventArgs e){
CheckBox chb = sender as CheckBox;
if(chb.Tag != null) {
Point coord = (Point)chb.Tag;
MessageBox.Show(string.Format("Row index: {0}\nColumn index: {1}", coord.Y, coord.X);
}
}
You should use a DataGridViewCheckBoxColumn instead, with that approach, you can handle the event CellContentClick...
if you are using the CellContentClick event or any other event that you get the DataGridViewCellEventArgs then you have ColumnIndex and RowIndex properties that are the column and row of the cell changed
Check this links. This gives you details about grid view.
http://msdn.microsoft.com/en-us/library/ms972814.aspx
http://msdn.microsoft.com/en-us/library/aa479344.aspx
I have manually added values in dropdown list using for loop.
for (int i = 1; i <= 31; i++)
{
date0.Items.Add(i.ToString());
date1.Items.Add(i.ToString());
date2.Items.Add(i.ToString());
date3.Items.Add(i.ToString());
}
for (int j = 1; j <= 12; j++)
{
month0.Items.Add(j.ToString());
month1.Items.Add(j.ToString());
month2.Items.Add(j.ToString());
month3.Items.Add(j.ToString());
}
for (int k = DateTime.Now.Year; k <= 2020; k++)
{
yyyy0.Items.Add(k.ToString());
yyyy1.Items.Add(k.ToString());
yyyy2.Items.Add(k.ToString());
yyyy3.Items.Add(k.ToString());
}
Now on clear button I want to clear out these values and set them to initial. I tried calling the function in which I have initialized them. But it's not happening. Can someone tell me why?
You will need to fill the dropdownlist only if the page is Not postback
if (! IsPostBack) {
//Fill out the dropdown list
}
and make sure to set SelectedIndex = 0 for those you want to set back to initial values
month0.SelectedIndex = 0;
You can reset it to the first record by setting the SelectedIndex to 0 so that the first element in the list will be selected:
date0.SelectedIndex = 0;
date1.SelectedIndex = 0;
date2.SelectedIndex = 0;
date3.SelectedIndex = 0;
month0.SelectedIndex = 0;
month1.SelectedIndex = 0;
month2.SelectedIndex = 0;
month3.SelectedIndex = 0;
yyyy0.SelectedIndex = 0;
yyyy1.SelectedIndex = 0;
yyyy2.SelectedIndex = 0;
yyyy3.SelectedIndex = 0;
You can put this on the Clear button.
I want to create dynamically 10 Labels inside a for loop
string labelName;
for(int i = 0; i < 10; i++)
{
labeName = "Label" & i;
// Creata & Instanciate the label here, How ?
}
How would you create a bunch of objects which weren't UI elements? Use a collection:
List<Label> labels = new List<Label>();
for (int i = 0; i < 10; i++)
{
Label label = new Label();
// Set properties here
labels.Add(label);
}
You'll presumably want to add these labels to a form or page or whatever too...
List<string> labelName = new List<string>();
for(int i = 0; i < 10; i++)
{
labeName.Add(string.Concat("Label", i));
}