[英]Cannot call a JS function from code behind
我正在嘗試從“ DisplayBrandGridView_RowInserting”事件背后的代碼提交JS函數。 當RegisterStratupScipt運行時,什么也沒有發生,並且JS函數甚至沒有命中。
將記錄插入數據庫后(在此事件中成功完成),我需要立即執行JS函數,該函數將顯示一個向其他表添加數據的按鈕。 請注意,當從客戶端執行此JS函數時,它可以正常工作。 我不在乎它是從客戶端還是服務器執行的。 我想到的在這種情況下執行它的唯一方法是從服務器端執行。
這是從HTML內部啟動新行對話框的事件:
<dx:ASPxButton ID="btnAddNew" Text="Add New" runat="server" CausesValidation="false" AutoPostBack="false" Theme="PlasticBlue" ClientInstanceName="btnAddNew">
<ClientSideEvents Click="function (s,e) { DisplayBrandsClientGridView.AddNewRow(); }" />
</dx:ASPxButton>
這是JS函數:
function ShowBrandModelSearch()
{
var associateBrand = eval( '<%# BrandModelSearch.ClientInstanceName %>' );
associateBrand.DoClick();
}
這是從上面的HTML客戶端事件執行的背后代碼:
protected void DisplayBrandGridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
var grid = sender as ASPxGridView;
try
{
grid.JSProperties["cpDesc"] = false;
var description = e.NewValues["Description"].ToString().Trim();
using (var dcWeb = DataContextExtension.FromConfig<DCMerchant>())
{
if (dcWeb.Merch_DisplayBrands.Any(a => a.Description == description))
{
// Display Brand already exists
grid.JSProperties["cpDesc"] = true;
grid.JSProperties["cpConfirmationMessageHeader"] = "Display Brand Exists";
grid.JSProperties["cpConfirmationMessage"] = string.Format("Display Brand {0} already exists. Please specify a unique name.", description);
}
else
{
try
{
var dBrand = new Merch_DisplayBrand()
{
Description = description,
IsActive = Utility.GetValue<bool>(e.NewValues["IsActive"]),
ModifiedBy = CurrentUser.LawsonId,
ModifiedOn = DateTime.Now
};
dcWeb.Merch_DisplayBrands.InsertOnSubmit(dBrand);
dcWeb.SubmitChanges(ConflictMode.ContinueOnConflict);
// Needed to keep track of filter
if (string.IsNullOrWhiteSpace(Utility.GetValue<string>(PageData["FilterText"])))
{
grid.JSProperties["cpFilterText"] = description;
PageData["FilterText"] = description;
SearchTextASPxTextBox.Text = description;
}
else
grid.JSProperties["cpFilterText"] = PageData["FilterText"];
Page.ClientScript.RegisterStartupScript(this.GetType(), "BrandModelSearch", "ShowBrandModelSearch();", true);
}
catch (Exception ex)
{
if (!ex.ResolveConflicts(dcWeb))
{
ex.AddTruncatedFieldInfo(dcWeb);
throw;
}
}
}
}
}
catch (Exception ex)
{
ex.Log();
}
finally
{
e.Cancel = true;
grid.CancelEdit();
}
}
添加一行並且“添加新對話框”消失后,如何成功執行此功能(客戶端或服務器端)?
您需要在EndCallback處理程序中執行js代碼。 我在這里和這里都回答了類似的問題。
ScriptManager.RegisterClientScriptBlock(this,this.GetType(),“ BrandModelSearch”,ShowBrandModelSearch(),false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.