简体   繁体   English

ASP.Net中的JavaScript

[英]JavaScript in ASP.Net CodeBehind

I am having trouble with some javascript that I have added to the codebehind. 我在添加到背后代码中的某些JavaScript时遇到了麻烦。 The goal I am trying to achieve here is on the page load to do another postback. 我要在此处实现的目标是在页面加载上执行另一次回发。 Now, you may find this odd, but there is method to my madness. 现在,您可能会发现这很奇怪,但是有一种使我疯狂的方法。

In my ASP.Net Wizard, I have a textbox that contains a date populated from another step. 在我的ASP.Net向导中,我有一个文本框,其中包含从另一步骤填充的日期。 This date is then used to populate 3 other controls with financial information. 然后使用该日期用财务信息填充其他3个控件。 It is necessary for these 3 other controls to be populated on the load of this step. 这3个其他控件必须根据此步骤的负载进行填充。 Now I have tried to do simply on page_load , but this doesn't work as certain controls either don't exist or the date isn't in the textbox. 现在,我尝试仅在page_load上执行此操作,但是由于某些控件不存在或日期不在文本框中,因此此操作不起作用。 I have also tried to do this on the page render method, but this didn't work either for the same reasons. 我也尝试过在page render方法上执行此操作,但是由于相同的原因,此操作也不起作用。

So, I have resorted to using javascript executing a double postback, but it is causing all sorts of problems. 因此,我不得不使用javascript执行两次回发,但这会引起各种问题。

Here is the code from the Page_Load : 这是Page_Load的代码:

 Dim validateFinancial as String = "<script language='javascript'>window.onload = function() ( ValidateFinancialDate() { __doPostBack('<%= UpdatePanel2.ClientID %>'); return false; })</script>"
 Page.ClientScript.RegisterStartUp(Me.GetType(), "MyScript", validateFinancial, false)

It is not firing and in the javascript error box you see in the bottom left hand corner of the browser it says missing ";". 它没有触发,在浏览器的左下角的javascript错误框中,它显示缺少“;”。 If I remove the javascript code and simply added it to the markup with the function name in the string it will work with errors, but this when posting to the webserver causes the AJAX controls to fail on the whole page. 如果我删除了javascript代码并将其简单地添加到带有字符串中函数名称的标记中,它将正常工作,但是在发布到网络服务器时,这会导致AJAX控件在整个页面上失败。

Is there away of getting this to work, please? 请问有没有让它起作用的方法?

Your problem is that 你的问题是

<%= UpdatePanel2.ClientID %>

will not be interpreted. 将不会被解释。 at the time when you want your javascript to post back, ASP has already finished rendering your page. 在您希望Javascript回发时,ASP已经完成了页面渲染。 You're goin to have to find another way of passing your UpdatePanel2.ClientID value. 您必须寻找另一种传递UpdatePanel2.ClientID值的方法。

You shouldn't use <%= UpdatePanel2.ClientID %> on backend side. 您不应在后端使用<%= UpdatePanel2.ClientID %> Get __doPostBack('<%= UpdatePanel2.ClientID %>') equivalent using ClientScript.GetPostBackEventReference(UpdatePanel2, string.Empty); 使用ClientScript.GetPostBackEventReference(UpdatePanel2, string.Empty);等效的__doPostBack('<%= UpdatePanel2.ClientID %>') ClientScript.GetPostBackEventReference(UpdatePanel2, string.Empty); replace your __doPostBack('<%= UpdatePanel2.ClientID %>') with return value of mentioned method. 将您的__doPostBack('<%= UpdatePanel2.ClientID %>')替换为上述方法的返回值。


More detailed: 更详细:

Let's start from the beginning. 让我们从头开始。 You need postback to be initiated by UpdatePanel2 after page loaded on client. 在客户端上加载页面后,需要由UpdatePanel2启动回发。 The correct JS function pattern should be: 正确的JS功能模式应为:

window.onload = function() { ValidateFinancialDate(); %Do postback%; return false;};

To obtain that %Do postback% function call for UpdatePanel2 we need to use ClientScript.GetPostBackEventReference(UpdatePanel2, string.Empty) backend method, which will produce correct JS __doPostBack function call to make postback request initiated by UpdatePanel2 control. 若要获取对UpdatePanel2的%Do postback%函数调用,我们需要使用ClientScript.GetPostBackEventReference(UpdatePanel2, string.Empty)后端方法,该方法将生成正确的JS __doPostBack函数调用,以使UpdatePanel2控件发起的回发请求。

So working example on C# will be as following: 因此,在C#上的工作示例如下:

string postbackReference = Page.ClientScript.GetPostBackEventReference(UpdatePanel2, string.Empty);
string validateFinancial = "window.onload = function() { ValidateFinancialDate(); " + postbackReference + "; return false;};";
Page.ClientScript.RegisterStartUp(this.GetType(), "MyScript", validateFinancial, true);

Please pay attention to true argument of RegisterStartUp method, this will wrap validateFinancial script contents into tags automatically. 请注意RegisterStartUp方法的true参数,这将把validateFinancial脚本内容自动包装到标签中。

But this workaround with double postback seems artificial for the problem you're trying to solve, if you provide more source code we can find better solution. 但是对于您要解决的问题,这种具有双重回发的解决方法似乎是人为的,如果您提供更多的源代码,我们可以找到更好的解决方案。

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

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