How to get value of hiddenfield from ascx.cs page to aspx.cs page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mahendra Kamble
    New Member
    • Sep 2011
    • 1

    How to get value of hiddenfield from ascx.cs page to aspx.cs page

    Check.aspx code is
    Code:
    <%@ Register TagName="TopBar" TagPrefix="CRS" Src="~/Demo.ascx" %>
    ---
    <div>
    <CRS:TopBar ID="Hide" runat="server"/>
    </div>
    Check.aspx.cs code is
    Code:
    protected void Page_Load(object sender, EventArgs e)
    {
      Demo uc = (Demo)Page.LoadControl("Demo.ascx");
      HiddenField hide = (HiddenField)uc.FindControl("hidden1");
      Response.Write("--- " + hide.Value + " Value <br>");
    
    
    <-- I want Hiddenfield value here how can I access from ascx.cs page-->
    }

    Demo.ascx code :
    Code:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Demo.ascx.cs" Inherits="Demo" %>
    <asp:HiddenField ID="hidden1" runat="server"/>


    Demo.ascx.cs
    Code:
    public String TextPropertuy
    {
      get
      {
        return hidden1.Value;
      }
      set
      {
        hidden1.Value = value;
      }
    } 
    
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {  
         hidden1.Value = "2255";
      }
    }
    Last edited by Frinavale; Sep 28 '11, 07:22 PM. Reason: Added code tags and formatted code.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Why can't you just access the public property that exposes the HiddenField value?

    You should not be using Response.Write in your C# code.
    You should only use it in your ASP code where you want the value written to. If you use Response.Write in your C# code it will make your page's HTML invalid.

    Use a Label or a Literal instead...put a Label or Literal on the aspx page and then set this Label or Literal's Text property to the value of the hidden field.

    -Frinny

    Comment

    • ma966
      New Member
      • Sep 2011
      • 2

      #3
      It's not getting value.. I used Label control where I want Hidden field value and I assign it, But not getting value... I wrote code like this.. in Check.aspx.cs page
      Instead of "Response.Write () I used Label..." i.e.
      Label1.text=hid e.value; But not getting any output, Will u give me another solution for this...
      Thanks.....

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Oh I see what you're doing wrong now!

        You're dynamically adding the control...but you're doing it at the wrong stage in the ASP.NET life cycle. You can't do this in the Page Load event.

        You need to move this to your Page Init event.
        In the Page Init event the controls are initialized.... right after this event, and before the Page Load event, the input on the page is loaded into the controls (that were initialized). If your control doesn't exist at this stage, then the input is not loaded!

        Check out this article on How to Use dynamic Controls in ASP.NET.

        If you are new to ASP.NET, you should consider just putting the user control into the page itself and avoid dynamically loading controls.

        -Frinny

        Comment

        • ma966
          New Member
          • Sep 2011
          • 2

          #5
          Will you please explain me this via code..... In my given code you change it, then reply mje , It's urgent please help me...

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Well, you have added the Demo user control to the Check.aspx page already...it has the ID "Hide".

            This means that "Hide" (which is a Demo user control) is part of the Check page so you can access it in the Check page.

            The Demo user control has a property named "TextPropertuy" (which is misspelled but that doesn't matter). You can call the TextPropertuy property anywhere in the life cycle of the Check page.

            For example, you can get the value of the hidden field in the Check Page's Load event through the TextPropertuy Property in order to set the Text property of a Label control with the ID "myLabel" like this:

            Code:
            protected void Page_Load(object sender, EventArgs e)
            {
               myLabel.Text = Hide.TextPropertuy;
            }
            Notice how I did not use the LoadControl method?
            That's because the control that contains the value is already part of the page. The LoadControl method is only used to dynamically add user controls to the page...which you aren't doing.

            -Frinny

            Comment

            Working...