asp.net c# multiple PlaceHolders on page with multiple dynamic buttons - c#

Im creating dynamic buttons and adding unique ones to 2 different placeholders. The click event is added correctly to each button but the buttons click event is not firing on the 2nd placeholder. If i change the buttons to be just add to the 1 placeholder they all fire correctly. I can alter things around to get round this but im curious as to why the 2nd placeholder is an issue. Code below:
code behind:
//Create Colour Button
RadButton buttonColour = new RadButton();
buttonColour.EnableEmbeddedBaseStylesheet = false;
buttonColour.EnableEmbeddedSkins = false;
buttonColour.CssClass = "VariationButtonsColour";
buttonColour.BackColor = System.Drawing.Color.White;
buttonColour.Height = new Unit(45);
buttonColour.Click += new EventHandler(ColourClick);
buttonColour.Text = Colour;
buttonColour.ButtonType = RadButtonType.SkinnedButton;
buttonColour.ID = dr["Colour"].ToString() ;
buttonColour.PressedCssClass = "VariationButtonsColourActive";
foreach (Control ctrl in phColourButtons.Controls)
{
if (ctrl is RadButton)
{
RadButton button = (RadButton)ctrl;
if (buttonColour.ID == button.ID)
{
ColourExists = ColourExists + 1;
}
}
}
if (ColourExists == 0)
{
phColourButtons.Controls.Add(buttonColour);
Label spacer = new Label();
spacer.Width = new Unit(15);
spacer.Text = " ";
phColourButtons.Controls.Add(spacer);
}
//Create Size Button
RadButton buttonSize = new RadButton();
buttonSize.EnableEmbeddedBaseStylesheet = false;
buttonSize.EnableEmbeddedSkins = false;
buttonSize.CssClass = "VariationButtonsSize";
buttonSize.BackColor = System.Drawing.Color.White;
buttonSize.Height = new Unit(45);
buttonSize.Click += new EventHandler(SizeClick);
buttonSize.Text = Size;
buttonSize.ButtonType = RadButtonType.SkinnedButton;
buttonSize.ID = dr["Size"].ToString();
buttonSize.PressedCssClass = "VariationButtonsSizeActive";
foreach (Control ctrl in phSizeButtons.Controls)
{
if (ctrl is RadButton)
{
RadButton button = (RadButton)ctrl;
if (buttonSize.ID == button.ID)
{
SizeExists = SizeExists + 1;
}
}
}
if (SizeExists == 0)
{
phSizeButtons.Controls.Add(buttonSize);
Label spacer = new Label();
spacer.Width = new Unit(15);
spacer.Text = " ";
phSizeButtons.Controls.Add(spacer);
}
If someone could explain why the 2nd placeholder is not working that would be great.

Related

Check which Radio Button is checked in C#, with dynamically created radiobuttons

I can't find any solution or hint on this problem. Problem described after this code.
I must create one picturebox and radiobutton for every folder found on a specific path:
{
InitializeComponent();
string pathtocircuits = "../../tracks";
string[] allfiles = Directory.GetDirectories(pathtocircuits, "*.*", SearchOption.TopDirectoryOnly);
int imgx = 387;
int imgy = 153;
int radx = 428;
int rady = 259;
String track = "";
String pici = "";
String pic = "pictureBox";
String rad = "radiobutton";
String radr = "";
String picr = "";
foreach (String file in allfiles)
{
track = Path.GetFileName(file);
pici = "../../tracks/" + track + "/p_" + track + ".png";
picr = pic + element.ToString();
radr = rad + element.ToString();
PictureBox pb = new PictureBox();
pb.Location = new System.Drawing.Point(imgx, imgy); ;
pb.Image = Image.FromFile(pici);
pb.Width = 100;
pb.Height = 100;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Name = picr;
Controls.Add(pb);
RadioButton rdo = new RadioButton();
rdo.Name = radr;
rdo.Text = "";
rdo.Tag = track;
rdo.Location = new Point(radx, rady);
this.Controls.Add(rdo);
element += 1;
imgx += 110;
radx += 110;
}
}
With this part I get to create the elements I need (it works).
My problem is when I press a button to reach Form2. How can I check which radiobutton is selected and store its Tag value in a String?
for(int i = 0; i<element; i++)
{
if( ??? .Checked == true )
{
globalstring = ??? .Tag;
}
}
If I try to use the name of a created radiobutton instead of a ??? it gives me an error like 'element ??? does not have a Checked or Tag attribute'
Add method below
Add to For loop : rdo.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton button = sender as RadioButton;
string name = button.Text;
}
RadioButtons as any other control are stored in the Controls collection of their container. If you add them directly to the form you can retrieve them using this code.
protected void Button1_Click(object sender, EventArgs e)
{
var radio = this.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
if(radio != null)
{
string tag = radio.Tag.ToString();
.....
// Form2 = new Form2(tag);
}
}
The radiobuttons are added to a same group by default, so you can get the checked radiobutton as followed.
List<RadioButton> radioButtons = this.Controls.OfType<RadioButton>().ToList();
RadioButton rb = radioButtons
.Where(r => r.Checked)
.Single();
string tag = rb.Tag.ToString();

