Add row-Click Event in gridview in ASP. NET

Source: Internet
Author: User

The gridview is used to display data during the two days of OA. To improve user experience. Write a single-host row event. The result showed that the gridview did not have this event, so I had to turn to Baidu.

Finally, the solution was found. Okay, I don't have to talk about it anymore. PasteCode

First, set a hidden buttonfield on the foreground.

 
<Columns>
<Asp: buttonfield visible ="False"TEXT ="Singleclick"Commandname ="Singleclick"/>
</Columns>

Write the code in the rowdatabound and rowcommand events.

  Protected   Void Gvroleinfos_rowdatabound ( Object Sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
Linkbutton _ singleclickbutton = (linkbutton) E. Row. cells [ 0 ]. Controls [ 0 ];
String _ Jssingle = clientscript. getpostbackclienthyperlink (_ singleclickbutton, "" );
E. Row. attributes [ " Onclick " ] = _ Jssingle;



}
}






Protected Void Gvroleinfos_rowcommand ( Object Sender, gridviewcommandeventargs E)
{
Gridview _ gridview = sender As Gridview;
Int _ Selectindex = Int . Parse (E. commandargument. tostring ());
String _ Commandname = E. commandname;
If (_ Commandname = " Singleclick " )
{
_ Gridview. selectedindex = _ selectindex;
}

}

Then rewrite the render method on this page.

   Protected   Override   Void Render (htmltextwriter writer)
{
Foreach (Gridviewrow rowIn Gvroleinfos. Rows)
{
If (Row. rowtype = datacontrolrowtype. datarow)
{
Page. clientscript. registerforeventvalidation (row. uniqueid + " $ Ct100 " );
Page. clientscript. registerforeventvalidation (row. uniqueid + " $ Ct101 " );
Page. clientscript. registerforeventvalidation (row. uniqueid +" $ Ct102 " );
}
}
Base . Render (writer );
}

 

Address: http://www.cnblogs.com/webabcd/archive/2007/04/22/723113.html

Http://www.codeproject.com/KB/webforms/EditGridviewCells.aspx

 

 

 

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.