Several pages (ASPX) use the same controls, one text box and two buttons (search and export). For better maintenance, place the same parts on a user control (ASCX. The processing logic is as follows:
The search event processing logic is processed on each page. The problem is, how do I write the Click event in the user control on each page?
The search results on each page are displayed in different formats.
The default export type of the user control is Enabled = "false". If a search result is displayed, the control is set to true. How does it know whether results are available on each page?
The value of the text box of the user control, used as the search condition, that is, the parameters of the execution method of each page. How does aspx obtain the value of the text box in the user control ascx?
The following is ascx:
Copy codeThe Code is as follows:
<% @ Control Language = "C #" AutoEventWireup = "true" CodeFile = "SearchForm. ascx. cs" Inherits = "System_Assets_Report_SearchForm" %>
<Div style = "margin: 3px;">
Asset No. <asp: TextBox ID = "TextBoxAssetsNumber" runat = "server" CssClass = "textbox" Height = "20px"> </asp: TextBox>
<Asp: Button ID = "ButtonSearch" runat = "server" Text = "Search" OnClick = "ButtonSearch_Click"/>
<Asp: Button ID = "ButtonExport" runat = "server" Text = "export to Excel" OnClick = "ButtonExport_Click" Enabled = "false"/>
</Div>
To solve the Click Event of the search on each page, Insus. NET uses the delegate (delegate) in the user control ):
Copy codeThe Code is as follows:
Public delegate void SearchEventHandler (object sender, EventArgs e );
Public delegate void ExportEventHandler (object sender, EventArgs e );
In addition, to check whether there are records in the search results and whether the corresponding export button is disabled and enabled, write a method with parameters in the user control:
Copy codeThe Code is as follows:
Public void EnabledExportButton (bool enabled)
{
This. ButtonExport. Enabled = enabled;
}
To add the value of the text box of the user control to the aspx. cs page, you also need to write a public modifier:
Copy codeThe Code is as follows:
Public string GetAssetsNumber ()
{
Return this. TextBoxAssetsNumber. Text. Trim ();
}
Complete ascx. cs code:
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using Insus. NET;
Public partial class System_Assets_Report_SearchForm: BaseUserControl
{
Public delegate void SearchEventHandler (object sender, EventArgs e );
Public delegate void ExportEventHandler (object sender, EventArgs e );
Public event SearchEventHandler SearchClick;
Public event ExportEventHandler ExportClick;
Public string GetAssetsNumber ()
{
Return this. TextBoxAssetsNumber. Text. Trim ();
}
Public void EnabledExportButton (bool enabled)
{
This. ButtonExport. Enabled = enabled;
}
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void ButtonSearch_Click (object sender, EventArgs e)
{
If (SearchClick! = Null)
SearchClick (this, e );
}
Protected void ButtonExport_Click (object sender, EventArgs e)
{
If (ExportClick! = Null)
ExportClick (this, e );
}
}
For each aspx page, only one page is shown below:
. Aspx. cs sample code: