Data Entry Check Diagram On Form At Client And Server


OnSelectionChanged

The name of the function that is executed when the user selects an option.

day, week or month

OnVisibleMonthChanged

The name of the function that is executed when the user moves to it.

another month

Maybe you are interested!

For example: The aspx page is as follows:

<body>

<Form id="Form1" runat="Server">

<asp:Calendar ID="Calendar1" DayNameFormat="Full" runat="Server">

<WeekendDayStyle BackColor="#fafad2" ForeColor="#ff0000" />

<DayHeaderStyle ForeColor="#0000ff" />

<TodayDayStyle BackColor="#00ff00" />

</asp:Calendar>

</Form>

</body>

The .aspx page above displays a calendar, the days of the week are written in full blue, weekends are written in red on a yellow background, the current date is shown with a green background as shown below:

Figure 3.37. Illustration of using the Calendar control

For example: The aspx page is as follows:

<body>

<Form id="Form1" runat="Server">

<asp:Calendar DayNameFormat="Full" runat="Server" SelectionMode="DayWeekMonth" SelectMonthText="<*>"

SelectWeekText="<->"/>

<SelectorStyle BackColor="#f5f5f5" />

</asp:Calendar>

</Form>

</body>

The aspx page above displays a calendar with the days of the week listed in full, the user can select a day, a week and a month, the selected day/week/month is displayed with a gray background color as shown below:

Figure 3.38. Illustration of using the Calendar control

- CalendarDay Control

The CalendarDay Control represents a day in a calendar Control.

Attributes:


Properties

Describe

Date

Day of the Day variable

DayNumberText

Day number (day) of the day

IsOtherMonth

Specifies whether days in other months are displayed.

Are not

IsSelectable

Determine if the date is selectable

IsSelected

Determine whether a date is selected or not

IsToday

Determines whether it is the current date or not

IsToday

Determine whether it is Saturday or Sunday.

- File Upload Control

The FileUpload control allows users to upload files from their own Web application. The uploaded files can be stored somewhere, either on the hard drive or in the database.

Attributes:


Properties

Describe

Enable

Allows disabling of FileUpload control.


FileBytes

Allows to get the uploaded file contents as a Byte array.

FileContent

Allows to get the contents of the uploaded file line by line

data

FileName

Get the uploaded file name

HasFile

Returns true when File is Uploaded

PostedFile

Enables you to get the uploaded file wrapped in the HttpPostedFile object.

The PostedFile property of the FileUpload control allows to get information from the uploaded File wrapped in an HttpPostedFile object. This object will give more information about the Upload file.

The HttpPostedFile class has the following properties:


Properties

Describe

ContentLength

Get the size of the Upload File in bytes

ContentType

ContentType: get the MIME type of the Upload File

FileName

Allows to get the name of the uploaded file.

InputStream

Allow upload as an input stream.

Example: Upload an image file to the Server's hard drive. Fileupload.aspx page

<body>

<Form id="Form1" runat="Server">

<div>

<asp:Label ID="Label1" runat="Server" Text="Select File"></asp:Label>

<asp:FileUpload ID="FileUpload1" runat="Server" Width="286px" /><br />

<asp:Button ID="Button1" runat="Server" Text="Add image" Width="92px" onclick="Button1_Click" /><hr />

<asp:DataList ID="listImage" RepeatColumns="3" runat="Server">

<ItemTemplate>

<asp:Image ID="Image1" ImageUrl='<%# Eval("Name", "~/Upload/{0}") %>' Runat="Server" /><br />

<%# Eval("Name") %>

</ItemTemplate>

</asp:DataList>

</div>

</Form>

</body>

Fileupload.aspx.cs page code

using System; using System.Data; using System.IO;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Page_PreRender(){

string upload_folder = MapPath("~/Upload/"); DirectoryInfo dir = new DirectoryInfo(upload_folder); listImage.DataSource = dir.GetFiles(); listImage.DataBind();

}

bool CheckFileType(string fileName)

{

string ext = Path.GetExtension(fileName); switch (ext.ToLower())

{

case ".gif": return true; case ".png": return true; case ".jpg": return true; case ".jpeg": return true; default: return false;

}

}

protected void Button1_Click(object sender, EventArgs e){ if (FileUpload1.HasFile){

if(CheckFileType(FileUpload1.FileName)){

string filepath ="~/Upload/" + FileUpload1.FileName; FileUpload1.SaveAs(MapPath(filepath));

}

}

}

}


In the Button1_Click event, check if there is a File to Upload? If yes, check if the uploaded file is in the correct image format using the CheckFileType function. If yes, write the file to the Server using the SaveAs method of the FileUpload control.

- ViewState object

The ViewState object is provided to save the information of the Web page after the Web Server sends the results back to the Client. By default, when created, Web pages will allow the use of the ViewState object through the EnableViewState property (of the Web page) = True.

Assign a value to ViewState.

ViewState("State name") = <value>

Gets values ​​from ViewState object.

<variable> = ViewState("State name")

- ASP.NET page events

When working with ASP.NET pages, we can encounter some page events in the following order: PreInit, Init, InitComplete, PreLoad, Load, LoadComplete, PreRender, PreRenderComplete, UnLoad. To declare ASP.NET page events, go to the View|Component Design menu or R-Click|View Component Design in the Solution Explorer window. Then, click the event icon in the Properties window, a list of page events will be displayed.

