Asp. NET to implement the abstract of the background code of similar pages and dynamic GridView Dynamic column data binding

Source: Internet
Author: User
Tags abstract foreach visual studio visual studio 2010

The following is a asp.net demo, hoping to deepen your understanding of polymorphism.

Now the demand is like this (of course the demand is with the help of problems encountered in the recent project), in the process management of this system, there are two pages, one shows the approval list which I initiated, the other is the list waiting for me to approve, their query and list display and view approval history and so on are consistent, The only difference is that there's a column for approval and an executable action, but different people or in different applications (I am here to assume that the system is a complex system with multiple applications) inside get the list of columns is not the same, so need to use dynamic columns, of course, these data sources I do not repeat here, for For example I wouldn't go to the database, you can construct a class that contains a set of attributes as columns, and a property as a dataset, of course the following examples are involved.

My environment for Windows 7+ Visual Studio 2010, I created a webapplication, named WebApplication3, I will not choose an empty Web application to look good for the page. Add two ASPX pages (Myapprovals and Toapprovals) and add links to the master page navigation (if you do not have a master page you can skip this step to create a page directly)

<asp:menu id= "Navigationmenu" runat= "Server" cssclass= "menu" enableviewstate= 

"false" includestyleblock= " False "orientation=" Horizontal >
                    <Items>
                        <asp:menuitem navigateurl= "~/default.aspx" text= "Home" />
                        <asp:menuitem navigateurl= "~/myapprovals.aspx text=" My Application "/> <asp:menuitem NavigateUrl=
                        " ~/ Toapprovals.aspx "text=" to my Approval "/> <asp:menuitem navigateurl=" ~/about.aspx "text="
                        about "/>
                    </Items >
                </asp:Menu>

The page code is simple, Myapprovals's page code is as follows:

<p>
    <asp:textbox id= "Txtkey" runat= "Server" ></asp:TextBox>&nbsp;
    <asp:button id= "btnsearch" runat= "Server" text= "button" onclick= "btnSearch_Click"/> <asp:label
    ID = "Lblmsg" runat= "Server" forecolor= "Red" ></asp:Label>
</p>
<p>
    <asp:gridview Id= "Gvapprovals" runat= "Server" >
    </asp:GridView>
</p>

Toapprovals the page code and post it later.

The base class name for their background code is approvalbasepage, and we probably have a more basepage base class in actual development, when Approvalbasepage should inherit from BasePage,

View Code public class Approvalbasepage:basepage {///<summary>///0 for my Approval page, 1 for
    
        Approval of the page, you can subclass to determine the type///</summary> protected virtual int PageType {get {0;}}
        Private Approvaldata DataSource; <summary>///Get data source///</summary> approvaldata DataSource {g ET {if (This.datasource = = null) {This.datasource = appr Ovaldata.getdata (this.
                PageType);
            return this.datasource; }///<summary>///data binding, parameters only as a demonstration,///because the binding may have some interaction with the user, these interactions are likely to be different, so leave the subclass to heavy
    
        Write///But in this case their data bindings are almost the same///</summary> protected virtual void Binddata (string key = "") {} <summary>///converts data into DataTable for easy binding///</summary>///<returns> </Returns> protected DataTable Datatotable () {DataTable dt = new DataTable (); if (this.
            DataSource = = null) {return null; foreach (var item in this.) datasource.headlist) {dt.
            Columns.Add (item, typeof (String));
            DataRow dr = null;
            int colindex = 0; foreach (var item in this.) datasource.contentlist) {dr = dt.
                NewRow ();
                Colindex = 0;
                    foreach (var itemcol in item) {dr[colindex++] = Itemcol; if (colindex >= dt. Columns.count) {//Although the guaranteed data provided for this interface must correspond to the number of columns//But I still try to keep the data different
                    Often, the program does not error break; } dt.
            Rows.Add (DR);
        } return DT;
   } 
        #region event two pages of common events are put in here. protected void Page_Load (object sender, EventArgs e) { if (!
            IsPostBack) {binddata ();
        } protected void btnSearch_Click (object sender, EventArgs e) {binddata (); } #endregion}

Related Article

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.