Complete code to implement the prompt of the delete button of the gridview

Source: Internet
Author: User
Today we have finally achieved the complete Code In the left-side navigation pane.

First, the HTML code:<ASP: gridview ID="Subjectgrid"Runat="Server">
</ASP: gridview>

Step 2: Initialize the gridview columns: VoidPage_load (Object sender, eventargs E)
{
Showgrid ();
}

Private   Void Showgrid ()
{
Datatable mermertable
=   New Datatable ( " MERs " );

Grdsubject. autogeneratecolumns
=   False ;
Grdsubject. showheader
=   False ;
Grdsubject. datakeynames
=   New String [] { " ID " };
Datacontrolfieldcollection dcfc
= grdsubject. columns;
dcfc. Clear ();
boundfield BF;

BF = New boundfield ();
BF. datafield
= " id " ;
BF. visible
= false ;
dcfc. add (BF);

BF= NewBoundfield ();
BF. datafield
= "Title";
Dcfc. Add (BF );

BF =   New Boundfield ();
BF. datafield
=   " Begindate " ;
BF. sortexpression
=   " Begindate " ;
BF. htmlencode
=   False ;
BF. dataformatstring
=   " {0: yyyy-mm-dd} " ;
Dcfc. Add (BF );

BF =   New Boundfield ();
BF. datafield
=   " Enddate " ;
BF. htmlencode
=   False ;
BF. dataformatstring
=   " {0: yyyy-mm-dd} " ;
Dcfc. Add (BF );

Buttonfield selectrow =   New Buttonfield ();
Selectrow. buttontype
= Buttontype. Button;
Selectrow. commandname
=   " Select " ;
Selectrow. Text
=   " Select " ;
Dcfc. Add (selectrow );

Buttonfield delrow =   New Buttonfield ();
Delrow. buttontype
= Buttontype. Button;
Delrow. accessibleheadertext
=   " Delete " ;
Delrow. commandname
=   " Delete " ;
Delrow. Text
=   " Delete " ;
Delrow. causesvalidation
=   True ;
Dcfc. Add (delrow );

Grdsubject. datasource=DT. defaultview;
Grdsubject. databind ();
}

Step 3: rowdatabound Void Grdsubject_rowdatabound ( Object Sender, gridviewroweventargs E)
{
// Determine whether it is datarow
If (E. Row. rowtype = Datacontrolrowtype. datarow)
{
// Effect when the mouse goes through the row
E. Row. Attributes. Add ( " Onmouseover " , " E = This. style. backgroundcolor; this. style. backgroundcolor = 'linen' " );
E. Row. Attributes. Add (
" Onmouseout " , " This. style. backgroundcolor = E " );

// When the start time is later than the current time, the behavior blue is displayed.
If (Datetime. parse (E. Row. cells [ 2 ]. Text) > Datetime. Now)
{
E. Row. backcolor
= Color. lightskyblue;
}
// When the end time is earlier than the current time, the behavior is grayed out.
If (Datetime. parse (E. Row. cells [ 3 ]. Text) < Datetime. Now)
{
E. Row. backcolor
= Color. Silver;
}

// Activation prompt when the delete button is clicked
Button BTN = (Button) E. Row. cells [ 5 ]. Controls [ 0 ];
BTN. Attributes. Add (
" Onclick " , " Javascript: Return confirm ('Are you sure you want to delete :\ "" + E. Row. cells [1]. Text + " \ " ? ') " );
}
}

The key is button BTN = (button) E. Row. cells [5]. controls [0]; that is, the event is declared to be called by the first control in the 6th column.

Step 4: Execute VoidGrdsubject_rowdeleting (ObjectSender, gridviewdeleteeventargs E)
{

}

Void Grdsubject_rowcommand ( Object Sender, gridviewcommandeventargs E)
{
// Throw new exception ("the method or operation is not implemented.") occurs when you click the button in the grid .");
Int Selindex = Convert. toint32 (E. commandargument );
Gridviewrow selectedrow
= Grdsubject. Rows [selindex];

If (E. commandname =   " Select " )
{
Txtsubjecttitle. Text
= Selectedrow. cells [ 1 ]. Text;
Datebegin. Value
= Selectedrow. cells [ 2 ]. Text;
Dateend. Value
= Selectedrow. cells [ 3 ]. Text;

}
If (E. commandname =   " Delete " )
{
HSF. Touch. DTO. subject item
=   New HSF. Touch. DTO. Subject ();
Item. ID
=   Int . Parse (grdsubject. datakeys [selindex]. value. tostring ());
Try
{
Touchfactory. createsubjectmanage (). Delete (item );
// Deleted successfully. The content in the input box is cleared.
Clear ();
}
Catch (Exception ERR)
{
Showmessagebox (
" Failed to delete \ n "   + Err. Message );
}
}

Showgrid ();
}

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.