Implementation of logic processing for interaction between web pages (aspx) and user controls (ascx)

Source: Internet
Author: User

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:

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.