I have a hidden field on my default.aspx page. Within default.aspx page I have a user control which has a label on it. I need the label to display the value on the hidden field. How can I go about this?
Create a public property on the user control which exposes the label text:
public string LabelText
{
get { return this.label1.Text; }
set { this.label1.Text = value; }
}
Then set this from the page code-behind. So if the ASPX for your page has something like this in it:
<uc1:UserControl ID="userControl" runat="server" />
In the code-behind, you could do
this.userControl.LabelText = "something";
Doing it this way means that your user control doesn't have to know about the page that's using it. This helps the user control to be re-used on many different pages.
Related
I have a aspx page which has five panels. Each panel has a usercontrol in it. The user controls on all the five panels are the same. The user control in the first panel is to gather data for primary members and the rest are used for secondary members. I use ">" and "<" custom buttons to move from one panel to another. Whenever I move from the primary panel to secondary I wan to hide two textboxes in the usercontrol. When they move back to the primary panel, those textboxes should reappear. Since the buttons are in the parent page, please let me know how to do it. I believe creating an event in the usercontrol and accessing it from parent page may work. But not sure how to do it. Or please let me know any other methods.
Thanks
You need not create an event in the user controls for this.
All that you need is to create a public property on the user control, that you can set when you use the user control.
Since you have not provided any code, I will just give a sample.
public partial class MyWidget: System.Web.UI.UserControl
{
private bool showPrimary;
public bool ShowPrimary
{
get { return showPrimary; }
set
{
showPrimary = value;
txtPK1.Visible = value;
txtPK2.Visible = value;
}
}
}
Then you set it when you call it as follows:
Main Panel:
<uc1:MyWidget ID="MyWidget1" ShowPrimary="true" runat="server" />
Secondary Panel:
<uc1:MyWidget ID="MyWidget1" ShowPrimary="false" runat="server" />
I don't understand, the buttons > and < are in the page to advance to the next/previous control. Then you just have to handle their click event in the page and switch visibility of the UserControls.
If you don't know how to control visibility of controls inside of UserControls from the page:
Use properties in the UserControl, for example PrimaryMode. There you can hide/show the TextBoxes accordingly. You can call these properties from the page. PrimaryMode could be of type bool or a custom enum type(if you want to provide multiple display-modes).
I have this following property in my custom user control:
public string selectedtab
{
get
{
if (ViewState["AdminCurrentNavID"] != null)
{
return ViewState["AdminCurrentNavID"].ToString();
}
else {
isfirstload = true;
return null;
}
}
set { ViewState["AdminCurrentNavID"] = value; }
}
I am setting the value of it on my Page_Load() in ascx control. What i need to do is that after setting the value of this property I need to access it from masterpage.cs in code behind. you can see how currently I am trying to do in below code, but the issue is that I am not able to get the value i thing it is because the masterpage's Page_Load() rendering before the ascx control so I thats why I am getting null value, please help, thanks.
masterpage.cs:
usercontrols.mainmenu adminmenu = (usercontrols.mainmenu)LoadControl("~/mymenupath.ascx");
lbmsg.Text = adminmenu.selectedtab;
When you call LoadControl in your master page, you are actually creating a new instance of your user control, not accessing the one you have somewhere in your site.
When you declare the User Control in your page you should have given it an id. You could access the property with something like ((usercontrols.mainmenu)MyUserControlId).selectedtab
I found the solution by using Delegate, you can see in the link below.
http://webdeveloperpost.com/Articles/Return-value-from-user-control-in-ASP-NET-and-C-Sharp.aspx
I am loading a control to a page dynamically with LoadControl("src to file").
In the usercontrol i have a validator and some other controls that i would like to access from my page. I canät get it to work, null pointer exception.
Scenario is like this. I have a Edit.aspx page which loads the EditTemplate.ascx usercontroll. I would like to get information or find the controls in the EditTemplate from the Edit.aspx site.
I have tried exposing the controls and validators as properties but how do i access them from my Edit.aspx?
Example code:
Edit.aspx, the control is later added into a
Control control = LoadControl("src to ascx");
TemplatePlaceHolder.Controls.Add(control);
EditTemplate.ascx
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="CompanyImageFile" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
CodeBehind
public partial class EditTemplate : System.Web.UI.UserControl, IEditTemplate {
public RequiredFieldValidator Validator {
get { return this.RequiredFieldValidator1; }
set { this.RequiredFieldValidator1 = value; }
}
From the Edit.aspx site i would like to check the validators isValid property. Isvalid is set in a Save method.
The save button that saves the template is located in edit.aspx, so the post in done from that page.
So the question is how to get a hold of the property from the usercontrol in the edit.aspx page, where and how should this be done?
Thanks again.
Easiest way is to have the user control define properties like:
public IValidator SomeValidator {
get { return this.cuvValidator; }
set { this.cuvValidator = value; }
}
public string Text {
get { return this.txtText.Text; }
set { this.txtText.Text = value; }
}
Which your edit page can use.
HTH.
You can always use recursive approach. Check the solution on Steve Smith's blog:
Recursive-FindControl.
As mentioned in previous answers, I would expose any validators you must access from the parent ASPX page as properties in the user control.
public RequiredFieldValidator ValidatorToCheck
{
get { return this.rfvMyField; }
}
Then, you can dynamically add your user control to some placeholder (being sure to assign an ID to the user control).
// In my example, this is occurring in the Page_Load event
Control control = LoadControl("~/Controls/EditTemplate.ascx");
control.ID = "ucEditTemplate";
pnlControlHolder.Controls.Add(control); // the placeholder in my example is a panel
When you want to access the IsValid property on the given validator (presumably in your save action) you can do so as follows (being sure to cast the control to the appropriate type and using the ID you originally assigned to the user control):
EditTemplate control = (EditTemplate)pnlControlHolder.FindControl("ucEditTemplate");
if (control.ValidatorToCheck.IsValid)
{
// Some action
}
I have created a menu control in asp.net and have put it into master page. This menu control have property ActiveItem which when modified makes one or another item from menu appear as active.
To control active item from child page I have created a control which modifies master page property enforced by IMenuContainer interface to update menu control's active item in master page.
public class MenuActiveItem : Control
{
public ActiveItemEnum ActiveItem
{
get
{
var masterPage = Page.Master as IMenuContainer;
if (masterPage == null)
return ActiveItemEnum.None;
return masterPage.ActiveItem;
}
set
{
var masterPage = Page.Master as IMenuContainer;
if (masterPage == null)
return;
masterPage.ActiveItem = value;
}
}
}
Everything works perfectly and I really enjoy this solution, but I was thinking that, if I knew how, I would have created a custom directive with same feature instead of custom control because it just makes more sense that way.
Does anybody know how to do it?
You should be able to turn this into a custom property of your Page, which you can set in the Page directive.
Create a base class for your page, and then change your Page directives like this:
<%# Page Language="C#" MasterPageFile="~/App.master"
CodeFileBaseClass="BasePage" ActiveItem="myActiveItem" AutoEventWireup="true"
CodeFile="Page1.aspx.cs" Inherits="Page1" %>
You may have to change the property to be a string, and do a conversion to the enum. But otherwise your code can remain the same, and it doesn't have to be in a control.
For example, I have a user control(ascx) with a label inside,
I will use the the user control in my aspx page.
How can I pass a string value to the ascx page so that it can be display in the label of ascx page at the beginning?
Add this...
public string Whatever
{
get { return label.Text; }
set { label.Text = value; }
}
to your ascx control. Then from the page you are putting it in you can just set the text like... usercontrol.Whatever = "text to display";
or you can use the Whatever as a property on the aspx side of the page.
You can expose whatever controls you want access to in your user control by creating property for them.
In the past when I have had user controls that required certain data for setup I would create an Initialize method which would take in and setup whatever was needed.