■ RequiredFieldValidator 클래스 : InitialValue 속성을 사용해 기본값 설정하기

------------------------------------------------------------------------------------------------------------------------

MainPage.aspx

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="TestProject.MainPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>RequiredFieldValidator 클래스 : InitialValue 속성을 사용해 기본값 설정하기</title>

    </head>

    <body>

        <form id="form" runat="server">

            <div>

                <asp:DropDownList ID="dropDownList" runat="server" />

                <asp:Button ID="selectButton" runat="server"

                    Text="선택"

                    OnClick="selectButton_Click" />

                <hr />

                <asp:Label ID="resultLabel" runat="server" Text="" />

                <asp:RequiredFieldValidator ID="requiredFieldValidator" runat="server"

                    ControlToValidate="dropDownList"

                    InitialValue="-1"

                    Display="None"

                    ErrorMessage="목록에서 항목을 선택해 주시기 바랍니다.">

                </asp:RequiredFieldValidator>

                <asp:ValidationSummary ID="validationSummary" runat="server"

                    ShowMessageBox="true"

                    ShowSummary="false" />

            </div>

        </form>

    </body>

</html>

 

 

MainPage.aspx.cs

 

 

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace TestProject

{

    /// <summary>

    /// 메인 페이지

    /// </summary>

    public partial class MainPage : Page

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 페이지 로드시 처리하기 - Page_Load(sender, e)

 

        /// <summary>

        /// 페이지 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        protected void Page_Load(object sender, EventArgs e)

        {

            if(!Page.IsPostBack)

            {

                this.dropDownList.Items.Add(new ListItem("[미정]", "-1"));

 

                for(int i = 0; i < 10; i++)

                {

                    ListItem listItem = new ListItem(i.ToString(), i.ToString());

 

                    this.dropDownList.Items.Add(listItem);

                }

            }

        }

 

        #endregion

        #region 선택 버튼 클릭시 처리하기 - selectButton_Click(sender, e)

 

        /// <summary>

        /// 선택 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        protected void selectButton_Click(object sender, EventArgs e)

        {

            this.resultLabel.Text = this.dropDownList.SelectedValue;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요