Implement winform Effect of click color change in gridview

Source: Internet
Author: User
1. Add the ridiobutton control to the gridview. < ASP: gridview ID = "Gridview1" Runat = "Server"
Performanceid = "Objectperformance1" Skinid = "Common" Onrowdatabound = "Gridview1_rowdatabound" >
< Columns >
< ASP: boundfield Datafield = "Depid" Headertext = "Department ID" Sortexpression = "Depid" />
< ASP: boundfield Datafield = "Depname" Headertext = "Department name" Sortexpression = "Depname" />
< ASP: boundfield Datafield = "Principal" Headertext = "Owner" Sortexpression = "Principal" />
< ASP: boundfield Datafield = "Depmemo" Headertext = "Remarks" Sortexpression = "Depmemo"   />
< ASP: templatefield Insertvisible = "False" >
< Itemtemplate >
< Input ID = '<% # "Select _" + Databinder. eval (container, "dataitem. depid") % > 'Name = "select" type = "radio"/>
</ Itemtemplate >
</ ASP: templatefield >
</ Columns >
</ ASP: gridview >

2. Write color changes in the rowdatabound event of the gridview Code Protected   Void Gridview1_rowdatabound ( Object Sender, gridviewroweventargs E)
{
If (E. Row. rowtype = Datacontrolrowtype. datarow)
{
Datarowview row = (Datarowview) E. Row. dataitem;
String ID = Row [ " Depid " ]. Tostring ();
E. Row. Attributes. Add ( " Onclick " , " Select "   + ID +   " Checked = true; " );
}  
If (E. Row. rowtype = Datacontrolrowtype. datarow)
{
E. Row. Attributes. Add ( " Onmouseover " , " If (this! = PREV) {c = This. style. backgroundcolor; this. style. backgroundcolor = '# d8f3c6 '} " ); // Change the background color when the mouse stays
E. Row. Attributes. Add ( " Onmouseout " , " If (this! = PREV) {This. style. backgroundcolor = c} " ); // Restore the background color when you move the mouse away.
E. Row. attributes [ " Style " ] =   " Cursor: Hand " ; // Set the floating mouse pointer to "little hand"

// Datarowview ROW = (datarowview) E. Row. dataitem;
// String id = row ["depid"]. tostring ();
// E. Row. Attributes. Add ("onclick", "select _" + E. Row. cells [0]. Text + ". Checked = true ;");

E. Row. Attributes. Add ( " Onclick " , " Select _ "   + E. Row. cells [ 0 ]. Text +   " . Checked = true; selectx (this) " );
}
}

3. Write control functions in front-end JS scripts < Script Language = "JavaScript" Type = "Text/JavaScript"   >
VaR Prev = Null ;
Function Selectx (ROW) /**/ /*Change the color of the selected row to the color of the selected row.*/
{
If (Prev ! = Null )
{
Prev. style. backgroundcolor='# E4f7d8 ';
}
Row. style. backgroundcolor = '# 8ec26f ';
Prev = Row;

}
</ Script >

Finally, remember to use request. Form ["select"] in the background to receive the selected value. In addition, you can hide the radiobuttou for better results.

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.