Solve repetitive work-an extension solution for the gridview

Source: Internet
Author: User

See the chart description:

The above two lists are created by the gridview, where the Red Two tables are the same, and the purple part in the middle changes according to the content. In the past, I used to define all columns in the gridview. Obviously, I have done a lot of repetitive work on the public part. I don't know how you handle this situation?

Today, the gridview was extended and a preliminary idea was made. The solution is listed below, which is still somewhat confusing. I hope you can discuss the solution.

1. First create a managergridview class that inherits the gridview. I will write it in app_code here. The custom control project is not proficient.

Namespace Mycontrol
{
Public   Class Managergridview: gridview
{
Public Managergridview ()
{
}
}
}

2. Create two Web user controls for public Columns

(1) index column: gvindex. ascx

<% @ Control Language = " C # " Autoeventwireup = " True " Codefile = " Gvindex. ascx. CS " Inherits = " Usercontrol_gvindex "   %>
<% = (This. Parent. Parent As Gridviewrow). dataitemindex + 1   %>

(2) operation column: gvoprate. ascx

<% @ Control Language = " C # " Autoeventwireup = " True " Codefile = " Gvoprate. ascx. CS " Inherits = " Gvoprate "   %>
< ASP: button ID = "Btedit" Runat = "Server" Text = "Modify" Commandname = "Editrow" Commandargument = '<% # Eval ("ID ") % > '/>
< ASP: button ID = "Btdelete" Runat = "Server" Text = "Delete" Commandargument = '<% # Eval ("ID ") % > '
Onclientclick = "Return confirm ('delete is an unrecoverable operation. Continue? '); "Commandname =" deleterow "/>

The processing here is temporarily processed in rowcommand mode, and the button is added with commandname.

3. Introduce two columns in managergridview. here we need to override the oninit event of the gridview.

Protected   Override   Void Oninit (eventargs E)
{
// Insert index Column
Templatefield ind =   New Templatefield ();
Ind. itemtemplate = Page. loadtemplate ( " ~ /Usercontrol/gvindex. ascx " );
This . Columns. insert ( 0 , IND );
Ind. headertext =   " Serial number " ;

// Insert operation Column
Templatefield OPR =   New Templatefield ();
OPR. itemtemplate = Page. loadtemplate ( " ~ /Usercontrol/gvoprate. ascx " );
This . Columns. Add (OPR );
OPR. headertext =   " Operation " ;

Base. Oninit (E );
}

Okay. The extension is coming to an end.

4. For extended use, deptmanager. aspx takes the Department management page as an example.

(1) register this control on the page

<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Deptmanager. aspx. CS " Inherits = " Crm_deptmanager "   %>
<% @ Register namespace = " Mycontrol " Tagprefix = " MC "   %>

(2) Use Controls on the page

  < MC: managergridview ID = "Gvdept" Runat = "Server"
Autogeneratecolumns = "False" Width = "600px"
Rowstyle-horizontalalign = "Center" Onrowcommand = "Gvdept_rowcommand"   >
< Columns >
< ASP: boundfield Datafield = "Partname" Headertext = "Department name"   />
< ASP: boundfield Datafield = "Description" Headertext = "Department description"   />
</ Columns >
</ MC: managergridview >

As shown above, we only need to add the Department name and description columns in columns and add onrowcommand in response to the event.

(3) bind data in the background, which is the same as the usage of the gridview.

(4) handling events

Protected   Void Gvdept_rowcommand ( Object Sender, gridviewcommandeventargs E)
{
If (E. commandname =   " Editrow " )
{
Ediedepart ();
}
Else
{
Deletedepart ();
} }

The above is the idea of the entire solution, and there are still many imperfections. You are welcome to give your comments.

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.