グリッドの編集モードでは、ユーザーが選択できるドロップダウン リストがあります。
新しいレコードの場合、デフォルトのメッセージ「選択してください」を選択します既存のレコードの場合、データベースからの値をデータバインドして、選択された値にします
ユーザーがドロップダウンリストを選択したことを確認するために、requiredvalidator を追加しました。ユーザーが新しいレコードを作成するときに機能し、ユーザーが選択しないとエラーメッセージが表示されます。
ドロップダウンリストで値が選択されていても、ユーザーがレコードを編集するとエラーが発生しました。それでもエラーメッセージが表示されます
<%@ Page Title="" Language="VB" MasterPageFile="~/LRDB.master" AutoEventWireup="false" CodeFile="Admin_dictionary_BU.aspx.vb" Inherits="Admin_Admin_dictionary_BU" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<br />
<strong><span class="style4">Business Unit</span><br /> </strong><br />
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function RowDblClick(sender, eventArgs) {
sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
}
function conformbox() {
var con = confirm("Are you sure want to delete?");
if (con == true) {
return true;
}
else {
return false;
}
}
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
</telerik:RadAjaxLoadingPanel>
<telerik:RadGrid ID="RadGrid_BU" runat="server" CellSpacing="0"
DataSourceID="LDS_BU" width="1000" PageSize="15"
GridLines="None" style="margin-top: 0px" AllowFilteringByColumn="True"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
ShowStatusBar="True" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
AllowAutomaticUpdates="True">
<GroupingSettings CaseSensitive="false" />
<MasterTableView AutoGenerateColumns="False"
DataKeyNames="BU_ID" DataSourceID="LDS_BU"
AllowFilteringByColumn="False" AllowPaging="False"
CommandItemDisplay="Top" OverrideDataSourceControlSorting="true" >
<CommandItemSettings ExportToPdfText="Export to PDF" />
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"
Visible="True">
</RowIndicatorColumn>
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"
Visible="True">
</ExpandCollapseColumn>
<Columns>
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
</telerik:GridEditCommandColumn>
<telerik:GridBoundColumn DataField="BU_ID"
FilterControlAltText="Filter BU_ID column" HeaderText="BU_ID"
SortExpression="BU_ID" UniqueName="BU_ID" Visible="false">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="BU_Name"
FilterControlAltText="Filter BU_Name column" HeaderText="BU Name"
SortExpression="BU_Name" UniqueName="BU_Name">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="shortname"
FilterControlAltText="Filter shortname column" HeaderText="Shortname"
SortExpression="shortname" UniqueName="shortname">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="bu_addr"
FilterControlAltText="Filter bu_addr column" HeaderText="Address"
SortExpression="bu_addr" UniqueName="bu_addr">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="BU_Tel"
FilterControlAltText="Filter BU_Tel column" HeaderText="Telephone"
SortExpression="BU_Tel" UniqueName="BU_Tel">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="BU_TelPre"
FilterControlAltText="Filter BU_TelPre column" HeaderText="Telephone Prefix"
SortExpression="BU_TelPre" UniqueName="BU_TelPre">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="BU_Fax"
FilterControlAltText="Filter BU_Fax column" HeaderText="Fax No."
SortExpression="BU_Fax" UniqueName="BU_Fax">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="BU_FaxPre"
FilterControlAltText="Filter BU_FaxPre column" HeaderText="Fax No. Prefix"
SortExpression="BU_FaxPre" UniqueName="BU_FaxPre">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="BU_SpeedDial"
FilterControlAltText="Filter BU_SpeedDial column" HeaderText="SpeedDial"
SortExpression="BU_SpeedDial" UniqueName="BU_SpeedDial">
</telerik:GridBoundColumn>
</Columns>
<EditFormSettings EditFormType="Template">
<EditColumn FilterControlAltText="Filter EditCommandColumn1 column"
UniqueName="EditCommandColumn1">
</EditColumn>
<FormTemplate>
<table id="Table2" cellspacing="2" cellpadding="1" width="50%" border="1" rules="none"
style="border-collapse: collapse;">
<tr class="EditFormHeader">
<td style="width:150px">
<asp:Label ID="Label5" Text="Company" runat="server"></asp:Label>
</td>
<td>
<telerik:RadDropDownList ID="rdl_company" runat="server" DataSourceID="LDS_Company"
DataValueField="CompID" DataTextField="CompanyName" DefaultMessage="Please select..." >
</telerik:RadDropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="rdl_company"
ErrorMessage="Please select a Company." ></asp:RequiredFieldValidator>
</td>
</tr>
<tr >
<td >
<asp:Label ID="lb" Text="BU Name" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="tb_BU_name" runat="server" Text='<%# Bind("BU_Name") %>' TabIndex="2"></asp:TextBox>
</td>
</tr>
<tr >
<td >
<asp:Label ID="Label1" Text="ShortName" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="tb_shortname" runat="server" Text='<%# Bind("shortname") %>' TabIndex="2"></asp:TextBox>
</td>
</tr>
<tr >
<td >
<asp:Label ID="Label2" Text="Address" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="tb_BU_addr" runat="server" Text='<%# Bind("BU_addr") %>' TabIndex="2" Width="300" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr >
<td >
<asp:Label ID="Label3" Text="Telephone" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="tb_tel" runat="server" Text='<%# Bind("bu_tel") %>' TabIndex="2"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Display="Dynamic"
ErrorMessage="Please, enter valid phone number." ValidationExpression="\d+" ControlToValidate="tb_tel">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr >
<td >
<asp:Label ID="Label4" Text="Telephone Prefix" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="tb_telpre" runat="server" Text='<%# Bind("bu_telpre") %>' TabIndex="2"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" Display="Dynamic"
ErrorMessage="Please, enter valid phone number." ValidationExpression="\d+" ControlToValidate="tb_telpre">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr >
<td >
<asp:Label ID="Label6" Text="Fax No." runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="tb_fax" runat="server" Text='<%# Bind("bu_fax") %>' TabIndex="2"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" Display="Dynamic"
ErrorMessage="Please, enter valid phone number." ValidationExpression="\d+" ControlToValidate="tb_fax">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr >
<td >
<asp:Label ID="Label7" Text="Fax No. Prefix" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="tb_faxpre" runat="server" Text='<%# Bind("bu_faxpre") %>' TabIndex="2"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" Display="Dynamic"
ErrorMessage="Please, enter valid phone number." ValidationExpression="\d+" ControlToValidate="tb_faxpre">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr >
<td >
<asp:Label ID="Label8" Text="SpeedDial" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="tb_speeddial" runat="server" Text='<%# Bind("bu_speeddial") %>' TabIndex="2"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" Display="Dynamic"
ErrorMessage="Please, enter valid phone number." ValidationExpression="\d+" ControlToValidate="tb_speeddial">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>'
runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>' >
</asp:Button>
<asp:Button ID="btnDelete" Text="Delete" runat="server" CausesValidation="False"
CommandName="Delete" OnClientClick="return conformbox();"></asp:Button>
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
CommandName="Cancel"></asp:Button>
</td>
</tr>
</table>
</FormTemplate>
</EditFormSettings>
<PagerStyle PageSizeControlType="RadComboBox" />
</MasterTableView>
<PagerStyle PageSizeControlType="RadComboBox" />
<FilterMenu EnableImageSprites="False">
</FilterMenu>
</telerik:RadGrid>
<asp:LinqDataSource ID="LDS_Company"
runat="server" ContextTypeName="dcLRDBDataContext" EntityTypeName=""
TableName="db_Companies">
</asp:LinqDataSource>
<asp:LinqDataSource ID="LDS_BU"
runat="server" ContextTypeName="dcLRDBDataContext" EntityTypeName=""
TableName="db_Business_units">
</asp:LinqDataSource>
</asp:Content>
コードビハインド:
Imports System
Imports System.IO
Imports Telerik.Web.UI
Imports System.Web.UI
Imports Telerik.Web.UI.AsyncUpload
Imports System.Runtime.Serialization.Json
Imports System.Web.Services
Imports System.Runtime.Serialization
Imports System.Collections.Generic
Imports System.Drawing
Partial Class Admin_Admin_dictionary_BU
Inherits System.Web.UI.Page
Protected Sub RadGrid_BU_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid_BU.ItemCommand
RadGrid_BU.MasterTableView.ClearEditItems()
If e.CommandName = RadGrid.InitInsertCommandName Then 'Click Add New Record button
Dim editColumn As GridEditCommandColumn = CType(RadGrid_BU.MasterTableView.GetColumn("EditCommandColumn"), GridEditCommandColumn)
editColumn.Visible = False
ElseIf (e.CommandName = RadGrid.RebindGridCommandName AndAlso e.Item.OwnerTableView.IsItemInserted) Then
e.Canceled = True
Else
Dim editColumn As GridEditCommandColumn = CType(RadGrid_BU.MasterTableView.GetColumn("EditCommandColumn"), GridEditCommandColumn)
If Not editColumn.Visible Then 'Click Cancel button (both Insert and Update mode)
editColumn.Visible = True
End If
Dim ddlComp_id As RadDropDownList = DirectCast(e.Item.FindControl("rdl_company"), RadDropDownList)
Dim txt_bu_name As TextBox = DirectCast(e.Item.FindControl("tb_bu_name"), TextBox)
Dim txt_shortname As TextBox = DirectCast(e.Item.FindControl("tb_shortname"), TextBox)
Dim txt_address As TextBox = DirectCast(e.Item.FindControl("tb_bu_addr"), TextBox)
Dim txt_tel As TextBox = DirectCast(e.Item.FindControl("tb_tel"), TextBox)
Dim txt_telpre As TextBox = DirectCast(e.Item.FindControl("tb_telpre"), TextBox)
Dim txt_fax As TextBox = DirectCast(e.Item.FindControl("tb_fax"), TextBox)
Dim txt_faxpre As TextBox = DirectCast(e.Item.FindControl("tb_faxpre"), TextBox)
Dim txt_speeddial As TextBox = DirectCast(e.Item.FindControl("tb_speeddial"), TextBox)
Dim dc As New dcLRDBDataContext
If e.CommandName = "PerformInsert" Then
Dim newRecord1 As New db_Business_unit
dc.db_Business_units.InsertOnSubmit(newRecord1)
newRecord1.CompID = ddlComp_id.SelectedValue
newRecord1.BU_Name = txt_bu_name.Text
newRecord1.LastUpdateDate = DateTime.Now
newRecord1.LastUpdateBy = HttpContext.Current.Session("UserID")
dc.SubmitChanges()
RadGrid_BU.MasterTableView.ClearEditItems()
RadGrid_BU.DataBind()
ElseIf e.CommandName = "Update" Then
Dim lid As Integer = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("BU_ID")
Dim rec = (From p In dc.db_Business_units Where p.BU_ID = lid).FirstOrDefault
If Not rec Is Nothing Then
rec.CompID = ddlComp_id.SelectedValue
rec.BU_Name = txt_bu_name.Text
rec.shortname = txt_shortname.Text
rec.BU_addr = txt_address.Text
rec.BU_Tel = txt_tel.Text
rec.BU_TelPre = txt_telpre.Text
rec.BU_Fax = txt_fax.Text
rec.BU_FaxPre = txt_faxpre.Text
rec.BU_SpeedDial = txt_speeddial.Text
rec.LastUpdateBy = HttpContext.Current.Session("UserID")
rec.LastUpdateDate = DateTime.Now
dc.SubmitChanges()
End If
RadGrid_BU.MasterTableView.ClearEditItems()
RadGrid_BU.DataBind()
ElseIf e.CommandName = "Delete" Then
Dim lid As Integer = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("BU_ID")
Dim chk_rec = (From a In dc.db_Countries Where a.BU_ID = lid).Count
If chk_rec > 0 Then
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MessageBox", "alert('Cannot delete because this dictionary had been used.');", True)
Else
Dim rec2 = (From p In dc.db_Business_units Where p.BU_ID = lid).FirstOrDefault
If Not IsNothing(rec2) Then
dc.db_Business_units.DeleteOnSubmit(rec2)
dc.SubmitChanges()
End If
End If
RadGrid_BU.MasterTableView.ClearEditItems()
RadGrid_BU.DataBind()
End If
dc.Dispose()
End If
End Sub
Private Sub RadGrid_BU_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid_BU.ItemDataBound
If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
Dim item As GridEditableItem = e.Item
Dim ddl As RadDropDownList = item.FindControl("rdl_company")
ddl.SelectedValue = DataBinder.Eval(e.Item.DataItem, "CompID").ToString
End If
End Sub
End Class