客户端JS和ASP.NET回发 [英] Client-side JS and ASP.NET Postback

查看:95
本文介绍了客户端JS和ASP.NET回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些设置表单字段的客户端JavaScript.页面回发时,这些字段将重置.

I have some client-side JavaScript that sets form fields. When the page posts back, those fields are reset.

这是为什么?回发发生时,字段值是否不会捕获并放入ViewState中?

Why is this? Aren't the field values captured and put in the ViewState when the postback occurs?

当我在Page_Load()上中断调试器时,在回发后执行任何代码之前,表单字段的值为空.

When I break the debugger on Page_Load(), before any code executes after the postback, the form field values are empty.

推荐答案

从您的客户端javascript填充的表单元素是否设置为已禁用?如果是这样,ASP.NET将忽略该值.

Are the form elements that are being populated from your client-side javascript set as disabled? If so, ASP.NET will ignore the value.

例如:

<asp:TextBox ID="TextBox1" Enabled="False" Runat="Server" />

<script type="text/javascript">
    document.forms[0].elements["TextBox1"].style.disabled = false;
    document.forms[0].elements["TextBox1"].value = "Value set from Javascript";
</script>

此代码运行时,ASP.NET认为该文本框已禁用,因此在回发中将其值丢弃,因此TextBox1.Text的值将始终为空.据我所知,这种行为适用于所有表单元素.如果ASP.NET认为它们已被禁用,并且随后被启用并在客户端填充,则该值将在回发中不可用.

When this code runs, ASP.NET thinks that the textbox is disabled, and therefore discards its value in the postback, so the value of TextBox1.Text will always be blank. As far as I know, this behavior applies to all form elements. If ASP.NET thinks they are disabled, and they are subsquently enabled and populated client-side, the value won't be available on the postback.

这篇关于客户端JS和ASP.NET回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