简体   繁体   English

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

[英]Cannot call a JS function from code behind

I am trying to submit a JS function from the code behind of a "DisplayBrandGridView_RowInserting" event. 我正在尝试从“ DisplayBrandGridView_RowInserting”事件背后的代码提交JS函数。 When the RegisterStratupScipt runs, nothing happens and the JS function is not even hit. 当RegisterStratupScipt运行时,什么也没有发生,并且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. 将记录插入数据库后(在此事件中成功完成),我需要立即执行JS函数,该函数将显示一个向其他表添加数据的按钮。 Note that this JS function works fine when executed from the client side. 请注意,当从客户端执行此JS函数时,它可以正常工作。 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. 我想到的在这种情况下执行它的唯一方法是从服务器端执行。

Here is the event which starts the new row dialog from within the HTML: 这是从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>

Here is the JS function: 这是JS函数:

function ShowBrandModelSearch()
 {
 var associateBrand = eval( '<%# BrandModelSearch.ClientInstanceName %>' );
 associateBrand.DoClick();
 }

Here is the code behind which gets executed from the HTML client side event above: 这是从上面的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();
 }
 }

How can I successfully execute this function (client or server side) after a row has been added and the "add new dialog" goes away? 添加一行并且“添加新对话框”消失后,如何成功执行此功能(客户端或服务器端)?

You need to execute your js code in EndCallback handler. 您需要在EndCallback处理程序中执行js代码。 I answered similar questions here and here . 在这里这里都回答了类似的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM