This is how a register the control on the page:
<%# Register TagPrefix="uc" TagName="Pcp" Src="../../Controls/ClientPayement.ascx" %>
And this is how I use it:
<uc1:Pcp ID = "pcpClient1"></uc1:Pcp>
I don't know why uc1 has a green underline warning. When I hover over it, it says: "Unrecognized namespace uc1". And the user control is not displaying on the page.
Am I missing something? Yet this tutorial says that this is all I need to do.
You are specifying a uc prefix in <%# Register TagPrefix="uc" .. so that's the one you should use:
<uc:Pcp ID = "pcpClient1"></uc:Pcp>
Or you could change the declared prefix:
<%# Register TagPrefix="uc1" TagName="Pcp" Src="../../Controls/ClientPayement.ascx" %>
And use the control as you posted in the question:
<uc1:Pcp ID = "pcpClient1"></uc1:Pcp>
EDIT
As Shai Cohen pointed out in another answer, you are also missing the runat="server" from the usercontrol tag.
<uc1:Pcp ID = "pcpClient1" runat="server"></uc1:Pcp>
In addition to Juan's answer, you are also missing runat="server".
Related
suppose i have two user control. usercontrolA and usercontrolB. how to include usercontrolB into usercontrolA. also please tell me how to read controls value of usercontrolB from usercontrolA and also from page. looking for details discussion. thanks
May be below can help you
In Control A
<%# Register Src="~/UserControls/ControlB.ascx" TagName="ControlB" TagPrefix="uc" %>
<uc:ControlB ID="myControlB" runat="server" />
In Control A.ascx.cs
string userName=myControlB.UserNameTextBox.Text;
In User ControlB.ascx.cs have some properties like
public TextBox UserNameTextBox
{
get {return this.txtUserName;}
}
I am doing this:
<% Html.RenderPartial("SomePartial", Model); %>
My Model has a property UserID
In my partial I try this:
<%= Model.UserID %>
but I get this error:
CS1061: 'object' does not contain a definition for 'UserID' and no extension method 'UserID' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Blankman,
Make sure your partial ascx file defines the model as per the main view i.e:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Your.Model>" %>
[edit] - as Stefanvds mentions below, if you only need the id portion from the model, then define your partial as:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int>" %>
that would be the definitive i reckon :)
cheers
Make sure that at the top of your partial view you have Inherits attribute of your Control tag set to the type of the model you're passing in:
Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.MyStronglyTypedModel>"
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DB.Models.TheModelYouWantToSend>" %>
your partial should start with this.
also, if you want to send the whole model, you dont need to specify it.
<% Html.RenderPartial("SomePartial"); %>
or if you would only like to send the ID.
<% Html.RenderPartial("SomePartial", Model.UserID); %>
which would make the header
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int>" %>
The model type on your Partial View and the model you pass in to Html.RenderPartial method should match.
The way this page is laid out, all of the data is loaded at Page_Init. Well, I have a custom control that is having problems with this though.
I have it on the page like so:
<cc:SomeControl... />
And then I set the value at Page_Init using
MyControl.Value="blah";
Simple stuff..
The Value is an accessor and has something similar to this:
public string Value{
get...
set{
EnsureChildControls();
MyHiddenField.Value=value;
}
}
and it is here that I have a problem. It says that MyHiddenField is null. Is Page_Init just too early for this? Or is there some other function I need to call?
The fix for this was changing from using a namespace to reference the CustomControl to using a src with a filename
changing this:
<%# Register Assembly="MyProduct" Namespace="MyProduct.CustomControls" TagPrefix="cc" %>
to this:
<%# Register src="/CustomControls/MyControl.ascx" tagname="MyControl" tagprefix="uc2" %>
I have included a user control in another statically following code :
place the folowing directive in the asp code of the parent page or
usercontrol:
<%# Register src="Name_of_your_child_control.ascx"
tagname="Name_of_your_child_control" tagprefix="uc1" %>
use the following tag in the asp-code of the parent page/control:
<uc1:Name_of_your_child_control ID="Name_of_your_child_control1"
runat="server" />
.....
But the issue is...i am not able to access the public properties of user control which got included(child user control) in given user control(parent user control)...
Please help :(
Say your usercontrol was this:
<%# Control Inherits="Project.MyControl" Codebehind="MyControl.ascx.cs" %>
<asp:TextBox ID="TB" runat="server" />
Your control code-behind:
namespace Project
{
public partial class MyControl : UserControl
{
public string MyTextProperty
{
get { return TB.Text; }
set { TB.Text = value; }
}
}
}
In your parent page that included the control, like this:
<%# Register src="~/MyControl.ascx" tagname="MyControl" tagprefix="uc1" %>
<uc1:MyControl ID="MyControlID" runat="server" />
You can use that property in code:
MyControlID.MyTextProperty = "bob";
Using
Name_of_your_child_control1.PublicPropertyName
must work for your parent user control.
Check the path and file names you are using, Anish. You have something wrong. Is Visual Studio telling you it can't find the control? Is it failing at compile time? Runtime?
It's funny but whenever you add a property to a user control.
You need to register it again in the parent. So in your case,
Add a space at the end of this line and remove it again:
$<% Register src="~/MyControl.ascx" tagname="MyControl" tagprefix="uc1" %>
This will re - register the user control and you will be able to access new properties.
Asp.net: Can we use MasterPage's viewstate in ContentPage ?
From a Content Page you can refer to a MasterPage through the Master Property. Create a Property on the Master Page that uses its getter and setter to store its value in ViewState, like so:
string MyProperty
{
get { return ViewState["MyProperty"] as string; }
set { ViewState["MyProperty"] = value; }
}
Obviously you could make that code safer by testing for nulls and what-not...
Here's the important bit: Viewstate elements are only accessible from the controls that added them so you need to refer back up the tree.
You can also strongly type the Master property on Page by using the <%# MasterType %> directive in your ASPX file, thusly:
<%# MasterType VirtualPath="~/masters/SourcePage.master"" %>
HTH.