Asp.net custom date Control Method

Source: Internet
Author: User
Tags tagname
In a recently developed Asp.net 2.0 system, a date selection box is often used on the query interface to export data to the business department. I read a book published on www.asp.net two weeks ago. Asp.net data tutorial Have some knowledge about the layer-3 structure development method of Asp.net (dataset, objectdatastore ). Last week, I wrote two examples on ASP.net. Program Code Duwamish7 and petshop3.0 (a competitive version of PetStore on the J2EE platform ). The code analysis of these two programs is also available on the Internet. duwamish7 uses a custom dataset + sqlconnection to encapsulate the stored procedure. petshop3.0 adopts a common three-tier structure, and the data access layer Dal uses ORM ing, map a row in the result table into an object (such as customer, product, order, etc., taking into account the paging mechanism), and implement business logic such as BLL processing parameter validation at the business logic layer, the presentation layer processes the display of data (separation of content and form ). After reading the code, I feel that the custom controls in Asp.net are still very powerful. Code reuse is more advanced than the tag library in J2EE, which is especially suitable for various form query controls. For example, various input boxes and drop-down boxes are combined based on the business logic for reuse.

A Date input box is used in this system. The Date title can be changed (Operation Date, application date, confirmation date, start date, end date ....), it can be blank or required. When the cursor is in the input box, the date selection box is displayed automatically. In combination with these requirements, a date Selection control is encapsulated.

ASP code
<% @ Control Language = "C #" autoeventwireup = "true" codefile = "customdate. ascx. cs" inherits = "control_startenddate" %>

<Asp: Label id = "label1" runat = "server" text = "label"> </ASP: Label>
<Asp: textbox id = "txtenddate" runat = "server" onfocus = "setday (this)" width = "88px"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator1" runat = "server" controltovalidate = "txtenddate"
Enabled = "false" display = "NONE">
</ASP: requiredfieldvalidator>

The CS code corresponding to ASP is:
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class control_startenddate: system. Web. UI. usercontrol
{
Protected void page_load (Object sender, eventargs E)
{
// Label1.text is dynamically changed, so the verification information is also dynamically generated.
Requiredfieldvalidator1.errormessage = label1.text + "cannot be blank! ";
}

// String value of the current date
Public String datestring
{
Get
{
Return txtenddate. text;
}
}

// The title name of the date (Operation Date, application date, confirmation date, start date, end date ....)
Public String datetitle
{
Set
{
// "End Date ";
Label1.text = value;
}
}

 

// whether the date can be blank, whether to enable requiredfieldvalidator
Public bool nullable
{< br> set
{< br> // bool nullable = bool. parse (value);
If (value = true)
requiredfieldvalidator1.enabled = false;
else
requiredfieldvalidator1.enabled = true;
}< BR >}

// Start date string text
Public String initialtext
{
Set
{
Txtenddate. Text = value;
}
}

// Initial date value
Public String initialdate
{
Set
{
// If (ispostback) return;

String datestr = "";
Datetime dt = datetime. Today; // obtain the date of the current day

// Take the date of the previous Open Day
If ("lastopenday". Equals (value ))
{
// For the sake of simplicity, take the day before yesterday for the previous open day (you must query the database for the exact date)
Datetime newdt = DT. adddays (-2 );
Datestr = newdt. tostring ("yyyy-mm-dd ");
}
Else if ("today". Equals (value ))
{
Datestr = DT. tostring ("yyyy-mm-dd ");
}
Else
{
}

Txtenddate. Text = datestr;
}
}
}

Example
<% @ Page Language = "C #" masterpagefile = "~ /Masterpage/navigation. Master "autoeventwireup =" true"
Codefile = "4154areadis. aspx. cs" inherits = "report_4254birthsex" theme = "Harvest" %>

 

<% @ Register src = "../control/fundcodelist. ascx" tagname = "fundcodelist" tagprefix = "uc1" %>
<% @ Register src = "../control/customdate. ascx" tagname = "customdate" tagprefix = "UC2" %>
<% @ Register Assembly = "Microsoft. reportviewer. webforms, version = 8.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a"
Namespace = "Microsoft. Reporting. webforms" tagprefix = "rsweb" %>

<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder1" runat = "server">

<Div>
<Uc1: fundcodelist id = "fundcodelist1" runat = "server" showcustomitem = "true"/>
<UC2: customdate id = "customdate1" runat = "server" datetitle = "End Date" nullable = "false"
Initialdate = "lastopenday"/>
<Asp: button id = "button1" runat = "server" text = "query" onclick = "button#click"/>
<Asp: validationsummary id = "validationsummary1" runat = "server" displaymode = "bulletlist"
Showmessagebox = "true" showsummary = "false"/>
<Rsweb: reportviewer id = "reportviewer1" runat = "server" font-names = "verdana" font-size = "8pt"
Style = "width: 100%; Height: 120%">
<Localreport>
</Localreport>
</Rsweb: reportviewer>
<Asp: objectdatasource id = "objectperformance1" runat = "server" selectmethod = "get4244areadis"
Typename = "VM. businessrule. custanalysis">
<Selectparameters>
<Asp: controlparameter controlid = "fundcodelist1" name = "fundcode" propertyname = "fundcode"
Type = "string"/>
<Asp: controlparameter controlid = "customdate1" name = "enddate" propertyname = "datestring"
Type = "string"/>
</Selectparameters>
</ASP: objectdatasource>
</Div>
</ASP: content>

 

For more information, see <UC2: customdate id = "customdate1" runat = "server" datetitle = "End Date" nullable = "false"
Initialdate = "lastopenday"/> in this line, datetitle is the date title, nullable is set to allow null, and initialdate is set to the initial date (the same day, the previous open day)
<Asp: validationsummary id = "validationsummary1" runat = "server" displaymode = "bulletlist"
Showmessagebox = "true" showsummary = "false"/> to summarize the verification information of each control, unified display (showmessagebox = "true" indicates that verification information is displayed in the pop-up message box ).

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.