最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Access an asp:hiddenfield control in JavaScript - Stack Overflow

programmeradmin1浏览0评论

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.

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>

EDIT: 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

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.

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.

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.

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>

EDIT: 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

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.

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.

Share Improve this question edited Oct 31, 2008 at 16:01 Michael Kniskern asked Oct 31, 2008 at 0:14 Michael KniskernMichael Kniskern 25.3k70 gold badges169 silver badges233 bronze badges 3
  • Well it can be set using Attributes property, but why do you need to do that server side? – Salamander2007 Commented Oct 31, 2008 at 0:43
  • I am setting it server side because the hidden field is displayed when the user selects an item from a search result set. – Michael Kniskern Commented Oct 31, 2008 at 0:45
  • Hidden field, by definition is hidden. If you need to show the value of hidden field, use other controls like TextBox or Label – Salamander2007 Commented Oct 31, 2008 at 0:47
Add a comment  | 

6 Answers 6

Reset to default 6

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. 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["style"] = "display:none";

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

If the Visibility is set to false server-side, the placeholder won't be rendered and you won't be able to access anything inside it from JavaScript. Your code should work when the placeholder is visible="true"

Get rid of the placeholder, leave the hidden field empty at first, after the search populate it.

Try this:

function popup(lid)
{
    var linkid=lid.id.toString();    
    var lengthid=linkid.length-25;    
    var idh=linkid.substring(0,parseInt(lengthid));  
    var hid=idh+"hiddenfield1";

    var gv = document.getElementById("<%=GridViewComplaints.ClientID %>");
    var gvRowCount = gv.rows.length;
    var rwIndex = 1;
    var username=gv.rows[rwIndex].cells[1].childNodes[1].innerHTML;
    var prdid=gv.rows[rwIndex].cells[3].childNodes[1].innerHTML;
    var msg=document.getElementById(hid.toString()).value;
    alert(msg);


    document.getElementById('<%= Labelcmpnme.ClientID %>').innerHTML=username;
    document.getElementById('<%= Labelprdid.ClientID %>').innerHTML=prdid;
    document.getElementById('<%= TextBoxviewmessage.ClientID %>').value=msg;
    return false;
}


<ItemTemplate>
    <asp:LinkButton ID="LabelComplaintdisplayitem" runat ="server"   Text='<%#Eval("ComplaintDisp").ToString().Length>5?Eval("ComplaintDisp").ToString().Substring(0,5)+"....":Eval("ComplaintDisp") %>' CommandName ="viewmessage" CommandArgument ='<%#Eval("username")+";"+Eval("productId")+";"+Eval("ComplaintDisp") %>' class='basic' OnClientClick =" return popup(this)"></asp:LinkButton>
    <asp:HiddenField ID="hiddenfield1" runat ="server" Value='<%#Eval("ComplaintDisp")%>'/>
</ItemTemplate>

If the place holder visibility is set to false, it will never be rendered , and the hidden field value will be only stored in the ViewState of the page.

just one question, why are you setting the visibility of the place holder to be false , if its containing a hidden field?

Anyway one possible way to get over this issue, is adding a TextBox or Label object , and set the display CSS style of it to "none" , then in your code copy whatever you are putting in the hidden field into the textbox/lable text property, this way you can easily read the value using javascript , since the textbox/label will be rendered but not visible to others, though this might not be that safe thing to do.

Instead of making ".visible=false", change the style to "display: none;". That will render your control but make it invisible.

Visible doesn't actually make it visible, you can leave it default. Just runat="server" and use its .Value.

发布评论

评论列表(0)

  1. 暂无评论