[ASP. NET + C #] DataGrid tips 4 (how to control multiple operation buttons in a cell and delete data)

Source: Internet
Author: User
I. Parts of aspx Code ;
<Asp: DataGrid id = "mydatagrid" runat = "server" width = "100%" font-names = "verdana" bordercolor = "black"
Borderwidth = "1px" cellpadding = "3" font-name = "verdana" font-size = "8pt" headerstyle-backcolor = "# aaaadd"
Alternatingitemstyle-backcolor = "# eeeeee" horizontalalign = "center" autogeneratecolumns = "false">
<Alternatingitemstyle backcolor = "# f7fbff"> </alternatingitemstyle>
<Headerstyle font-bold = "true" horizontalalign = "center" backcolor = "#99 CCFF">
<Columns>
<Asp: boundcolumn datafield = "tempsemester" headertext = "term">
<Headerstyle width = "25%" type = "codeph" text = "/codeph">
</ASP: boundcolumn>
<Asp: boundcolumn datafield = "examdate" headertext = "test time">
<Headerstyle width = "25%" type = "codeph" text = "/codeph">
</ASP: boundcolumn>
<Asp: boundcolumn datafield = "examname" headertext = "times">
<Headerstyle width = "35%" type = "codeph" text = "/codeph">
</ASP: boundcolumn>
<Asp: boundcolumn headertext = "operation">
<Itemstyle horizontalalign = "center" width = "15%"> </itemstyle>
</ASP: boundcolumn>
<Asp: hyperlinkcolumn visible = "false" text = "& lt; IMG src = & quot ;.. /.. /images/modi.gif & quot; border = & quot; 0 & quot; Alt = & quot; Modify & quot; & gt ;"
Datanavigateurlfield = "examnameid" datanavigateurlformatstring = "javascript: newwin ('modiexamset. aspx? Examnameid = {0 }')"
Headertext = "edit"> </ASP: hyperlinkcolumn>
<Asp: buttoncolumn visible = "false" text = "& lt; IMG src = & quot ;.. /.. /images/del.gif & quot; border = & quot; 0 & quot; Alt = & quot; Delete & quot; onclick = & quot; return confirm ('Are you sure you want to delete this address book? '); & Quot; & gt ;"
Headertext = "delete" commandname = "delete"> </ASP: buttoncolumn>
<Asp: boundcolumn visible = "false" datafield = "examnameid">
<Itemstyle horizontalalign = "center"> </itemstyle>
</ASP: boundcolumn>
<Asp: boundcolumn visible = "false" datafield = "examtype">
<Itemstyle horizontalalign = "center"> </itemstyle>
</ASP: boundcolumn>
</Columns>
</ASP: DataGrid>
2. Events called;
This. mydatagrid. deletecommand + = new system. Web. UI. webcontrols. datagridcommandeventhandler (this. mydatagrid_deletecommand );
This. mydatagrid. itemdatabound + = new system. Web. UI. webcontrols. datagriditemeventhandler (this. mydatagrid_itemdatabound );
3. Control Multiple buttons in a cell
Private void mydatagrid_itemdatabound (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
If (E. Item. itemindex> = 0 ){
If (E. Item. cells [7]. Text. Trim () = "C "){
Tablecell optd = (tablecell) E. Item. controls [3];
// Pop-up editing
Tablecell opedit = (tablecell) E. Item. controls [4];
Optd. Controls. Add (opedit. controls [0]);
// Delete button
Tablecell opdel = (tablecell) E. Item. controls [5];
Optd. Controls. Add (opdel. controls [0]);
}
}
}
4. Click Delete to execute the operation;
Private void mydatagrid_deletecommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
String examnameid = E. Item. cells [6]. Text. Trim ();
String strsql = "delete examsubject where examnameid =" + examnameid;
Strsql + = ";";
Strsql + = "delete examname where examnameid =" + examnameid;
If (db. insertmoredb (strsql ))
{
Response. Write (JS. inforerror ("test time deleted successfully! "));
}
Else {
Response. Write (JS. inforerrorback ("Hello, teacher! The score of this test cannot be deleted because it has been entered. If you need to delete this test, delete the score of this test first. Thank you! "));
}
Examinit ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.