I am able to add checkbox dynamically on windows form and add data value to its text property. On click of any checkbox I have run a procedure which will make certain other checkbox disabled.
I am not able to find eventhandler for it.
Have you tried this
CheckBox check = new CheckBox();
check.Checked = true;
check.AccessibleName = checkName;
check.Location = new System.Drawing.Point(340, 40);
check.CheckedChanged +=new EventHandler(check_CheckedChanged);
this.Controls.Add(check);
private void custom_event_handler(object sender, EventArgs e)
{
....
}
and then you add checbox like this:
CheckBox cb = new CheckBox();
cb.CheckedChanged += new EventHandler(custom_event_hahndler);
if the name of the dynamically added checkbox is c, the answer is as below:
c.CheckedChanged += c_CheckedChanged;
and c_CheckedChanged is as below:
private void c_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
((CheckBox)(this.Controls.Find("c1", false))[0]).Enabled = false;
}
}
which c1 is the name of the checkbox you want to disable.
Add event handler when you create a checkbox programmatically. And its handler you can do your code logic.
CheckBox dynamicCheckBox = new CheckBox();
dynamicCheckBox.CheckedChanged +=new EventHandler(dynamicCheckBox_CheckedChanged);
private void dynamicCheckBox_CheckedChanged(object sender, EventArgs e)
{
// Your code
}
Related
I want to get a selected item from a drop down list, on button click need to show that selected item into a text box.
I haven't tried anything cause i don't know what to do.
Just use this code...
Suppose, Button click event:
protected void Button1_Click(object sender, EventArgs e)
{
Textbox1.Text = DropDownList1.SelectedItem.Text.ToString();
}
The following class can give you the general idea of doing what you want.
public class MyClass
{
public MyClass()
{
comboBox = new ComboBox();
button = new Button();
textBox = new TextBox();
button.Click += OnButtonClick;
}
private void OnButtonClick(Object sender, EventArgs e)
{
textBox.Text = comboBox.SelectedItem.ToString();
}
ComboBox comboBox;
Button button;
TextBox textBox;
}
In form_load event I create three check boxes. Then with a button_click event I want to check the state of the check boxes, but they are not available in my if statement. I found this piece of code as an example:
CheckBox chk = new CheckBox();
chk.Top = 50;
chk.Left = 50;
chk.Text = "Check Box Test";
chk.Name = "chkTest";
this.Controls.Add(chk);
CheckBox chkTest = (CheckBox)Controls["chkTest"];
which works only if I check the state inside the form_load. How do I access the check boxes with a button_click?
An option would be to create a List<CheckBox> at the class level to hold a collection of the checkboxes you create.
Example:
List<CheckBox> CheckBoxes = new List<CheckBox>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CreateCheckBoxes();
}
private void CreateCheckBoxes()
{
//Create 3 checkboxes
int intialTop = 50;
for (int i = 0; i < 3; i++)
{
CheckBox chk = new CheckBox();
chk.Top = intialTop;
chk.Left = 50;
chk.Text = "Check Box Test";
chk.Name = "chkTest";
this.Controls.Add(chk);
CheckBoxes.Add(chk);
intialTop += 20;
}
//You can access your checkboxes anywhere in Form1 now.
var first = CheckBoxes.First();
first.Text = "First Checkbox";
}
Additional Example:
This example shows adding two checkboxes to the form as well as giving them their own click event handlers.
CheckBox checkBoxA;
CheckBox checkBoxB;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Add checkbox A
checkBoxA = new CheckBox();
checkBoxA.Top = 10;
checkBoxA.Left = 50;
checkBoxA.Text = "CheckBoxA";
//Register the event handler for this checkbox
checkBoxA.Click += new EventHandler(checkBoxA_Click);
this.Controls.Add(checkBoxA);
//Add checkbox B
checkBoxB = new CheckBox();
checkBoxB.Top = 30;
checkBoxB.Left = 50;
checkBoxB.Text = "checkBoxB";
//Register the event handler for this checkbox
checkBoxB.Click += new EventHandler(checkBoxB_Click);
this.Controls.Add(checkBoxB);
}
void checkBoxA_Click(object sender, EventArgs e)
{
MessageBox.Show("CheckBoxA has been clicked!!!");
}
void checkBoxB_Click(object sender, EventArgs e)
{
MessageBox.Show("CheckBoxB has been clicked!!!");
}
As stated earlier, you can create a class level CheckBox and then check that anywhere you need to in your code:
CheckBox checkBox1 = new CheckBox();
private void Form1_Load()
{
checkBox1.Name = "CheckBox1";
checkBox1.Text = "Click Me!";
checkBox1.Click += new EventHandler(checkBox1_Click); // Only need this if you want a click handler
this.Controls.Add(checkBox1);
}
private void checkBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("You click the check box");
}
private void submitButton_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
MessageBox.Show("Check box is checked!");
}
}
Alternatively, you can iterate over the form's controls and search for the check boxes there:
private void CheckCheckBoxes(string checkBoxName)
{
// this.Controls is a collection of all controls on the form (assuming this is run on the Form class)
foreach(Control control in this.Controls)
{
if (control.Name == checkBoxName && control is CheckBox)
{
CheckBox checkBox = control as CheckBox;
if (checkBox.Checked)
{
MessageBox.Show("Check box is checked");
}
}
}
}
I have added a CheckBox dynamically in asp.net
CheckBox cb = new CheckBox();
cb.Text = "text";
cb.ID = "1";
I can access this CheckBox via c# in pageLoad itself, just after declaring above codes.
But when I try to access this values after a button click I'm getting null values.
CheckBox cb1 = (CheckBox)ph.FindControl("1");
Response.Write(cb1.Text);
ph.Controls.Add(cb);
(ph is a placeholder)
Can any one tell me whats wrong here?
You need to recreate the checkbox everytime the page posts back, in Page_Load event, as it's dynamically added to page.
Then you can access the checkbox later in button click event.
// Hi here is updated sample code...
Source
<body>
<form id="frmDynamicControl" runat="server">
<div>
<asp:Button ID="btnGetCheckBoxValue" Text="Get Checkbox Value" runat="server"
onclick="btnGetCheckBoxValue_Click" />
</div>
</form>
</body>
code behind
protected void Page_Load(object sender, EventArgs e)
{
CheckBox cb = new CheckBox();
cb.Text = "text";
cb.ID = "1";
frmDynamicControl.Controls.Add(cb);
}
protected void btnGetCheckBoxValue_Click(object sender, EventArgs e)
{
CheckBox cb1 = (CheckBox)Page.FindControl("1");
// Use checkbox here...
Response.Write(cb1.Text + ": " + cb1.Checked.ToString());
}
After you click the button it will post back the page which will refresh the state. If you want the values to be persistent then you'll need to have them backed inside the ViewState or similar.
private bool CheckBox1Checked
{
get { return (ViewState["CheckBox1Checked"] as bool) ?? false; }
set { ViewState["CheckBox1Checked"] = value; }
}
void Page_load(object sender, EventArgs e)
{
CheckBox cb = new CheckBox();
cb.Text = "text";
cb.ID = "1";
cb.Checked = CheckBox1Checked;
cb.OnCheckedChanged += CheckBox1OnChecked;
// Add cb to control etc..
}
void CheckBox1OnChecked(object sender, EventArgs e)
{
var cb = (CheckBox)sender;
CheckBox1Checked = cb.Checked;
}
I'm a bit later here, but i just do:
try{
if(Request.Form[checkboxId].ToString()=="on")
{
//do whatever
}
}catch{}
If a checkbox is not checked, it will not appear in the Form request hence the try catch block. Its quick, simple, reusable, robust and most important, it just works!
I add checkboxes that way:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CheckBox FieldCh = new CheckBox();
FieldCh.ID = "Field_" + Field.Id;
Panel1.Controls.Add(FieldCh);
}
}
but when I try to get this checkboxes from form:
foreach (Control item in FindControl("FieldForm").Controls)
{
if (item is Panel)
{
foreach (Control checkbox in item.Controls)
i cannot find this checkboxes :/ This could be problem with runat=server? I not find this property in Checkbox ..
If you want to find this CheckBox after PostBack (what I've assumed), you need to recreate it. Try to create this CheckBox out of if(!PostBack) clause (so it's recreated after postback too):
protected void Page_Load(object sender, EventArgs e)
{
CheckBox FieldCh = new CheckBox();
FieldCh.ID = "Field_" + Field.Id;
Panel1.Controls.Add(FieldCh);
if (!IsPostBack)
{
// ....
}
}
You must have to use Page_Load even to add controls dynamically.
protected void page_load()
{
CheckBox FieldCh = new CheckBox();
FieldCh.ID = "Field_" + Field.Id;
Panel1.Controls.Add(FieldCh);
}
you may simply use a better method of finding your control
just use this
CheckBox chkBox = this.form1.FindControl("YourControlId") as CheckBox;
I have two controls created on Form_Load, a button and a combobox. I also have an event for the button, but the event should be able to see the newly created combobox.
When I try to call the combobox by it's name it says that it doesn't exist in this context.
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
ComboBox kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
kombo.Items.Add("Panel"); //just an example
}
Is there a way to make this work?
Only controls which are in used in markup with runat="server" will be class variables on your page. They are actually defined in the designer file.
What you'll want to do is in the class add something like the following where you have a class variable, then assign kombo in your page-load function. Then, it will exist in your click event handler.
// kombo is now scoped for use throughout this class
ComboBox kombo = null;
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
// Assign to our kombo instance
kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
// Using the kombo we created in form load, which is still referenced
// in the class
kombo.Items.Add("Panel"); //just an example
}
You will have to use the FindControl() method to find the object first.
private void przycisk_Click(object sender, EventArgs e)
{
ComboBox kombo = (ComboBox)FindControl("kombo");
kombo.Items.Add("Panel");
}