不能从后面的代码调用JS函数 [英] Cannot call a JS function from code behind
问题描述
我正在尝试从DisplayBrandGridView_RowInserting事件的后面的代码中提交一个JS函数。当RegisterStratupScipt运行时,没有任何事情发生,JS函数甚至没有命中。
I am trying to submit a JS function from the code behind of a "DisplayBrandGridView_RowInserting" event. When the RegisterStratupScipt runs, nothing happens and the JS function is not even hit.
将记录插入数据库(在此事件中成功)之前,我需要立即执行JS函数,该函数将显示一个按钮,将数据添加到另一个表。请注意,从客户端执行时,该JS函数工作正常。我不在乎它是从客户端还是服务器执行。在这些条件下,我可以想到执行它的唯一方法是从服务器端。
After a record is inserted into the DB (which is successful from within this event), I need to immediately execute the JS function which will show a button to add data to another table. Note that this JS function works fine when executed from the client side. I don't care whether it's executed from the client or server. The only way I can think of to execute it under these conditions is from the server side.
以下是从HTML开始新行对话框的事件:
Here is the event which starts the new row dialog from within the 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客户端事件执行的代码:
Here is the code behind which gets executed from the HTML client side event above:
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();
}
}
如何成功执行此功能(客户端或服务器一边添加一行,添加新对话框就会消失?
How can I successfully execute this function (client or server side) after a row has been added and the "add new dialog" goes away?
推荐答案
你需要执行你的js代码在 EndCallback 处理程序。我回答了类似的问题这里和此处。
You need to execute your js code in EndCallback handler. I answered similar questions here and here.
这篇关于不能从后面的代码调用JS函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!