Click here to Skip to main content
15,885,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have addded columns and checkboxes Dynamically... But met with a problem that the CheckedChanged Event is not fired.. here is my Code..


C#
// 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

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.
 
Share this answer
 
v2
Comments
anish.karunakaran 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..
[no name] 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 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 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 ..
[no name] 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
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...
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900