访问一个asp:在JavaScript中HiddenField控件 [英] Access an asp:hiddenfield control in JavaScript

查看:273
本文介绍了访问一个asp:在JavaScript中HiddenField控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是访问嵌入在通过JavaScript的ASP.NET占位符控件的ASP.NET HiddenField控件的最好方法?可见属性设置为false在最初的页面加载,可以通过一个AJAX回调改变。

What is the best way to access an ASP.NET HiddenField control that is embedded in an ASP.NET PlaceHolder control through JavaScript? The Visible attribute is set to false in the initial page load and can changed via an AJAX callback.

下面是我的电流源$ C ​​$ C:

Here is my current source code:

<script language="javascript" type="text/javascript">
    function AccessMyHiddenField()
    {
        var HiddenValue = document.getElementById("<%= MyHiddenField.ClientID %>").value;

        //do my thing thing.....
    }
</script>
<asp:PlaceHolder ID="MyPlaceHolder" runat="server" Visible="false">
    <asp:HiddenField ID="MyHiddenField" runat="server" />
</asp:PlaceHolder>

编辑:如何在C#中设置的ASCX code div标签的样式落后?这是从code中的描述背后:CssStyleCollection HtmlControl.Style

How do I set the style for a div tag in the ascx code behind in C#? This is the description from the code behind: CssStyleCollection HtmlControl.Style

更新:我更换了ASP:hiddenfield有一个asp:标签,我得到一个不确定当我显示一个警告框的HiddenValue变量。我将如何解决这个问题。

UPDATE: I replaced the asp:hiddenfield with an asp:label and I am getting an "undefined" when I display the HiddenValue variable in a alert box. How would I resolve this.

更新2:我继续和重构了code,I替换为文本框控件隐藏的现场控制并设置样式显示:无;。我也删除了JavaScript函数(它被用来通过CustomValidator控件),并与RequiredFieldValidator控件取代了它。

UPDATE 2: I went ahead and refactored the code, I replaced the hidden field control with a text box control and set the style to "display: none;". I also removed the JavaScript function (it was used by a CustomValidator control) and replaced it with a RequiredFieldValidator control.

推荐答案

我的理解是,如果你设置controls.Visible =初始页面加载时假的,它不会在客户端的响应呈现。
我的建议来解决你的问题。

My understanding is if you set controls.Visible = false during initial page load, it doesn't get rendered in the client response. My suggestion to solve your problem is


  1. 请不要使用占位,从方案来看,你并不真的需要一个占位符,除非你需要动态添加服务器端控件。 div的使用,没有RUNAT =服务器。你总是可以控制的了可视性div的使用CSS。

  2. 如果您需要动态以后添加控件,利用占位符,但是没有设置可见=假。占位符不会有任何显示无论如何,使用CSS设置该占位符的可见性。以下是如何做到这一点programmactically:

  1. Don't use placeholder, judging from the scenario, you don't really need a placeholder, unless you need to dynamically add controls on the server side. Use div, without runat=server. You can always controls the visiblity of that div using css.
  2. If you need to add controls dynamically later, use placeholder, but don't set visible = false. Placeholder won't have any display anyway, Set the visibility of that placeholder using css. Here's how to do it programmactically :

placeholderId.Attributes [风格] =显示:无;

placeholderId.Attributes["style"] = "display:none";

不管怎么说,正如其他已经指出,出现你的问题,因为一旦你设置control.visible =假,它不会在客户端的响应呈现。

Anyway, as other have stated, your problems occurs because once you set control.visible = false, it doesn't get rendered in the client response.

这篇关于访问一个asp:在JavaScript中HiddenField控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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