无法从代码后面调用JS函数

时间:2013-11-03 00:59:00

标签: c# javascript html webforms devexpress

我正在尝试从“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();
 }
 }

如何在添加行并且“添加新对话框”消失后成功执行此功能(客户端或服务器端)?

2 个答案:

答案 0 :(得分:3)

您需要在EndCallback处理程序中执行js代码。我回答了类似的问题herehere

答案 1 :(得分:0)

ScriptManager.RegisterClientScriptBlock(this,this.GetType(),“BrandModelSearch”,ShowBrandModelSearch(),false);

相关问题