Last active
December 6, 2015 13:36
-
-
Save kakari2/47d1216ce8f0222c78ff to your computer and use it in GitHub Desktop.
ASP.NET_Validate&Message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Student.aspx.vb" Inherits="FileUpload.Student" %> | |
| <!DOCTYPE html> | |
| <html xmlns="http://www.w3.org/1999/xhtml"> | |
| <head runat="server"> | |
| <script type="text/javascript"> | |
| function OnClientValidation() { | |
| document.getElementById('Message').style.display = 'none'; | |
| } | |
| function EnabledButton() { | |
| document.getElementById("Button_Insert").style.display = 'none'; | |
| document.getElementById("dummy1").style.display = 'inline'; | |
| setTimeout("EnableButton()", 2000) | |
| return true; | |
| } | |
| function EnableButton() { | |
| document.getElementById("Button_Insert").style.display = 'inline'; | |
| document.getElementById("dummy1").style.display = 'none'; | |
| return true; | |
| } | |
| </script> | |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
| <title></title> | |
| </head> | |
| <body> | |
| <form id="form1" runat="server"> | |
| <asp:ScriptManager ID="ScriptManager_Student" runat="server"></asp:ScriptManager> | |
| <asp:UpdatePanel ID="UpdatePanel_Student" runat="server"> | |
| <ContentTemplate> | |
| <asp:ValidationSummary ID="ValidationSummary_Student" runat="server" /> | |
| <div runat="server" id="Message"> | |
| <asp:Label ID="Label_Message" runat="server" Text="Label"></asp:Label> | |
| </div> | |
| <p> | |
| <span>生徒管理</span> | |
| </p> | |
| <p> | |
| <asp:HyperLink ID="HyperLink_Top" runat="server" NavigateUrl="~/StudentTop.aspx">トップに戻る</asp:HyperLink> | |
| </p> | |
| <p> | |
| <span>a</span> | |
| </p> | |
| <p> | |
| <span>b</span> | |
| </p> | |
| <p> | |
| <span>c</span> | |
| </p> | |
| <div> | |
| <table> | |
| <tr> | |
| <td> | |
| <span>都道府県</span> | |
| </td> | |
| <td> | |
| | |
| </td> | |
| <td> | |
| <asp:DropDownList runat="server" ID="DropDown_Prefectures" AutoPostBack="true"></asp:DropDownList> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td> | |
| <span>市</span> | |
| </td> | |
| <td> | |
| | |
| </td> | |
| <td> | |
| <asp:DropDownList runat="server" ID="DropDown_City" AutoPostBack="true"></asp:DropDownList> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td> | |
| <span>学校</span> | |
| </td> | |
| <td> | |
| | |
| </td> | |
| <td> | |
| <asp:DropDownList runat="server" ID="DropDown_School" AutoPostBack="true"></asp:DropDownList> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td> | |
| <span>名前</span> | |
| </td> | |
| <td> | |
| | |
| </td> | |
| <td> | |
| <asp:TextBox ID="TextBox_Name" runat="server"></asp:TextBox> | |
| <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox_Name" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator> | |
| </td> | |
| </tr> | |
| </table> | |
| </div> | |
| <div> | |
| <table> | |
| <tr> | |
| <td> | |
| <span>削除理由</span> | |
| </td> | |
| <td> | |
| | |
| </td> | |
| <td> | |
| <asp:TextBox ID="TextBox_Delete" runat="server"></asp:TextBox> | |
| <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox_Delete" ErrorMessage="削除理由を入力してください">*</asp:CustomValidator> | |
| </td> | |
| </tr> | |
| </table> | |
| <p> | |
| <span></span> | |
| </p> | |
| </div> | |
| <asp:Button ID="Button_Insert" runat="server" Text="登録" OnClientClick="EnabledButton();" /> | |
| <asp:button ID="dummy1" runat="server" Text="登録" style="DISPLAY:none" Enabled="False"></asp:button> | |
| | |
| <asp:Button ID="Button_Update" runat="server" Text="更新" /> | |
| <asp:button ID="dummy2" runat="server" Text="更新" style="DISPLAY:none" Enabled="False"></asp:button> | |
| | |
| <asp:Button ID="Button_Delete" runat="server" Text="削除" ValidationGroup="Delete" OnClientClick="return confirm('a');"/> | |
| <asp:button ID="dummy3" runat="server" Text="削除" style="DISPLAY:none" Enabled="False"></asp:button> | |
| </ContentTemplate> | |
| </asp:UpdatePanel> | |
| </form> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Imports System.Data.SqlClient | |
| Public Class Student | |
| Inherits System.Web.UI.Page | |
| Private StudentBC As BC.StudentBC | |
| Public Sub New() | |
| 'StudentBC = New BC.StudentBC(Session("UserID"), Session("UserName")) | |
| StudentBC = New BC.StudentBC | |
| End Sub | |
| Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load | |
| StudentBC.UserID = Session("UserID") | |
| StudentBC.UserName = Session("UserName") | |
| Dim csname As String = "OnSubmitScript" | |
| Dim cstype As Type = Me.GetType() | |
| Dim cs As ClientScriptManager = Page.ClientScript | |
| If (Not cs.IsOnSubmitStatementRegistered(cstype, csname)) Then | |
| Dim cstext As String = "OnClientValidation();" | |
| cs.RegisterOnSubmitStatement(cstype, csname, cstext) | |
| End If | |
| Message.Visible = False | |
| If Not Page.IsPostBack Then | |
| 'MsgBox(Request.QueryString("ID")) | |
| Dim dr As DataRow | |
| Dim prefecture_num As Integer = 0 | |
| If Request.QueryString("ID") IsNot Nothing Then | |
| ViewState.Add("StudentID", Request.QueryString("ID")) | |
| Dim Students As DataTable = getStudent(Request.QueryString("ID")) | |
| dr = Students.Rows(0) | |
| 'TODO:選択した生徒の情報を表示する | |
| TextBox_Name.Text = dr("Student_Name") | |
| prefecture_num = dr("Prefecture_ID") | |
| If Session("Message") IsNot Nothing Then | |
| Label_Message.Text = Session("Message").ToString | |
| Message.Visible = True | |
| Session.Remove("Message") | |
| End If | |
| End If | |
| DropDown_Prefectures.DataSource = getPrefectures() | |
| DropDown_Prefectures.DataValueField = "Prefecture_ID" | |
| DropDown_Prefectures.DataTextField = "Prefecture" | |
| DropDown_Prefectures.DataBind() | |
| DropDown_Prefectures.SelectedValue = prefecture_num | |
| DropDown_City.DataSource = getCities(DropDown_Prefectures.SelectedValue) | |
| DropDown_City.DataValueField = "City_ID" | |
| DropDown_City.DataTextField = "City" | |
| DropDown_City.DataBind() | |
| DropDown_School.DataSource = getSchools(DropDown_City.SelectedValue) | |
| DropDown_School.DataValueField = "School_ID" | |
| DropDown_School.DataTextField = "School" | |
| DropDown_School.DataBind() | |
| 'DropDown_Prefectures.SelectedIndex = 0 | |
| 'DropDown_City.SelectedIndex = 0 | |
| 'DropDown_School.SelectedIndex = 0 | |
| Button_Insert.Enabled = True | |
| Button_Update.Enabled = True | |
| Button_Delete.Enabled = True | |
| End If | |
| End Sub | |
| Private Sub DropDown_Prefectures_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDown_Prefectures.SelectedIndexChanged | |
| DropDown_City.DataSource = getCities(DropDown_Prefectures.SelectedValue) | |
| DropDown_City.DataBind() | |
| DropDown_City.SelectedIndex = 0 | |
| DropDown_School.DataSource = getSchools(DropDown_City.SelectedValue) | |
| DropDown_School.DataBind() | |
| End Sub | |
| Private Sub DropDown_City_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDown_City.SelectedIndexChanged | |
| DropDown_School.DataSource = getSchools(DropDown_City.SelectedValue) | |
| DropDown_School.DataBind() | |
| End Sub | |
| Private Sub DropDown_School_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDown_School.SelectedIndexChanged | |
| End Sub | |
| Private Function getStudent(ByVal student_id As String) As DataTable | |
| Dim sc As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("FileUploadConnectionString").ConnectionString) | |
| Dim sql As New SqlCommand | |
| sql.CommandText = "select" & | |
| " A.Student_ID" & | |
| ", A.Prefecture_ID" & | |
| ", A.City_ID" & | |
| ", A.School_ID" & | |
| ", B.Prefecture" & | |
| ", C.City" & | |
| ", D.School" & | |
| ", A.Student_Name" & | |
| " from r_Student As A" & | |
| " left join m_Prefectures As B on A.Prefecture_ID = B.Prefecture_ID" & | |
| " left join m_City As C on A.City_ID = C.City_ID" & | |
| " left join m_School As D on A.School_ID = D.School_ID" & | |
| " where A.Student_ID = " & student_id & | |
| " order by Student_ID" | |
| Dim cmd As SqlCommand = New SqlCommand(sql.CommandText, sc) | |
| cmd.Connection.Open() | |
| Dim datatable As New DataTable | |
| datatable.Load(cmd.ExecuteReader()) | |
| cmd.Connection.Close() | |
| Return datatable | |
| End Function | |
| Private Function getPrefectures() As DataTable | |
| Dim sc As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("FileUploadConnectionString").ConnectionString) | |
| Dim sql As New SqlCommand | |
| sql.CommandText = "select" & | |
| " Prefecture_ID" & | |
| ", Prefecture" & | |
| " from m_Prefectures" & | |
| " order by Prefecture_ID" | |
| Dim cmd As SqlCommand = New SqlCommand(sql.CommandText, sc) | |
| cmd.Connection.Open() | |
| Dim datatable As New DataTable | |
| datatable.Load(cmd.ExecuteReader()) | |
| cmd.Connection.Close() | |
| Return datatable | |
| End Function | |
| Private Function getCities(ByVal prefecture_id As Integer) As DataTable | |
| Dim sc As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("FileUploadConnectionString").ConnectionString) | |
| Dim sql As New SqlCommand | |
| sql.CommandText = "select" & | |
| " City_ID" & | |
| ", City" & | |
| " from m_City" & | |
| " where Prefecture_ID = " & prefecture_id & | |
| " order by City_ID" | |
| Dim cmd As SqlCommand = New SqlCommand(sql.CommandText, sc) | |
| cmd.Connection.Open() | |
| Dim datatable As New DataTable | |
| datatable.Load(cmd.ExecuteReader()) | |
| cmd.Connection.Close() | |
| Return datatable | |
| End Function | |
| Private Function getSchools(ByVal city_id As Integer) As DataTable | |
| Dim sc As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("FileUploadConnectionString").ConnectionString) | |
| Dim sql As New SqlCommand | |
| sql.CommandText = "select" & | |
| " School_ID" & | |
| ", School" & | |
| " from m_School" & | |
| " where City_ID = " & city_id & | |
| " order by School_ID" | |
| Dim cmd As SqlCommand = New SqlCommand(sql.CommandText, sc) | |
| cmd.Connection.Open() | |
| Dim datatable As New DataTable | |
| datatable.Load(cmd.ExecuteReader()) | |
| cmd.Connection.Close() | |
| Return datatable | |
| End Function | |
| Protected Sub Button_Insert_Click(sender As Object, e As EventArgs) Handles Button_Insert.Click | |
| If Not Page.IsValid Then Exit Sub | |
| Dim cnt As Integer = StudentBC.InsertStudent() | |
| ' Add new record's ID (now, this is dummy) | |
| ViewState("StudentID") = 3 | |
| Session("Message") = "DB Inserted OK" | |
| Response.Redirect("~/Student.aspx?ID=" & ViewState("StudentID").ToString) | |
| End Sub | |
| Protected Sub Button_Update_Click(sender As Object, e As EventArgs) Handles Button_Update.Click | |
| If Not Page.IsValid Then Exit Sub | |
| Session("Message") = "DB Updated OK" | |
| Response.Redirect("~/Student.aspx?ID=" & ViewState("StudentID").ToString) | |
| End Sub | |
| Protected Sub Button_Delete_Click(sender As Object, e As EventArgs) Handles Button_Delete.Click | |
| If String.IsNullOrEmpty(TextBox_Delete.Text) Then | |
| CustomValidator1.ErrorMessage = "Need reason to delete" | |
| CustomValidator1.IsValid = False | |
| End If | |
| If Not Page.IsValid Then Exit Sub | |
| Session("Message") = "Deleted OK" | |
| Response.Redirect("~/Student.aspx?ID=" & ViewState("StudentID").ToString) | |
| End Sub | |
| Protected Sub CustomValidator1_ServerValidate(source As Object, args As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate | |
| If String.IsNullOrEmpty(TextBox_Delete.Text) Then | |
| CustomValidator1.IsValid = False | |
| End If | |
| End Sub | |
| End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment