Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

c# - Add checkbox with CheckedChanged Event to a Dynamic GridView

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...