I would like to know why the event is not firing & how to find which checkbox control fired the event.
chkList1 = new CheckBox();
chkList1.Text = row["subj_nme"].ToString();
chkList1.ID = row["subjid"].ToString();
chkList1.Checked = true;
chkList1.Font.Name = "Verdana";
chkList1.Font.Size = 12;
chkList1.AutoPostBack = true;
chkList1.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
Panel1.Controls.Add(chkList1);
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
Label1.Text = "Called";
}
If the events aren't firing, it's likely for one of two reasons:
The controls are recreated too late in the page lifecycle. Try creating the controls during OnInit.
Validation is preventing the postback. To work around this you can set CausesValidation to false on all of the CheckBox controls.
You can find out which control triggered the event using the sender argument.
protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
//write the client id of the control that triggered the event
Response.Write(((CheckBox)sender).ClientID);
}
Related
I have a simple WPF application that uses a "frame" for multi-page navigation. One of that pages creates a series of CheckBoxes and adds a couple of handlers (Checked/Unchecked) for each checkbox created. The CheckBoxes work as intended and are programmatically accessible, they can be checked or unchecked by click but none of the two events is ever fired if I click.
Here is the creation of the CheckBoxes:
ModuleStackpanels[i].Children.Add(ModuleCheckBoxes[i]);
StackPanel.SetZIndex(ModuleCheckBoxes[i], 2);
ModuleCheckBoxes[i].Checked += new RoutedEventHandler(ModuleCheckBoxClick);
ModuleCheckBoxes[i].Unchecked += new RoutedEventHandler(ModuleCheckBoxClick);
Where I go from 0 to 30. Then I have the handler:
private void ModuleCheckBoxClick(object sender, RoutedEventArgs e)
{
int CheckBoxCounter = 0;
for(int i=0;i<30;i++)
{
if (ModuleCheckBoxes[i].IsChecked == true) CheckBoxCounter++;
}
if(CheckBoxCounter > 1)
{
Button_QueryStatus.IsEnabled = false;
}
}
But nothing is fired.
Someone has got an idea?
Try to use CheckedChanged instead of Checked and Unchecked.
For example:
public bool checkedthecheckbox { get; set; }
CheckBox testchbox = new CheckBox();
private void Form1_Load(object sender, EventArgs e)
{
testchbox.CheckedChanged += new EventHandler(testchbox_CheckedChanged);
}
void testchbox_CheckedChanged(object sender, EventArgs e)
{
if (testchbox.Checked)
checkedthecheckbox = true;
else
checkedthecheckbox = false;
}
In your case:
ModuleStackpanels[i].Children.Add(ModuleCheckBoxes[i]);
StackPanel.SetZIndex(ModuleCheckBoxes[i], 2);
ModuleCheckBoxes[i].CheckedChanged += new EventHandler(ModuleCheckBoxClick);
private void ModuleCheckBoxClick(object sender, RoutedEventArgs e)
{
int CheckBoxCounter = 0;
for(int i=0;i<30;i++)
{
if (ModuleCheckBoxes[i].IsChecked == true) CheckBoxCounter++;
}
if(CheckBoxCounter > 1)
{
Button_QueryStatus.IsEnabled = false;
}
}
Good news! Not all the checkboxes had the event handler because of a fault in the creation of the buttons. 3 on 30 had it, and these buttons represented a different kind of item.
I just dealt with a similar issue on a WPF app that I did not write the front end for and here it came down to: Checked and similar event handlers will fire if the checkbox is triggered manually or programmatically. Other types are not guaranteed in the same way if you are changing the checkbox by setting to IsChecked.
ex. in my case, they attempted to use a Clicked event handler which would only fire from actual user interaction and not programmatic change, which makes perfect sense because setting the IsChecked status is truly not a click event (even though both can potentially check or uncheck the checkbox)
Currently, I am doing a project for students' hostel and now I have to implement some search strategies about students.Here I have to create a button dynamically when the user clicks on the another server button in .aspx page and accordingly I have to create the onclick event handler for the newly created button. The code-snippet that I used is:
protected void btnsearchByName_Click(object sender, EventArgs e)
{
TextBox tbsearchByName = new TextBox();
Button btnsearchName = new Button();
tbsearchByName.Width = 250;
tbsearchByName.ID = "tbsearchByName";
tbsearchByName.Text = "Enter the full name of a student";
btnsearchName.ID = "btnsearchName";
btnsearchName.Text = "Search";
btnsearchName.Click += new EventHandler(this.btnsearchName_Click);
pnlsearchStudents.Controls.Add(tbsearchByName);
pnlsearchStudents.Controls.Add(btnsearchName);
}
protected void btnsearchName_Click(object sender, EventArgs e)
{
lblsearch.Text = "btnsearchName_Click event fired in " + DateTime.Now.ToString();
}
Here, the problem is newly created eventHandler doesnot get fired. I have gone through this site and looked several questions and answers and also gone through the page life-cycle and they all say that the dynamic button should be on Init or Pre_init, but my problem is I have to create it when another button is clicked, how can it be possible?
You need to add the click handler for the button on every postback.
you could look for the button in the search students panel on page load or try the page OnInit() method to add the handler when its created.
Also check here:
Dynamically added ASP.NET button click handler being ignored
and here:
asp.net dynamically button with event handler
and here:
asp:Button Click event not being fired
(all of which give similar suggestions)
Try this http://msdn.microsoft.com/ru-ru/library/system.web.ui.webcontrols.button.command(v=vs.90).aspx
btnsearchName.Command += new CommandEventHandler(this.btnsearchName_Click);
btnsearchName.CommandName = "Click";
You need to recreate the button and attach the event handler every time. For this, create a list of button and save it on session. On page load, go through the List and create the button every time
public Button create_button()
{
btnsearchName.ID = "btnsearchName";
btnsearchName.Text = "Search";
btnsearchName.Click += new EventHandler(this.btnsearchName_Click);
return btnsearchName;
}
public TextBox create_textbox()
{
TextBox tbsearchByName = new TextBox();
Button btnsearchName = new Button();
tbsearchByName.Width = 250;
tbsearchByName.ID = "tbsearchByName";
tbsearchByName.Text = "Enter the full name of a student";
return tbsearchByName;
}
protected void btnsearchByName_Click(object sender, EventArgs e)
{
TextBox tbsearchByName = create_textbox();
Button btnsearchName = create_button();
//add to panels
pnlsearchStudents.Controls.Add(tbsearchByName);
pnlsearchStudents.Controls.Add(btnsearchName);
//add to session
List<Button> lstbutton = Session["btn"] as List<Button>
lstbutton.add(btnsearchName);
//similarly add textbox
//again add to session
Session["btn"] = lstbutton
}
public override page_load(object sender, eventargs e)
{
//fetch from session, the lstButton and TextBox and recreate them
List<Button> lstbutton = Session["btn"] as List<Button>;
foreach(Button b in lstbutton)
pnlsearchStudents.Controls.Add(b);
//similar for textbox
}
I am not sure but may be you have to override the OnInit() method like this.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
You just need to add this code on ready state of jquery code and it will work fine for the dynamic button too
$(document).ready(function(){
$('input#tbsearchByName').click(function(){
// code goes here
});
});
This question already has an answer here:
Event won't fire to dynamically added control
(1 answer)
Closed 2 years ago.
I saw lots of thing about this topic but i can't fin a solution.
I add many dropdown list with one event but they are not firing SelectedIndexChanged evet.
Here is drplist creator code:
foreach (var row in cmdSelectCats.ExecuteReader())
{
var id = row["ProductCategoryID"].ToString();
var dropDownStatus = new DropDownList {ID = "DrpStatus-" + id};
dropDownStatus.Items.Add(new ListItem("Aktif", "1"));
dropDownStatus.Items.Add(new ListItem("Pasif", "2"));
dropDownStatus.AutoPostBack = true;
dropDownStatus.SelectedIndexChanged += Status_SelectedIndexChanged;
var tableCell = new TableCell();
tableCell.Controls.Add(dropDownStatus);
dropDownStatus.SelectedValue = row["ProductCategoryStatusID"].ToString();
tableRow.Cells.Add(tableCell);
TblCatList.Rows.Add(tableRow);
}
And ofcourse my Event:
public void Status_SelectedIndexChanged(object sender, EventArgs e)
{
//DO SOMETHING
}
What am i missing?
This is a common issue and it's related to the page life cycle:
Take a look at the following questions:
Click events on Array of buttons
Button array disappears after click event
Dynamically create an ImageButton
Now the basic steps to remember when creating dynamic controls are:
Dynamic controls should be created in the PreInit event when you are not working with a master page, if you are, then create the controls in the Init event
Avoid setting properties that can be changed in each post in these events because when the view state is applied (in a post event) the properties will be overridden
Dynamic controls must be created every time the page is posted, avoid this if(!this.IsPostBack) this.CreatemyDynamicControls();
When you create the controls in the PreInit or Init events, their states will be automatically set in a post event, which means in the LoadComplete event your controls will contain their state back even when you create them again in each post and even when you did not explicitly set their state. Note this behavior is different when you are dealing with controls created at design time, in that case, the event where the state has been set is the Load event
Event subscription should occur before the PageLoadComplete or they will not be raised
Consider the following description from MSDN
If controls are created dynamically at run time or declaratively within templates of data-bound controls, their events are initially not synchronized with those of other controls on the page. For example, for a control that is added at run time, the Init and Load events might occur much later in the page life cycle than the same events for controls created declaratively. Therefore, from the time that they are instantiated, dynamically added controls and controls in templates raise their events one after the other until they have caught up to the event during which it was added to the Controls collection.
The above is not so clear to me, but I have found the following. The following TextBox's are created at design time
protected void Page_PreInit(object sender, EventArgs e)
{
this.txtDesignTextBox1.Text = "From PreInit";
this.txtDesignTextBox1.Text += DateTime.Now.ToString();
}
protected void Page_Init(object sender, EventArgs e)
{
this.txtDesignTextBox2.Text = "From Init";
this.txtDesignTextBox2.Text += DateTime.Now.ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
this.txtDesignTextBox3.Text = "From Load";
this.txtDesignTextBox3.Text += DateTime.Now.ToString();
}
At first sight you might think that in every post all the textboxes are updated with the current date, but this is not the case, since they were created at design time they follow strictly the ASP.Net page life-cycle which means, their state is overriden after the PreInit and Init events, only the txtDesignTextBox3 is updated in every post because its Text property is updated after the view state has been set (in the Load event).
But with dynamic controls the behavior is different, remember the MSDN description:
for a control that is added at run time, the Init and Load events might occur much later in the page life cycle
Consider the following:
protected void Page_PreInit(object sender, EventArgs e)
{
var textBox = new TextBox { Text = "From PreInit", Width = new Unit("100%") };
textBox.Text += DateTime.Now.ToString();
this.myPlaceHolder.Controls.Add(textBox);
}
protected void Page_Init(object sender, EventArgs e)
{
var textBox = new TextBox { Text = "From Init", Width = new Unit("100%") };
textBox.Text += DateTime.Now.ToString();
this.myPlaceHolder.Controls.Add(textBox);
}
protected void Page_Load(object sender, EventArgs e)
{
var textBox = new TextBox { Text = "From Load", Width = new Unit("100%") };
textBox.Text += DateTime.Now.ToString();
this.myPlaceHolder.Controls.Add(textBox);
}
In this case, the controls behave slightly different, in this case, in each post, the controls are never updated not even the controls created in the Load event
The reason is their life-cycle events occurs much later in the page-life cycle which means their state is overridden even after the Load event
To solve this, you can use the LoadComplete event, in this event you can change the state of dynamic controls:
protected void Page_LoadComplete(object sender, EventArgs e)
{
var textBox = new TextBox { Text = "From LoadComplete", Width = new Unit("100%") };
textBox.Text += DateTime.Now.ToString();
this.myPlaceHolder.Controls.Add(textBox);
}
In this case, the state will be updated in each post.
However, take in consideration that you should subscribe to dynamic controls events, before the LoadComplete event or they will not be raised.
... I know I hate this kind of behavior, that's why I love MVC
As a quick reference for controls created at design time: Notice how the LoadViewState method is called after the PreInit and Init events but before the Load event. The Load event is considered stable because in this event you can access the view state of your controls. Also notice that the RaisePostBackEvent method represent the control event that caused the post back, this can be, the SelectedIndexChanged, Click, etc this event is handled after the Load event
For a complete detailed specification read the MSDN Page Life-Cycle documentation
I have usually seen this caused by a page lifecycle problem. If the control is only created when an event is fired, then when your index changed event fires the control doesn't exist to bind it to on the postback.
Example:
MyEvent fires. Drop-down created. Event Handler specified.
Index Changed event triggered. Page reloads. Drop-down not found, cannot fire.
You have to ensure the drop-down is created before .NET attemps to handle the event.
You are missing:
1- Override SaveViewState
2- Override LoadViewState
I provide a sample code for this question. I test it. It's work.
ASPX:
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<div id="myDiv" runat="server">
</div>
<asp:Label ID="lblDescription" runat="server"></asp:Label>
</form>
Code Behind:
public partial class Default : System.Web.UI.Page
{
private List<string> values = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
}
protected override object SaveViewState()
{
object baseState = base.SaveViewState();
object[] allStates = new object[2];
allStates[0] = baseState;
allStates[1] = values;
return allStates;
}
protected override void LoadViewState(object savedState)
{
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewState(myState[0]);
if (myState[1] != null)
{
values = (List<string>)myState[1];
MyRender();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
DropDownList ddl = new DropDownList();
ddl.ID = "ClientID" + i;
ddl.Items.Add("Item 1");
ddl.Items.Add("Item 2");
ddl.AutoPostBack = true;
values.Add(ddl.SelectedValue);
myDiv.Controls.Add(ddl);
}
}
private void MyRender()
{
for (int i = 0; i < values.Count; i++)
{
DropDownList ddl = new DropDownList();
ddl.ID = "ClientID" + i;
ddl.Items.Add("Item 1");
ddl.Items.Add("Item 2");
ddl.AutoPostBack = true;
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
myDiv.Controls.Add(ddl);
}
}
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
lblDescription.Text = ((DropDownList)sender).ID + ": Selected Index Changed";
}
}
I have an issue with event as Page_Init, Page_Load, there are wired up 2 times when I click on a Gridview inside my page.
I have checked about AutoEventWireup, but aspx and ascx are correct (false and OnInit overrided).
Event from Gridview are set in Page_Init, but same behavior if it is in Page_Load. I use same event for 2 Gridviews.
Some ideas to help me ?
Here a sample of my code
protected override void OnInit(EventArgs e)
{
instance = (IServiceActivity)InterfaceRepository.GetService(typeof(ServiceActivity));
this.Load += new EventHandler(Page_Load);
this.CollectionGridView1.RowDeleting += new GridViewDeleteEventHandler(CollectionGridView1_RowDeleting);
this.CollectionGridView1.RowDataBound += new GridViewRowEventHandler(CollectionGridView1_RowDataBound);
this.CollectionGridView1.RowEditing += new GridViewEditEventHandler(CollectionGridView1_RowEditing);
this.CollectionGridView1.Sorting += new GridViewSortEventHandler(CollectionGridView1_Sorting);
this.CollectionGridView1.PageIndexChanging += new GridViewPageEventHandler(CollectionGridView1_PageIndexChanging);
this.CollectionGridView2.RowDeleting += new GridViewDeleteEventHandler(CollectionGridView1_RowDeleting);
this.CollectionGridView2.RowDataBound += new GridViewRowEventHandler(CollectionGridView1_RowDataBound);
this.CollectionGridView2.RowEditing += new GridViewEditEventHandler(CollectionGridView1_RowEditing);
this.CollectionGridView2.Sorting += new GridViewSortEventHandler(CollectionGridView1_Sorting);
this.CollectionGridView2.PageIndexChanging += new GridViewPageEventHandler(CollectionGridView1_PageIndexChanging);
base.OnInit(e);
}
protected void CollectionGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
CollectionGridView cgv = (CollectionGridView)sender;
cgv.SelectedIndex = e.RowIndex;
CvrActivity cvrAct = new CvrActivity();
cvrAct = instance.GetActivityById(long.Parse(cgv.SelectedDataKey.Value.ToString()));
if (cvrAct != null)
{
//DELETE
}
}
protected void CollectionGridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
CollectionGridView cgv = (CollectionGridView)sender;
cgv.SelectedIndex = e.NewEditIndex;
base.Modify(long.Parse(cgv.SelectedDataKey.Value.ToString()));
}
Finally, issues is on CommandField, when it is in ButtonType="Image", Edit or Delete is fired 2 times.
Then how use ButtonType with Image ?
Comment out the ones being wired up and see what happens? :-)
When there is a chance that the event has already been wired up you can remove it before trying to add it. Attempting to remove an event handler where there are none will not cause an exception.
// remove existing handler if present
this.CollectionGridView1.RowDeleting -= new GridViewDeleteEventHandle(CollectionGridView1_RowDeleting);
// add new handler
this.CollectionGridView1.RowDeleting += new GridViewDeleteEventHandle(CollectionGridView1_RowDeleting);
Following the pattern of remove then add will ensure that the event is only handled once.
Is your grid inside an ajax update panel?
I once had a similar issue which was fixed by removing the updatepanel.
I have a datagrid with dynamically generated checkbox column..I am not able to
generate the checkedChanged event for the checkbox..
Here is my code:
public class ItemTemplate : ITemplate
{
//Instantiates the checkbox
void ITemplate.InstantiateIn(Control container)
{
CheckBox box = new CheckBox();
box.CheckedChanged += new EventHandler(this.OnCheckChanged);
box.AutoPostBack = true;
box.EnableViewState = true;
box.Text = text;
box.ID = id;
container.Controls.Add(box);
}
public event EventHandler CheckedChanged;
private void OnCheckChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(sender, e);
}
}
}
and Here is the event
private void OnCheckChanged(object sender, EventArgs e)
{
}
Thanks In advance
Typically we've used the "CommandName" property on the control. This will pass through to the RowCommand event of the GridView. You can then inspect the value of CommandName and act accordingly.
since you add the control dynamically, you also need to add it to the viewState (see overrides for LoadViewState and SaveViewState).
when you do postback, the page has no information about the checkbox you've added and that's why you don't get any event.
please check this article: http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx
it describes those issues very well.