简体   繁体   中英

How is a hidden field declared in ascx page so that it takes value from hidden field in aspx?

Objective: Read a Hidden Field value from my aspx page in an ascx page

Issue: I am new to ASP and I don't know how to accomplish this. I can set the value of the hidden field in the aspx page, but how do I read that value in the ascx page? The code I am using is as follows

Page1.aspx

<%@ Register Src="~/UserControl/Page2.ascx" TagName="Info" TagPrefix="uc" %>
<asp:HiddenField ID="hdnfldInfo" runat="server" />

Page1.aspx.cs

String strInfo = Convert.ToString(e.CommandArgument);
hdnfldInfo.Value = strInfo;

Page2.ascx

HiddenField Info = (HiddenField)this.Info.FindControl("hdnfldIncDesc");

the code above on Page2.ascx does not have the same value. What am I missing or doing incorrectly?

Thank you in advance for any comments, recommendations or suggestions

You need to create a property on the ascx (also called a user control) and have the aspx page set the property based on the hidden field

Have your ascx code behind something like this:

public class MyUserControl : UserControl{
     public String MyProperty {get;set;}

     //...do stuff with myProperty

}

and then in the aspx page code behind

public class MyPage : Page{
     protected void Page_Load(){
      HiddenField info = (HiddenField)myHiddenField;
      MyUserControl control = myusercontrol;
      control.MyProperty = info.Value;
     }

}

you may need to change which part of the page life cycle the property is set.

I'd also check out some articles on creating user controls since you are new to asp.net. It will pay off later.

Take a look at: good article in gernal about creating user controls: http://msdn.microsoft.com/en-us/library/3457w616(v=vs.100).aspx (skip to Adding Custom Properties and Methods to the User Control section for relevant question)

and send custom parameters to user control ascx has an ok example

you would need to access the parent page from the user control in addition to making that hidden field public and not protected (by default) by going into the designer code behind. The best practice is to make a property in the user control from which the parent sets it then the user control would use the property in question to display it on the page.

// usercontrol:

public string MyProperty {get; set;}

// ASPX page:
this.ucMyUserControl.MyProperty = this.hdnfldInfo.Value;

Then in your user control, use "MyProperty" wherever you see fit.

The ascx control has a reference to the page it's in which we could use.

this.Page

Now we need to get a control from that page. Since we know it's name you can use FindControl

HiddenField Info = this.Page.FindControl("hdnfldIncDesc");

Note that this will return null if a control with that name could not be found. So code accordingly.

使用this.Parent.FindControl(HiddenBankAccountId)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM