Özel Sunucu Tarafı Fonksiyon Doğrulaması Ekleyin
Olası Kullanım Senaryoları
Bazen sunucu tarafında veri doğrulaması uygulamanız gerekebilir. Aspose.Cells.GridWeb, özel sunucu tarafı veri doğrulaması eklemenize olanak tanır. Hücre aralığını veya alanını belirtmeniz gerekmektedir. Ayrıca, özel sunucu tarafı doğrulaması için geri arama için istemci tarafı doğrulama işlevlerini ayarlayabilirsiniz.
Özel Sunucu Tarafı İşlev Doğrulaması Ekleme
GridValidation.ServerValidation özniteliği aracılığıyla GridCustomServerValidation arabirimini uygulayan sunucu doğrulama sınıfını ayarlamanız gerekmektedir. Ayrıca, istemci tarafı doğrulama işlevini (bu işlev istemci tarafında JavaScript dilinde yazılmalıdır), bu işlev geri aramada veriyi doğrulamak için zorunludur. İhtiyaçlarınız için GridValidation.ErrorMessage ve GridValidation.ErrorTitle özelliklerini kullanarak hata ileti dizesini ve başlığını ayarlamanız gerekmektedir. Aşağıdaki örnek kodu çalıştırdıktan sonra verilen senaryoda nasıl adım adım çalıştığını gösteren bir dizi ekran görüntüsünü inceleyebilirsiniz. Örnekte, B2:C3 hücrelerinde verileri güncelleyemezsiniz. Sayfada bu hücreleri düzenlemeye çalıştığınızda, bazı hata iletileri alacaksınız ve önceki değer geri yüklenecektir. Belirli olaylar için hücrenin ayrıntılarını/kimlik bilgilerini yazdırmak için (tarayıcınızda) Konsol penceresini açabilirsiniz.
Örnek Kod
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
----------------------------------------------- | |
AddCustomServerSideFunctionValidation.aspx | |
----------------------------------------------- | |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddCustomServerSideFunctionValidation.aspx.cs" Inherits="Aspose.Cells.GridWeb.Examples.AddCustomServerSideFunctionValidation" %> | |
<%@ Register TagPrefix="acw" Namespace="Aspose.Cells.GridWeb" Assembly="Aspose.Cells.GridWeb" %> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head runat="server"> | |
<link rel="stylesheet" href="/Scripts/jquery-ui.css" /> | |
<script src="/Scripts/jquery-2.1.1.js"></script> | |
<script src="/Scripts/jquery-ui.js"></script> | |
<script type="text/javascript"> | |
var lastselectvalue = null; | |
var localvalue = {}; | |
function myCellSelect(cell) { | |
//Get the selected cell. | |
var value = this.getCellValueByCell(cell); | |
//Get the value to store. | |
lastselectvalue = value; | |
var key = this.acttab + "_" + this.getCellRow(cell) + "_" + this.getCellColumn(cell); | |
//Store the respective cell's value. | |
localvalue[key] = lastselectvalue; | |
console.log("OnCellSelect: value:" + value + " row:" + this.getCellRow(cell) + ",col:" + this.getCellColumn(cell)); | |
console.log("Last selected value:" + lastselectvalue); | |
} | |
function ValidationErrorClientFunctionCallback(cell, msg) { | |
//Get the error message string. | |
var errmsg1 = getattr(cell, "errmsg"); | |
//Show the error message in the client dialog. | |
alert(errmsg1); | |
//Showing an alert message where "this" refers to GridWeb | |
alert(this.id + "----> " + msg + " Previous value will be restored."); | |
$("#errmsg").text(msg); | |
console.log("Selected cell:" + " row:" + this.getCellRow(cell) + ",col:" + this.getCellColumn(cell)); | |
//Get the GridWeb. | |
var who = this; | |
//Restore to valid value/previous value. | |
who.setValid(cell); | |
var key = this.acttab + "_" + this.getCellRow(cell) + "_" + this.getCellColumn(cell); | |
lastselectvalue = localvalue[key]; | |
setInnerText(cell.children[0], lastselectvalue); | |
} | |
</script> | |
<title>Add Custom Server-side Function Validation</title> | |
</head> | |
<body> | |
<form id="form1" runat="server"> | |
<div> | |
<div> | |
<b>GridWeb Version: </b> | |
<asp:Label ID="lblVersion" runat="server" Text="Label"></asp:Label> | |
<br /> | |
<acw:GridWeb ID="GridWeb1" runat="server" OnCellSelectedClientFunction="myCellSelect" Width="44%" Height="384px" ShowLoading="true" XhtmlMode="true" | |
PresetStyle="Standard" EnableAJAX="true" EnableAsync="true" RenderHiddenRow="true" MaxColumn="15" MaxRow="21"> | |
</acw:GridWeb> | |
</div> | |
</div> | |
<span id="errmsg" style="color: red;"></span> | |
</form> | |
</body> | |
</html> | |
----------------------------------------------- | |
AddCustomServerSideFunctionValidation.aspx.cs | |
----------------------------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
using Aspose.Cells.GridWeb; | |
using Aspose.Cells.GridWeb.Data; | |
namespace Aspose.Cells.GridWeb.Examples | |
{ | |
class MyServerValidation : GridCustomServerValidation | |
{ | |
public string Validate(GridWorksheet sheet, int row, int col, string value) | |
{ | |
if ((row == 1) || (row == 2)) | |
{ | |
return "Value Not Passed!"; | |
} | |
else | |
{ | |
return string.Empty; | |
} | |
} | |
} | |
public partial class AddCustomServerSideFunctionValidation : System.Web.UI.Page | |
{ | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
if (!IsPostBack && !this.GridWeb1.IsPostBack) | |
{ | |
lblVersion.Text = GridWeb.GetVersion(); | |
//Input values to B2:C3 cells in the active worksheet. | |
GridWeb1.ActiveSheet.Cells["B2"].PutValue("This"); | |
GridWeb1.ActiveSheet.Cells["C2"].PutValue("cannot"); | |
GridWeb1.ActiveSheet.Cells["B3"].PutValue("be"); | |
GridWeb1.ActiveSheet.Cells["C3"].PutValue("changed"); | |
//Get the validations of the active sheet. | |
var gridValidationCollection = GridWeb1.ActiveSheet.Validations; | |
//Add data validation to range/area: B2:C3 | |
GridValidation gv = gridValidationCollection.Add(new GridCellArea(1, 1, 2, 2)); | |
//Set the validation type to custom server function. | |
gv.ValidationType = GridValidationType.CustomServerFunction; | |
//Set the server validation class which implements the GridCustomServerValidation interface. | |
gv.ServerValidation = new MyServerValidation(); | |
//Set the client validation function (written in JavaScript on client-side). | |
//This function is compulsory to validate the data on client end on callback. | |
gv.ClientValidationFunction = "ValidationErrorClientFunctionCallback"; | |
//Set the error message string. | |
gv.ErrorMessage = "Error found! You cannot change this value."; | |
//Set the error title. | |
gv.ErrorTitle = "Error"; | |
} | |
} | |
} | |
} |