2

I want to dynamically add a checkbox to a dynamic GridView along with an Event.

i.e. For the grid I have to add check boxes dynamically checked or unchecked according to the Database. And by clicking the checkbox itself I want to update the database.

For this I need the Event to also be dynamically loaded along with the checkbox.

What I have completed is a static version and is exhibited here:

In database RoleID(Admin,Purchase Officer etc), ActivityID(Leave application etc) and OperationID(Save,Edit Etc) are stored.

First row implies for Admin(roleid 1) Save operation(OperationID 1) is allowed for activity Leave application(Activityid 3).

2 Answers 2

7

I'm sorry, follow this

Place a check box in gridview

this is an example HTML Code to declare a checkbox in gridview

               <asp:TemplateField HeaderText="chkbox">
                   <ItemTemplate>
                       <asp:CheckBox ID="CheckBox1" runat="server"  AutoPostBack="true"
                           oncheckedchanged="CheckBox1_CheckedChanged"  />
                   </ItemTemplate>
               </asp:TemplateField>

Now about the event for the checkbox

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
   GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
    int index = row.RowIndex;
    CheckBox cb1 = (CheckBox)Gridview.Rows[index].FindControl("CheckBox1");
    string checkboxstatus;
    if (cb1.Checked == true)
        checkboxstatus = "YES";
    else if(cb1.Checked == false)
        checkboxstatus = "NO";

    //Here Write the code to connect to your database and update the status by 
    //sending the checkboxstatus as variable and update in the database.
}
Sign up to request clarification or add additional context in comments.

2 Comments

can you please make it little more specific.. ie am adding checkboxes at runtime..is it possible to add an event elong with it.. ie Checkedchanged event .. or such ones like cellMouseClick event of windows..
Senior I managed it.. I just embedded the event Selected IdexChanged in PreRender event... But The dynamically added CheckedChanged Event is not fired and even after clicking the checkbox its state is not changing...
4

if you are adding checkboxes at runtime, when you add checkbox, the checkbox event needs to be defined.

For example :

    TableCell tcCheckCell = new TableCell();
    var checkBox = new CheckBox();
    checkBox.CheckedChanged += checkBox_CheckedChanged;
    tcCheckCell.Controls.Add(checkBox);
    gridView.Rows[0].Cells.AddAt(0, tcCheckCell);

    void checkBox_CheckedChanged(object sender, EventArgs e)
    {
        //do something: You can use Krishna Thota's Code.
    }

7 Comments

Senior.. it Was an Excellent Solution.. Thank U Very much For your Precious Moments..
Hai senior... I met with another problem with the way you suggested.. i jst tried with setting autopostback= true for check box.. bt i ddin't worked. the event is not firing and check boxes get cleared when clicked.. What to do.. Pls Help me.. its very Urgent..
You need to do in OnPreRender. protected override void OnPreRender(EventArgs e){ //your code }
Thank U sir.. I have a Problem That ma Entire code is in the SelectedIndexChanged Event of the Dropdown list.. and i havn't handled the event you Specified.. Sir So can u please tell me how can i handle this Condition.. Pls Help Me.. i wil be Very glad with ur answer.. Thank You..
Senior I managed it.. I just embedded the event Selected IdexChanged in PreRender event... But The dynamically added CheckedChanged Event is not fired and even after clicking the checkbox its state is not changing...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.