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.
Related
I have a template property in my control declared as follows:
<TemplateContainer(GetType(GenericTemplateContainer)),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateInstance(TemplateInstance.Single)>
Property CustomTemplate As ITemplate
In my control's Init event I have the following:
If Me.CustomTemplate IsNot Nothing Then
Dim TemplateContainer As New GenericTemplateContainer
Me.CustomTemplate.InstantiateIn(TemplateContainer)
PlaceHolder.Controls.Add(TemplateContainer)
End If
This allows me to place controls in markup inside my template, but on a post back the controls inside the template are not holding their ViewState.
I have tried adding PersistChildren(True) attribute to CustomTemplate property but I cannot because it's not valid.
Are you putting the values into ViewState? From what I understand, you need to do that. Either that, or re-bind with the data on every postback.
Here's what I like to do inside User Controls. I apologize for this being C# and not VB, but I don't know VB:
public string Text {
get { return (string)ViewState["Text"]; }
set { ViewState["Text"] = value; }
}
Reference: https://weblogs.asp.net/infinitiesloop/Truly-Understanding-Viewstate
I've a file upload control in my asp.net user control. This user control is being used in a asp.net page. I want to access the file path selected through this control in my parent page. I'm aware that for user controls, we need to make public properties in code behind which are then available to parent. I wrote the below code in the code behind of user control to make a public property for file path
public string TCSnippetFilePath
{
get
{
return Path.GetFullPath(fuTCSnippet.PostedFile.FileName);
}
set
{
Path.GetFullPath(fuTCSnippet.PostedFile.FileName) = value;
}
}
fuTCSnippet is the ID of File Upload control
This code is giving me below error:
The left-hand side of an assignment must be a variable, property or indexer
Any inputs on what needs to be done?
you cant assing value to fileupload control you can get the file name
public string TCSnippetFilePath
{
get
{
return Path.GetFullPath(fuTCSnippet.PostedFile.FileName);
}
// not need to set properties value
}
beacause FileUpload Control Properties HttpPostedFile have only get value you can't assing HttpPostedFile to Fileuploadcontrol
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 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.
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
}