ASP. NET Web Custom control

Source: Internet
Author: User

0. Calling code

protected override void Page_Load (object sender, EventArgs e)
{
Copy to base class service interface, no pay
if (IsPostBack)
{
if (session["ReportTable3.Dev.Session"]! = NULL)
{
var dt = (System.Data.DataSet) session["ReportTable3.Dev.Session"];
Aspxgridview1.datasource = DT;
Aspxgridview1.databind ();
}
}

1. Here AfterSelect is a delegate object, inheriting the multicast delegate, here is the registration, the final execution of the delegate will be the content of the delegate execution once, + = can also be referred to as a delegate registration method instance
This . Mypagebar1.afterselect + = Mypagebar1_afterselect;

Base. Page_Load (sender, E);
}

2. The method of delegate or delegate content registration is not immediately executed, the event needs to be triggered, you can pass the sender object and the event argument E

private void Mypagebar1_afterselect (object sender, Usercontrols.selecteventargs e)
{

Aspxgridview1.datasource = E.datasource;
Aspxgridview1.databind ();
}

1. First on the core code. Ascx.cs

Using System;
Using System.Collections.Generic;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Devexpress.web;
Using DevExpress.XtraPrinting.Native;
Using Framework;

Namespace Hraweb.usercontrols
{
public partial class MyPageBar:System.Web.UI.UserControl
{
public delegate void Begbinddataeventhandler (object sender, Selecteventargs e);
public event Begbinddataeventhandler AfterSelect;
Private Contract.IService.IDaoService _dao;
<summary>
Common Database Operations
</summary>
Public virtual Contract.IService.IDaoService Dao
{
Get
{
if (_dao = = null)
_dao = (Contract.IService.IDaoService) ctx["Daoservice"];
return _dao;
}
}
Private Spring.Context.IApplicationContext _ctx;
Protected Spring.Context.IApplicationContext CTX
{
Get
{
if (_ctx = = null)
_ctx = Spring.Context.Support.ContextRegistry.GetContext ();
return _ctx;
}
}
private int totalblock = (PageCount-1)/UNITQ + 1;

public Object DataSource
{
Get
Set
}

private int pagecount = (TotalCount-1)/PageSize + 1;


public int TotalCount {get; set;}

private int PageIndex {get; set;}
public int PageSize {get; set;}

Public Aspxgridview Viewsource {get; set;}

public void Griddatabind ()
{
if (session["searchinfo"] = = null)
{
session["SearchInfo"] = SearchInfo;
}
Loadblock (Currentblock);
if (session["SearchInfo"]! = NULL)
{
SearchInfo = (queryinfo) session["SearchInfo"];
Searchinfo.pagesize = PageSize;
Searchinfo.startrecord = (PageIndex-1) * PageSize;
Searchinfo.totalcount = 1;
}

if (session["viewsource"] = = null)
{
session["Viewsource"] = Viewsource;
}
Else
{
Viewsource = (Aspxgridview) session["Viewsource"];
}
DataTable dt = Dao.excutedataset (searchinfo). Tables[0];

3. The event is actively triggered here (in fact, the invocation of the delegate executes the previously registered method and passes the parameter who fired the object sender, and the pass event argument e)

var onafterselect = this. AfterSelect;
Onafterselect?. Invoke (This, new Selecteventargs (DT));

}

Public QueryInfo SearchInfo {get; set;}

protected void Page_Load (object sender, EventArgs e)
{
Currentblock = 1;
if (session["Currentblock"]! = NULL)
{
Currentblock = Int. Parse (session["Currentblock"). ToString ());

}

PageIndex = PageIndex = = 0? 1:pageindex;
PageSize = PageSize = = 0? 6:pagesize;
TotalCount = 100;
Controls.Add ("1", LinkButton1);
Controls.Add ("2", LinkButton2);
Controls.Add ("3", LinkButton3);
Controls.Add ("4", LinkButton4);
Controls.Add ("5", LinkButton5);
Controls.Add ("6", LinkButton6);
}

public void Btn_change (object sender)
{
for (int i = 0; i < Controls.Count; i++)
{
controls[(i + 1). ToString ()]. Enabled = true;
}
LinkButton btn = (sender as LinkButton);
if (btn! = null)
{
Btn. Enabled = false;
PageIndex = Int. Parse (btn. Text);
Griddatabind ();

}
}

<summary>
1
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void LinkButton1_Click (object sender, EventArgs e)
{
Btn_change (sender);
}
<summary>
2
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Linkbutton2_click (object sender, EventArgs e)
{
Btn_change (sender);

}
<summary>
3
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Linkbutton3_click (object sender, EventArgs e)
{
Btn_change (sender);
}

<summary>
4
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>

protected void Linkbutton4_click (object sender, EventArgs e)
{
Btn_change (sender);
}
<summary>
5
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Linkbutton5_click (object sender, EventArgs e)
{
Btn_change (sender);
}
<summary>
6
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Linkbutton6_click (object sender, EventArgs e)
{
Btn_change (sender);
}

public int Currentblock {get; set;}

private void Loadblock (int currentblock)
{
session["Currentblock"] = Currentblock;
for (int i = 0; i < Controls.Count; i++)
{
controls[(i + 1). ToString ()]. Enabled = true;
controls[(i + 1). ToString ()]. Visible = true;
}
int start = (CurrentBlock-1) * UNITQ + 1;
int end = 0;
for (int i = 0; i < UNITQ; i++)
{
End = start + i;
if (End < PageCount)
{
controls[(i + 1). ToString ()]. Text = end. ToString ();
}
Else
{
controls[(i + 1). ToString ()]. Visible = false;
}

}
}

protected void Imagebuttonpre_click (object sender, ImageClickEventArgs e)
{
if (Currentblock > 1)
{
currentblock--;
}
Loadblock (Currentblock);

}


public int UNITQ = 6;

Public dictionary<string, linkbutton> Controls = new dictionary<string, linkbutton> ();
protected void Imagebuttonnext_click (object sender, ImageClickEventArgs e)
{

if (Currentblock < Totalblock)
{
currentblock++;
}
Loadblock (Currentblock);

}
}

4 Defining Inheritance for event arguments EventArgs
Public class Selecteventargs:eventargs
{
Private DataTable _datasource = null;
Public DataTable DataSource
{
get {return _datasource;}
}

Public Selecteventargs (DataTable data)
{
_datasource = data;
}
}

}