Check all items in a specified checked list box c#

I have created a small kitchen display program that display food orders. So I created dynamically a panel that contains a table layout panel that contains a checked list box and a check all button . My problem is... I have a check all button in each table layout panel created dynamically and every time I click it, it checks all items in the last created CheckedListBox not the clicked one.
This is my code:
p = new Panel();
p.Size = new System.Drawing.Size(360, 500);
p.BorderStyle = BorderStyle.FixedSingle;
p.Name = "panel";
tpanel = new TableLayoutPanel();
tpanel.Name = "tablepanel";
clb = new CheckedListBox();
tpanel.Controls.Add(b1 = new Button() { Text = "CheckAll" }, 1, 4);
b1.Name = "b1";
b1.Click += new EventHandler(CheckAll_Click);
b1.AutoSize = true;
private void CheckAll_Click(object sender, EventArgs e)
{
var buttonClicked = (Button)sender;
var c = GetAll(this, typeof(CheckedListBox));
for (int i = 0; i < c.Count(); i++)
{
\\any help
}
}
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type)).Concat(controls).Where(c =>
c.GetType() == type);
}
First I will describe the struct
Order = TableLayoutPanel
TableLayoutPanel has 1 CheckAll Button and CheckListBox
And you want when you click to CheckAll Button it will checks exactly all items in current TableLayoutPanel.
So try this code
class XForm : Form {
// create Dictionary to store Button and CheckListBox
IDictionary<Button, CheckListBox> map = new Dictionary<Button, CheckListBox> ();
// when you create new order (new TableLayoutPanel)
// just add map Button and CheckListBox to map
private void CreateOrder () {
var panel = new Panel ();
panel.Size = new System.Drawing.Size (360, 500);
panel.BorderStyle = BorderStyle.FixedSingle;
panel.Name = "panel";
var table = new TableLayoutPanel ();
var checklistBox = new CheckedListBox ();
var button = new Button () { Text = "CheckAll" };
table.Controls.Add (button, 1, 4);
button.Name = "b1";
button.Click += new EventHandler (CheckAll_Click);
button.AutoSize = true;
map[button] = checklistBox;
}
// and on event handle
private void CheckAll_Click (object sender, EventArgs e) {
var buttonClicked = (Button) sender;
var c = map[buttonClicked];
if (c == null) return;
for (int i = 0; i < c.Items.Count; i++)
{
c.SetItemChecked(i, true);
}
}
}
And dont for get remove it from map when remove the order.
Hope it helps

Disabled checkboxes in flowLayoutPanel

