Checkbox added programmatically not found - c#

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;

Related

ASPx controls not present in PostBack when created in code behind

I created a LayoutGroup in this way:
protected void Page_LoadComplete(object sender, EventArgs e){
LayoutGroup layoutGroup = new LayoutGroup
{
Caption = "Services",
Name = "Services",
ColumnCount = 4
};
foreach (Service service in services)
{
LayoutItem layoutItem = new LayoutItem
{
Caption = service.Name
};
ASPxCheckBox checkBox = new ASPxCheckBox
{
Value = service.ID,
};
layoutItem.Controls.Add(checkBox);
layoutGroup.Items.Add(layoutItem);
}
MyFormLayout.Items.Add(layoutGroup);
}
I had to write this code in Page_LoadComplete and not in Page_Init because this code should be executed after an event is raised (getting the "services" list after an item in a combobox is selected).
I also have a submit button, and I want to retrieve the CheckBoxs' values from its event, like this :
protected void SubmitButton_Click(object sender, EventArgs e)
{
LayoutGroup layoutGroupServices = MyFormLayout.FindItemOrGroupByName("Services") as LayoutGroup;
foreach (LayoutItem item in layoutGroupServices.Items)
{
var debug = item.Controls;
}
}
but Controls is an empty list. How can I retrieve the checkboxes? Thank you in advance

Access dynamically created checkbox values in c#

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!

How to handle events of dynamically added checkbox on windows form

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
}

Asp.net Update GridView with AutoGenerateEditButton

How do I use the AutoGenerateEditButton to update my gridview Table (has a dataset bound to it -Dataset Retrieved from an sql database)
--Removed the Broken code in the question to put Fixed code in the Answer--
To get the values of the updated row add this to your "RowUpdating" event handler
protected void grdViewDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)grdViewDetails.Rows[e.RowIndex];
foreach (Control item in row.Controls)
{
if (item.Controls[0] is TextBox)
{
TextBox textbox = (TextBox)item.Controls[0];
string x = textbox.Text; //theres your value you can do stuff with
}
if (item.Controls[0] is Label)
{
Label mylabel = (Label)item;
//do stuff - just do the same as the textbox
}
}
}
and in "RowEditing" event handler
protected void grdViewDetails_RowEditing1(object sender, GridViewEditEventArgs e)
{
grdViewDetails.EditIndex = e.NewEditIndex;
//e.newedit index:- will be provide index of row for which edit button is selected
grdViewDetails.DataSource = yourdatasource //mine was a datset
grdViewDetails.DataBind();
}

Dynamically Creating Multiple controls which use a common handler

This first section is in a loop. It creates the dynamic check boxes with no problems.
// All I am doing here is incrementing our session counter
int id = Convert.ToInt32(Session["id"]);
id++;
Session["id"] = id;
// Now I create my checkbox
chkDynamic = new CheckBox();
chkDynamic.Text = "hey";
string chk = "chk" + id.ToString();
chkDynamic.ID = chk;
chkDynamic.CheckedChanged += new EventHandler(this.chkDynamic_CheckedChanged);
Panel1.Controls.Add(chkDynamic);
My event handler is not wiring up for this. Strangly if I hard code the ID it does work, but only for one iteration of the loop because if we hard coded the IDs then we would run into 'multiple id errors'
protected void chkDynamic_CheckedChanged(object sender, EventArgs e)
{
if (chkDynamic.Checked)
Response.Write( "you checked the checkbox");
else if (!chkDynamic.Checked)
Response.Write("checkbox is not checked");
}
You need to check sender in your event handler to know which checkbox sent the event:
protected void chkDynamic_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
Response.Write( "you checked the checkbox");
else
Response.Write("checkbox is not checked");
}

Categories

Resources