2. Back to the front desk code, nothing to look at

<%@ Control language= "C #" autoeventwireup= "true" codebehind= "MyPageBar.ascx.cs" inherits= " HraWeb.UserControls.MyPageBar "%>


<asp:imagebutton id= "Imagebuttonpre" runat= "Server" height= "20px" imageurl= "~/images/previous.png" Width= "20px" onclick= "Imagebuttonpre_click"/>
<asp:linkbutton id= "LinkButton1" text= "1" runat= "Server" onclick= "LinkButton1_Click" >1</asp:linkbutton >
<asp:linkbutton id= "LinkButton2" text= "2" runat= "Server" onclick= "Linkbutton2_click" >2</asp:linkbutton >
<asp:linkbutton id= "LinkButton3" text= "3" runat= "Server" onclick= "Linkbutton3_click" >3</asp:linkbutton >
<asp:linkbutton id= "LinkButton4" text= "4" runat= "Server" onclick= "Linkbutton4_click" >4</asp:linkbutton >
<asp:linkbutton id= "LinkButton5" text= "5" runat= "Server" onclick= "Linkbutton5_click" >5</asp:linkbutton >
<asp:linkbutton id= "LinkButton6" text= "6" runat= "Server" onclick= "Linkbutton6_click" >6</asp:linkbutton >
<asp:imagebutton id= "Imagebuttonnext" runat= "Server" imageurl= "~/images/next.png" height= "20px" Width= "20px" onclick= "Imagebuttonnext_click" viewstatemode= "Enabled"/>

3. Designer code (automatic generation can be used without tube)

//------------------------------------------------------------------------------
< automatic generation >
This code is generated by the tool.
//
Changes to this file may cause incorrect behavior if
If you regenerate the code, your changes will be lost.
</Automatic Generation >
//------------------------------------------------------------------------------

Namespace Hraweb.usercontrols {


Public partial class Mypagebar {

<summary>
The Imagebuttonpre control.
</summary>
<remarks>
Auto-generated fields.
To make modifications, move the field declaration from the designer file to the code-behind file.
</remarks>
protected Global::system.web.ui.webcontrols.imagebutton Imagebuttonpre;

<summary>
The LinkButton1 control.
</summary>
<remarks>
Auto-generated fields.
To make modifications, move the field declaration from the designer file to the code-behind file.
</remarks>
protected Global::system.web.ui.webcontrols.linkbutton LinkButton1;

<summary>
The LinkButton2 control.
</summary>
<remarks>
Auto-generated fields.
To make modifications, move the field declaration from the designer file to the code-behind file.
</remarks>
protected Global::system.web.ui.webcontrols.linkbutton LinkButton2;

<summary>
The LinkButton3 control.
</summary>
<remarks>
Auto-generated fields.
To make modifications, move the field declaration from the designer file to the code-behind file.
</remarks>
protected Global::system.web.ui.webcontrols.linkbutton LinkButton3;

<summary>
The LinkButton4 control.
</summary>
<remarks>
Auto-generated fields.
To make modifications, move the field declaration from the designer file to the code-behind file.
</remarks>
protected Global::system.web.ui.webcontrols.linkbutton LinkButton4;

<summary>
The LinkButton5 control.
</summary>
<remarks>
Auto-generated fields.
To make modifications, move the field declaration from the designer file to the code-behind file.
</remarks>
protected Global::system.web.ui.webcontrols.linkbutton LinkButton5;

<summary>
The LinkButton6 control.
</summary>
<remarks>
Auto-generated fields.
To make modifications, move the field declaration from the designer file to the code-behind file.
</remarks>
protected Global::system.web.ui.webcontrols.linkbutton LinkButton6;

<summary>
The Imagebuttonnext control.
</summary>
<remarks>
Auto-generated fields.
To make modifications, move the field declaration from the designer file to the code-behind file.
</remarks>
protected Global::system.web.ui.webcontrols.imagebutton Imagebuttonnext;
}
}

ASP. NET Web Custom control

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.