[英]ASP.NET - How do I register multiple JavaScript calls to run on PostBack?
I have an ASP.NET page with two instances of the same Web User Control (a simple WYSIWYG editor). 我有一个带有相同Web用户控件的两个实例(一个简单的WYSIWYG编辑器)的ASP.NET页。 On submit, the WUCs do a little JavaScript magic and then proceed with a normal postback. 在提交时,WUC会做一点JavaScript魔术,然后继续进行正常的回发。
The first instance seems to be working, but the second fails to post changes back to the server (it reverts to the original, and posts that). 第一个实例似乎可以正常工作,但是第二个实例无法将更改发布回服务器(它还原为原始实例,然后将其发布)。 I believe the problem is that the JS only fires for the first WUC. 我认为问题在于JS只为第一个WUC触发。 I've traced that to the following code, from the generated client-side source: 我已经从生成的客户端源中将其追溯到以下代码:
function WebForm_OnSubmit() {
prepHtml('AddEditPopup1_ctlEditorQuestion_txtEdit','AddEditPopup1_ctlEditorQuestion_divEdit', 'AddEditPopup1_ctlEditorQuestion_divHT' );
//snip...
}
The problem seems to be that there should be two calls to prepHtml
: one for the ctlEditorQuestion
instance of the WUC, and one for the ctlEditorAnswer
instance. 问题似乎是应该对prepHtml
进行两次调用:一个调用WUC的ctlEditorQuestion
实例,另一个ctlEditorAnswer
实例。
Instead, there's only the one for ctlEditorQuestion
. 相反,只有ctlEditorQuestion
一个。 Both controls are registering the OnSubmit event, but one of them overwrites the other. 这两个控件都在注册OnSubmit事件,但是其中一个覆盖另一个。
The prepHtml
call is registered from the WUCs' C# code at runtime: prepHtml
调用是在运行时从WUC的C#代码中注册的:
//Page_Load
_onSubmit = String.Format("prepHtml('{0}','{1}', '{2}' );",
txtEdit.ClientID, divEdit.ClientID, divHT.ClientID);
//OnPreRender
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "get-html", _onSubmit);
I should point out that I didn't write this control myself, and I've never seen this kind of runtime registration of OnSubmit JS code before. 我应该指出,我不是自己编写此控件的,而且我之前从未见过这种OnSubmit JS代码的运行时注册。 Page.ClientScript.RegisterOnSubmitStatement
is totally new to me. Page.ClientScript.RegisterOnSubmitStatement
对我来说是全新的。
I need to register both prepHtml
calls so they run sequentially. 我需要注册两个prepHtml
调用,以便它们按顺序运行。 Is this possible? 这可能吗? I'm open to alternatives to Page.ClientScript.RegisterOnSubmitStatement
, so long as the code still gets fired on submit. 我愿意接受Page.ClientScript.RegisterOnSubmitStatement
替代Page.ClientScript.RegisterOnSubmitStatement
,只要代码在提交时仍被触发即可。
This should do what you want without tightly coupling the controls to the page. 这应该在不将控件紧密耦合到页面的情况下完成所需的操作。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string onSubmit = string.Format("prepHtml('{0}','{1}', '{2}';",
txtEdit.ClientID,
divEdit.ClientID,
divHT.ClientID);
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(),
this.Id + "_getHtml", onSubmit);
}
}
The key (as I mentioned in my comment) is the unique name. 密钥(正如我在评论中提到的)是唯一名称。 Notice that I use "this.ID" in the script name. 请注意,我在脚本名称中使用了“ this.ID”。 The ID property is guaranteed to be unique within the page, so it would be a good candidate. 保证ID属性在页面内是唯一的,因此它是一个很好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.