Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have addded columns and checkboxes Dynamically... But met with a problem that the CheckedChanged Event is not fired.. here is my Code..
 

// binding datatable Dynamically..
protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
{
 

        for (int i = 0; i < DtOperation.Rows.Count; i++)
        {
            Dt.Columns.Add(DtOperation.Rows[i][0].ToString());
        }
        dgrDynamic.DataSource = Dt;
        dgrDynamic.DataBind();
.
.
        //addind check boxes dynamically.

        tcCheckCell = new TableCell();
        var checkBox = new CheckBox();
 
       // ActivityID|OperationID is stored to work it at the time of Save in CheckedChanged.

       checkBox.ID = DtOperation.Rows[i][0] + "|" + DtOperation.Rows[i][2];
       checkBox.AutoPostBack = true;
       checkBox.Checked = true;
       checkBox.CheckedChanged +=new EventHandler(checkBox_CheckedChanged);
 
        tcCheckCell.Controls.Add(checkBox);
                                        dgrDynamic.Rows[j].Cells.RemoveAt(GetColumnIndexByName((int)DtOperation.Rows[i][2]));
                    dgrDynamic.Rows[j].Cells.AddAt(GetColumnIndexByName((int)DtOperation.Rows[i][2]), tcCheckCell);
                    //(int)DtOperation.Rows[i][2]+2
}
 

void checkBox_CheckedChanged(object sender, EventArgs e)
{
 
...Code;
}
 

am a fresher in this field and can you pls help me to Over come the misery.. Thank you all
Posted 7-Mar-12 1:52am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Because the controls are created dynamically they must be recreated during any postback before the events will fire. Check to make sure this is happening.
 
You could add the controls during an event, such as RowCreated, then use JavaScript to show/hide when the row is selected.
  Permalink  
v2
Comments
anish.karunakaran at 7-Mar-12 23:18pm
   
thank u Senior..it is mandatory to make Changes in SelectedIndex changed Event of Dropdownlist... Can i use PreRender event.. I dont know much About it..
 
i l be grateful to you for your valuable time..
Mark Nischalke at 8-Mar-12 8:02am
   
Try to walk through the process of how ASP.NET creates controls and fires events and you should understand why this is not recommended or possible.
 
The SelectedIndexChanged event is a postback event, meaning that it is not fired until an action happens. In this event you are creating dynamic controls. Now you click on the checkbox which generates another postback, however, since the control was created dynamically it is not in viewstate and the ASP.NET engine has no idea how to recreated it and attached the event to be handled. The only place you are creating this control is in a separate postback event handler whose event was not the trigger for the postback so is not fired and thus not recreating the control to have the source of the postback handle.
 
Do you understand?
anish.karunakaran at 9-Mar-12 0:45am
   
yes Senior I got it.. and i tried with row create event As u said.. but the Problem Persists.. I added Controls in rowcreated Event ...ie dgrDynamic.Rows[j].Cells[Ind].Controls.Add(chkBox); in RowCreated Event.. In the SelectedIndexChanged Event Of the DropDownList I binded the Data Table with Gridview and that atime RowCreated Event Get Fired. and the problem is that the Rowcreated event Fires again when check box is clicked and it gets cleared then.. I dont Know How to recreate the Added Controls to the gridView So that It never Gets cleared and Fires the Event Checked Changed.. Or i do have To Change My Entire Coding Scenario... Pls Do a favor For me.. I am Grateful to you ..
anish.karunakaran at 9-Mar-12 0:43am
   
yes Senior I got it.. and i tried with row create event As u said.. but the Problem Persists.. I added Controls in rowcreated Event ...ie
 
dgrDynamic.Rows[j].Cells[Ind].Controls.Add(chkBox);
 
in RowCreated Event.. In the SelectedIndexChanged Event Of the DropDownList I binded the Data Table with Gridview and that atime RowCreated Event Get Fired.
and the problem is that the Rowcreated event Fires again when check box is clicked and it gets cleared then..
 
I dont Know How to recreate the Added Controls to the gridView So that It never Gets cleared and Fires the Event Checked Changed.. Or i do have To Change My Entire Coding Scenario...
 
Pls Do a favor For me.. I am Grateful to you ..
Mark Nischalke at 9-Mar-12 7:48am
   
You apparently did not understand. Dynamically created controls, and any data bound to them, must be recreated during postback. If the checkbox is needed on each row then add it in the grid template. Try reading this http://www.codeproject.com/Articles/2580/DataGridDemo
anish.karunakaran at 10-Mar-12 4:54am
   
But Senior U got Me na.. Thank you for Staying With me.. Bt now I got Some Sense on what you are Saying.. link Follows how to create Static grid na?..
 

i jst done is tht Created Check boxes at Run time with events then Added those check boxes to list..then tried to recreate it in the PreRender Event of the Gridview.. Bt the event get invoked every time when i click the check box.. and check boxes are not cleared.bt Still the Checked Changed Event is not Firing..
 
Can you pls tell me How can i handle the Scenario.. Which event Should be fired to recreate and Why checked changed Event is not firing.
anish.karunakaran at 12-Mar-12 0:04am
   
But Senior U got Me na.. Thank you for Staying With me.. Bt now I got Some Sense on what you are Saying.. link Follows how to create Static grid na?.. i jst done is tht Created Check boxes at Run time with events then Added those check boxes to list..then tried to recreate it in the PreRender Event of the Gridview.. Bt the event get invoked every time when i click the check box.. and check boxes are not cleared.bt Still the Checked Changed Event is not Firing.. Can you pls tell me How can i handle the Scenario.. Which event Should be fired to recreate and Why checked changed Event is not firing.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Haii..
 
I have solved the Issue By recreating the Dynamic Controls In Page Load Event.. Now Checkboxes are Not Refreshed in Postback.. thank u All...
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 23 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100