Cannot call a JS function from code behind

2019-09-09 20:00发布

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.

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.

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>

Here is the JS function:

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

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?

2条回答
Melony?
2楼-- · 2019-09-09 20:35

You need to execute your js code in EndCallback handler. I answered similar questions here and here.

查看更多
等我变得足够好
3楼-- · 2019-09-09 20:47

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

查看更多
登录 后发表回答