I'm trying to create some checkboxes dynamically in the flowLayoutPanel control.
I have managed to create it but they seem to be disabled inside the panel. I can't check/uncheck them. They are grey and not active.
I suppose there is some flowLayoutPanel property that prevents these checkboxes to be enabled that I'm missing.
Here is my code for flowLayoutPanel:
//
// flowLayoutPanelForCheckBoxes
//
this.flowLayoutPanelForCheckBoxes.Controls.Add(this.SomeCheckBox);
this.flowLayoutPanelForCheckBoxes.AutoScroll = true;
this.flowLayoutPanelForCheckBoxes.Location = new System.Drawing.Point(132, 8);
this.flowLayoutPanelForCheckBoxes.Name = "flowLayoutPanelForCheckBoxes";
this.flowLayoutPanelForCheckBoxes.Size = new System.Drawing.Size(546, 38);
this.flowLayoutPanelForCheckBoxes.TabIndex = 28;
this.flowLayoutPanelForCheckBoxes.WrapContents = false;
For generating checkboxes:
private List<CheckBox> GetGeneratedCheckboxes()
{
var generatedCheckboxes = new List<CheckBox>();
var valuesForCheckboxes = GetCheckboxValuesFromDb(); //it returns dictionary<int, string>
// with some numbers and text. I think this method is not as important
if (valuesForCheckboxes != null && valuesForCheckboxes.Count != 0)
{
int index = 0;
foreach (var chbx in valuesForCheckboxes)
{
var checkboxToAdd = new System.Windows.Forms.CheckBox();
checkboxToAdd.AutoSize = true;
checkboxToAdd.Enabled = true;
checkboxToAdd.Checked = true;
checkboxToAdd.CheckState = System.Windows.Forms.CheckState.Checked;
checkboxToAdd.Size = new System.Drawing.Size(84, 21);
checkboxToAdd.UseVisualStyleBackColor = true;
checkboxToAdd.Name = "chboxCountry" + chbx.Key;
checkboxToAdd.Text = chbx.Value;
checkboxToAdd.TabIndex = index + 1;
index++;
generatedCheckboxes.Add(checkboxToAdd);
}
}
return generatedCheckboxes;
}
And it's used in the FormLoad method:
var checkboxesForPanel = GetGeneratedCheckboxes();
foreach (var checkbox in checkboxesForPanel)
{
this.flowLayoutPanelForCheckBoxes.Controls.Add(checkbox);
}
Code looks fine for me. It has no issue. Check if your flow layout panel is in disabled state some how. Check in designer or check your code if its getting in disabled state.

c# How to change the visible property of a label created at runtime

//Here I create the labels at runtime in one click
Label[] labels = new Label[countresult];
for (int i = 1; i < countresult; i++)
{
labels[i] = new Label();
labels[i].Font = new Font("Arial Rounded MT Bold", 30);
labels[i].ForeColor = System.Drawing.Color.Red;
labels[i].AutoSize = true;
labels[i].Text = "";
//Here I try to assign the value visible = true
labels[i].Visible = true;
labels[i].TabIndex = i;
}
//In a private void of a timer tick I assign the name of label to var "a" and I do the 3 methods
string a = string.Format("labels[{0}]", labelscount);
//1st method
if (this.Controls.ContainsKey(a))
{
this.Controls[a].Visible=false;
}
//2nd method
foreach (Control control in Controls)
{
if (control.Name == a)
{
control.Visible = false;
}
}
//3rd method
if (this.Controls[a] is Label) this.Controls[a].Visible=false;
labelscount++;
Unfortunately none works.
Someone know What's happened?
You are not adding the labels to the owning control. So they will never be displayed. So in your loop you need to add the following as the last line...
this.Controls.Add(labels[i]);

GUI output differs from local vs production

