469,304 Members | 2,018 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

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

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

Demo.ascx code :
Expand|Select|Wrap|Line Numbers
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Demo.ascx.cs" Inherits="Demo" %>
  2. <asp:HiddenField ID="hidden1" runat="server"/>

Expand|Select|Wrap|Line Numbers
  1. public String TextPropertuy
  2. {
  3.   get
  4.   {
  5.     return hidden1.Value;
  6.   }
  7.   set
  8.   {
  9.     hidden1.Value = value;
  10.   }
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14.   if (!IsPostBack)
  15.   {  
  16.      hidden1.Value = "2255";
  17.   }
  18. }
Sep 28 '11 #1
5 14393
9,735 Expert Mod 8TB
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.

Sep 28 '11 #2
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=hide.value; But not getting any output, Will u give me another solution for this...
Sep 29 '11 #3
9,735 Expert Mod 8TB
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.

Sep 29 '11 #4
Will you please explain me this via code..... In my given code you change it, then reply mje , It's urgent please help me...
Sep 30 '11 #5
9,735 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.    myLabel.Text = Hide.TextPropertuy;
  4. }
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.

Sep 30 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Tom | last post: by
4 posts views Thread by William LaMartin | last post: by
5 posts views Thread by Fernando Chilvarguer | last post: by
1 post views Thread by Ann | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.