I am trying to use the already written code which accesses a control from an other control in the following code.
Controls_SearchFeaturedMerchants UCMerchant = (Controls_SearchFeaturedMerchants)this.Parent.FindControl("UCSearchFeaturedMerchants1");
I am wondering what this statement says. Can someone please give me some idea :)
Thanks in advance!
If I understood your question correctly here is the brief:
On a page (ContentPage) is hosting two user controls (UsrCtrl1, UsrCtrl2).
Now "UsrCtrl1" wanted to access some data in the "UsrCtrl2".
For that I'll write code like - "UsrCtrl1" parent is the "ContentPage" that page hosts "UsrCtrl2", so I'll first get the "UsrCtrl2" from the parent page with the following code:
this.Parent.FindControl("UsrCtrl2Name") -> this means current user control which is "UsrCtrl1" and Parent means is the "ContentPage" (it may be parent control or page) in that you are trying to find a contrl with Id "UsrCtrl2Name" (this is the id of the 2nd user control in the content page).
When you use FindContrl method it always returns base type UserControl and you need to cast it to your usercontrol in this case it is of type "UsrCtrl2".
I hope this is clear now.
in plain text it is saying:
Get the Parent Control of the current control and then find UCSearchFeaturedMerchants1 on said control. Cast the result into a Controls_SearchFeaturedMerchants.
Hope that sums it up for you
Related
I have a user control, say control 1, that looks for control2, which is placed on the root master page : Site.Master
And this is how I am getting Control2 from Control 1 now,
MasterPage showMaster = this.Page.Master.Master;
MasterPage siteMaster = showMaster.Master;
Control2= siteMaster.FindControl("Control2");
The above code works fine. But because our application uses nested master pages, I am running into a bit of situation here.
How do I find control 2 dynamically regardless of where I put Control1 in which template? Becasue right now, depends on where I put Control1, and how nested is that template in relation to the Site.Master, I have to change how far up in the chain I get Site.Master in the Control 1 code.
Any good ideas on how I can avoid doing that?
Please advise.
i found a work around with recursively loading the control
Am using a user control say C in a page say A. So also while doing a submit / or action in the user control ( It contains a grid with edit delete functionality) , control goes to parent page A then only enters to the edit or delete function.
How can i identify that the control has been directed from the user control?
Please help me out.
Thanks in Advance
How about
if (sender is C) // You said your usercontrol is say C
{
//Your Code Here
}
I got a work around . I would like to share it over here.
We can use
this.Parent.FindControl("x").Visible = false;
here x is the div in the parent page which incorporates the user control. Using this i solved my problem instead of tracking the flow.
My problem was like while the user control pops up, one control inside the parent page also get popped up even if its visibility is set to false. I needed to truncate this.
Thanks .
I have a page called test.aspx with test.cs.
However, i want to access my control called mbResult
Which is my custom messagebox control, from a sepearate CS page.
I know many people have asked this question and i have found that this is a method to access my controls.
MessageBoxControl mbox1 = this.FindControl("mbResult") as MessageBoxControl;
But I keep getting this error
Error 5 Keyword 'this' is not valid in a static property, static method, or static field initializer
Any ideas on how to access this control all i am trying to do is make it visible.
Thanks
You need to move the code into a non-shared method. You need to be operating in an instance of the page.
Update for clarification in comments
Unfortunately, your application is going to need some restructuring.
If the messageboxcontrol is shown in a new window, then you will need to pass the value from your source page to the new window in the query string.
However, if you want the messagebox control to be displayed on the source page, then you will need to convert it from a page to a UserControl, add a reference to the user control to your source page, and then add an instance of the usercontrol directly to the source page.
Statics don't have instance-based contexts, so using this is not applicable. You'll need a reference to the control for which you want to use .FindControl (possibly by passing it as a parameter).
I have seen two suggestions for my original question about whether it is possible to define a content area inside a user control and there are some helpful suggestions i.e.
Passing in content to ASP.NET user control
and
ASP.NET User Control inner content
Now, I like the theory of the latter better than the former just for aesthetic reasons. It seems to make more sense to me but the example given uses two variables content and templateContent that the answerer has not defined in their example code. Without these details I have found that the example does not work. I guess they are properties of the control? Or some such?
EDIT - DETAILS: What I am trying to do
I have need of an ASP.Net user control that conceals some content in a panel inside a placeholder and asks for the input of a code in a visible panel.
Essentially the user will put their code into the provided textbox in Panel A and submit it, it will be checked and, if it is valid, panel B and the locked content will be displayed.
I have done a test where the content was hard coded into panel B but as soon as I need to make the content a generic input it fails. If it were just text or somesuch then I could make it a property of the control, but as it is, in fact, another User Control I am having some difficulty getting this into the "hidden" panel.
Any other workable solutions are also welcome.
EDIT NOTE: The solution I'm trying to implement this in 2.0 I did find a 3.5 solution which I cannot use.
The former example seems workable but I'd prefer to go with the latter if someone could fill in the blanks for me.
Thanks.
Okay, so this is disturbingly easy but many of the tutorials on the web that talk about this kind of thing push to do extravagant things that require the control to parse ListItems or such.
So this solution is purely so that you can build a control that, for whatever reason, has a placeholder in it that could have anything inside it (kind of like a content area on a Master page). In this instance it happens to be because the Panel containing the placeholder is hidden until appropriate input actions have taken place in another panel.
First, you need to add this:
[ParseChildren(true,"Content")]
[PersistChildren(false)]
just above the part of the control which looks like this:
public partial class MyControl : System.Web.UI.UserControl
then in the control scoped declarations at the head of the control you want to declare thus:
private Control _content;
[PersistenceMode(PersistenceMode.InnerProperty)]
public Control Content { get { return _content; } set { _content = value; } }
Finally you need to place the content into the placeholder like this:
phContent.Controls.Add((Control)_content);
That last line goes into the Page_Init event. For reference "phContent" is the name of the place holder where you want the content to appear, like this:
<asp:Panel ID="pnlLockable" runat="server" Visible="False">
<asp:Placeholder runat="server" ID="phContent" />
</asp:Panel>
On the front end the resulting implementation looks like this:
<uc:MyControl runat="server" ID="lockit1">
<Content>
//...your stuff goes here...
</Content>
<uc:MyControl>
Note that I presume that what is inbetween the Content Tags is a root control. This is because I nested another user control in there. I imagine if you put whatever content you want within a panel or placeholder it should be fine.
Also you can read "How to: Create Templated ASP.NET User Controls". Really helpful.
I'm trying to edit a textbox's text in a Page_Load form on an ASP.NET page, but when I try to edit TextBox1.Text it throws a NullReferenceException because the TextBox has not yet been instantiated. Specifically what I'm trying to implement is this: http://www.codeproject.com/KB/user-controls/popupcalendarcontrol.aspx but it is written for an older version of ASP.NET and does not work for me. Is there a way to instantiate the controls at the start of Page_Load? or another event that I can catch on load? With a normal windows form I would call InitializeComponent() in the constructor.
There are absolutely different events you can attach to but it sounds like the page isn't loading the controls properly because they should be available by that point. You can take a look at the ASP.NET Page Life Cycle for more information.
Does the TextBox sit within a bound control, such as a FormView, GridView, DataList, etc? If so, then the control won't exist until after the databinding happens. Once that event happens, you can do something like
DirectCast(myDataList.SelectedRow.FindControl("myTextBox"),
TextBox).Text
I know that I am a bit behind in getting to this question BUT I have found something unusual that I can NOT find documented anywhere. It would appear that IF your page is "under" a Master Page, and IF you refer to ANYTHING on said master page, the controls to your current page are null - EVERYONE of them. I found this out by referencing "Master.Environment" - a public string - during my InitializeCulture method and having the FIRST control on my current (Default.aspx) page be null during Page_Load() - WHATEVER the first control was.
I know I am late but if this can help ANYBODY, I want the word out.