I am having a weird experience. I am dynamically creating a row of textboxes at runtime when the user clicks a button.
However, on my local machine the text boxes appear correctly (example
[TextBox1] [TextBox2] [TextBox3] [TextBox4] [TextBox5]
[TextBox1] [TextBox2] [TextBox3] [TextBox4] [TextBox5]
[TextBox1] [TextBox2] [TextBox3] [TextBox4] [TextBox5]
When I run this app on the production, the output side-by-side is:
[TextBox1][TextBox1] [TextBox2][TextBox2] [TextBox3][TextBox3] [TextBox4][TextBox4]
The output should be one row of textboxes, then a second row of 5 text boxes etc.
The code that builds the textboxes is:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
dyntxtCar = new TextBox[myCount];
dyntxtMake = new TextBox[myCount];
dyntxtMileage = new TextBox[myCount];
dyntxtVIN = new TextBox[myCount];
dyntxtSLIC = new TextBox[myCount];
dyntxtPlateNumber = new TextBox[myCount];
for (i = 0; i < myCount; i += 1)
{
TextBox txtCar = new TextBox();
TextBox txtMake = new TextBox();
TextBox txtMileage = new TextBox();
TextBox txtVIN = new TextBox();
TextBox txtSLIC = new TextBox();
TextBox txtPlateNumber = new TextBox();
txtCar.ID = "txtVehCar" + i.ToString();
txtMake.ID = "txtVehMake" + i.ToString();
txtMileage.ID = "txtVehMilage" + i.ToString();
txtVIN.ID = "txtVehVIN" + i.ToString();
txtSLIC.ID = "txtVehSLIC" + i.ToString();
txtPlateNumber.ID = "txtVehPlate" + i.ToString();
//Set tabIndex values for dynamic text fields;
txtCar.TabIndex = System.Convert.ToInt16(i * 10 + 1);
txtMake.TabIndex = System.Convert.ToInt16(i * 10 + 2);
txtMileage.TabIndex = System.Convert.ToInt16(i * 10 + 3);
txtVIN.TabIndex = System.Convert.ToInt16(i * 10 + 4);
txtSLIC.TabIndex = System.Convert.ToInt16(i * 10 + 5);
txtPlateNumber.TabIndex = System.Convert.ToInt16(i * 10 + 6);
//Set maxlength for dynamic fields;
txtCar.MaxLength = System.Convert.ToInt16(7);
txtVIN.MaxLength = System.Convert.ToInt16(17);
txtSLIC.MaxLength = System.Convert.ToInt16(4);
//Set width of text boxes
txtCar.Width = System.Convert.ToInt16("65");
txtMileage.Width = System.Convert.ToInt16("50");
txtVIN.Width = System.Convert.ToInt16("220");
txtSLIC.Width = System.Convert.ToInt16("45");
//txtPlateNumber.Width = System.Convert.ToInt16("35");
phCar.Controls.Add(txtCar);
phMake.Controls.Add(txtMake);
phMileage.Controls.Add(txtMileage);
phVIN.Controls.Add(txtVIN);
phSLIC.Controls.Add(txtSLIC);
phPlateNumber.Controls.Add(txtPlateNumber);
dyntxtCar[i] = txtCar;
dyntxtMake[i] = txtMake;
dyntxtMileage[i] = txtMileage;
dyntxtVIN[i] = txtVIN;
dyntxtSLIC[i] = txtSLIC;
dyntxtPlateNumber[i] = txtPlateNumber;
LiteralControl literalBreak = new LiteralControl("<br />");
phCar.Controls.Add(literalBreak);
phMake.Controls.Add(literalBreak);
phMileage.Controls.Add(literalBreak);
phVIN.Controls.Add(literalBreak);
phSLIC.Controls.Add(literalBreak);
phPlateNumber.Controls.Add(literalBreak);
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
Control myControl = GetPostBackControl(this.Page);
if ((myControl != null))
{
if ((myControl.ClientID.ToString() == "btnAddTextBox"))
{
myCount = myCount + 1;
}
}
}
public static Control GetPostBackControl(Page thePage)
{
Control myControl = null;
string ctrlName = thePage.Request.Params.Get("__EVENTTARGET");
if (((ctrlName != null) & (ctrlName != string.Empty)))
{
myControl = thePage.FindControl(ctrlName);
}
else
{
foreach (string Item in thePage.Request.Form)
{
Control c = thePage.FindControl(Item);
if (((c) is System.Web.UI.WebControls.Button))
{
myControl = c;
}
}
}
return myControl;
}
Anyone experience this?

Categories