Figure 3.39. ASP.NET page events

Init: The Page_Init event occurs first when the Web page is requested.

Load: This event is where most of the processing and initialization of the web page will be done. This event always occurs every time the web page is requested.

PreRender: This event occurs when the Web page is about to be returned to the Client.

Unload: This event is the opposite of the Page_Init event. If the Page_Init event occurs first when the Web page is requested, then here, Page_Unload is the last event, occurring after all other events.

- PageLoad event and IsPostback property

The presentation will be based on the division into the first Request and the Request from the second time onwards. In the case of the first Request, if ASP.NET receives an HTTP Request from the Web Browser, it will create a Form instance. The Form instance will create a tree of Control instances inside it. Then, start the Load event and call the event handler to create it. In the event handler, set the property of the Control in the Control tree to control the display of the screen. Finally, the Form will generate HTML and return it to the Web Browser. The generated HTML is processed to the Control, and the Control will generate HTML that matches its own property. Furthermore, it is possible to save the state of the Control into the ViewState when needed and the tag

<input type=“hidden”> will be generated.

Request case from 2nd time onwards

The content until the part of receiving HTTP Request, creating the tree of Form instance and Control instance is the same as the first Request. In Postback Request includes ViewState created in the previous Response, so it will set Control property the same as when returning the previous Response based on that ViewState.

Then, comparing the state between the HTTP Request and the current Control state (restored by ViewState) we will start the appropriate event. Since the Form is loaded similar to the first Request case, first, load event if the value entered into the textbox is different from ViewState and if TextChanged event, button is clicked, then we feel like Clicked event.

Of course, with load event handlers where both the first Request and the Postback (Page_Load() method) are called unconditionally, use the IsPostback property to indicate whether there is a Postback or not, to replace the processing content. Normally, the Page_Load() method will be as follows:

protected void Page_Load(object sender, EventArgs e)

{ // Use IsPostback property to determine if there is a Postback or not? if (!Page.IsPostback) {

// here will describe the initialization process.

}

// Here will describe the common handling in all events.

}

The following Requests are similar to the first Request. The Control will generate HTML and ViewState will be generated to hold the state.

- AutoPostback property of some Web Server Controls

When changing some values: background color, foreground color, font name, font size, border style, default picture or greeting text, if the AutoPostback property of the corresponding Web Server Controls: <asp:TextBox>, <asp:dropdownlist>,

<asp:radiobuttonlist>, <asp:checkbox> on the page set value = true, we will see the card editing will be updated immediately after the change.

A Web Server Control has the AutoPostback=true attribute, which means that the Control will Postback to the Server every time the user interacts with that Control (this attribute supports some ASP.NET Controls such as CheckBox, TextBox, ListControl because by default they do not automatically Postback to the Server. With other ASP.NET Controls such as Button, they will automatically Postback to the Server every time the user interacts with them). Therefore, it is necessary to set this attribute = true for the chkAll checkbox as the code below, so that the Server can receive the signal from this Control after the user interacts.

<asp:CheckBox ID="chkAll" runat="Server" OnCheckedChanged="chkAll_CheckedChanged" AutoPostback="true" />

3.1.7. Validation Controls

When building an application, it is necessary to check the data entered by the user to limit the errors in the data entered to ensure that the data processing is performed accurately according to the business requirements. If writing code to check, it will take a lot of time (using JavaScript or VBScript). Web Form supports Validation Controls to check the data entered by the user in the Controls server, the purpose is to avoid users entering wrong information or not leaving important required information blank, ...

With the previous version of ASP.NET, ASP, to fix that error, we had to write JavaScript code to catch that error, but with ASP.NET, we have provided controls to check the validity of input controls on the Form. In this section, we will learn about those controls and then learn how to extend those controls as desired to check input on the Client side.

There are 6 Validation controls in .Net framework:

+ RequiredFieldValidator: Requires the user to enter a value into the specified field on the Form

+ RangeValidator: Checks whether the input value is within a predetermined minimum and maximum range.

+ CompareValidator: Compares whether the entered value is equal to a value of another field on the Form.

+ RegularExpressionValidator: Compares the input value with a regular expression, which can be an email, phone number, bank account number, etc.

+ CustomValidator: can customize the Validator object as you like

+ ValidationSummary: allows displaying a summary of all errors on 1 page.

Figure 3.40. Diagram of checking data entered on Form at Client and Server

When Postbacking to the Server, the Web page always checks the validity of the data (if required in the design). If the data is invalid (blank, value domain violation, incorrect password re-entered, ...), the Web page will not be able to Postback to the Server.

Common properties of validation Controls.


Properties

Describe

ControlToValidate

The name of the control to check. This is the property that must be verified.

defined when using Validation Control.

Text

The message string that appears when an error occurs.


ErrorMessage

The message string appears in the Validation Summary control. This value will be displayed in place of the control.

control if no value is assigned to the Text property.


Display

Display format rules:

- None: Do not display error message (still check data)

- Static: In the absence of data violations, the control is not visible but still occupies the position as it was designed.

- Dynamic: In the absence of a data breach,

Comment


Agree Privacy Policy *