I am having a problem passing a variable to a user control.
When I pass a hard-coded value, my control works fine. However passing a variable is not working.
My ASPX code:
<% System.Console.Out.WriteLine("Operate Flag is set to: " + operateFlag); %>
<uc:menu ID="navigationMenu" runat="server" operate="<%# operateFlag %>" />
From debug output, it is clear that operateFlag is set to 'true'.
My control codebehind only has synthesized methods as below:
public partial class MenuControl : System.Web.UI.UserControl
{
public bool operate { get; set; }
}
I also print out the received value from inside the control:
<% System.Console.Out.WriteLine("Operate Flag is received as: \'" + operate + "\'"); %>
Here, operate is received by the control as False.
I figured out a way to get my page working. Still do not understand what was wrong with the original "form."
Now, as before from OnLoad(), I set the property of the control instead of passing a variable as:
navigationMenu.operate = false;
It solves my need.
Related
This is the ascx control in .aspx
<Menu:MNU ID="PMPHeaderMenu" runat="server" HiLiter="<%=h%>"></Menu:MNU>
in aspx.cs I have
public int h = 1;
....
....
h = 5;
in ascx.cs I have the HiLiter Property
public string HiLiter { get; set; }
When I debug I get the value as <%=h%> for HiLiter when I expect it to be 5.
How will I pass the server side set value to the user control?
You can't use <%=%> for controls with runat="server" for setting properties, <%=%> is similar like Response.Write.
<%# %> (Data-binding expressions) can be used to fill control properties, but the control should be inside a data-binding container like GridView, DetailsView, FormView and Repeater.
In your case you should set the value of the property from the code behind (aspx.cs) page like following.
PMPHeaderMenu.HiLiter = h;
this.DataBind();
I have been searching the internet and most I find resolves the issue of accessing the master page properties from the user control's code behind. But I am unable to find a solution where the user control can have access to the master page's properties within the markup.
Background:
The master page dynamically adds user control onto the page.
The master page has two properties which the user control needs to access via markup.
Here is some code to represent my problem:
Master page's code behind properties:
public IModule Module
{
get
{
return MyContext.Current.Module;
}
}
public IDictionary<string, object> Arguments
{
get
{
return MyContext.Current.Arguments;
}
}
Master page dynamically adds to control in code behind (it HAS to be dynamically added in master page's code behind):
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!(Page is VehicleForm) && !(Page is VsrManageForm) && !(Page is VpbManageForm))
{
MenuTab view = (MenuTab)this.LoadView(plhMenu, "~/Controls/MenuTab.ascx", "", MyContext.Current.Module);
}
}
User control's markup:
<web:FlowLink class="tools__lnk" arguments="<%# Arguments %>" id="flowLink1" runat="server" contextmodule='<%# Module %>' flowcall="FavouritesView" title="" rel="nofollow" ClientIDMode="Omitted">Shortlist</web:FlowLink>
<web:FlowLink class="tools__lnk" arguments="<%# Arguments %>" id="flowLink2" runat="server" contextmodule='<%# Module %>' flowcall="CompareView" title="" rel="nofollow" ClientIDMode="Omitted">Compare</web:FlowLink>
<web:FlowLink class="tools__lnk" arguments="<%# Arguments %>" id="flowLink5" runat="server" contextmodule='<%# Module %>' flowcall="UserView" title="" rel="nofollow" ClientIDMode="Omitted">Account</web:FlowLink>
Error:
Compiler Error Message: CS0103: The name 'Arguments' does not exist in the current context
Question:
How do I access <%# Arguments %> and <%# Module %> master page properties from the user control?
It might be possbile (have not tested it though) to do something like this:
arguments="<%# ((MasterPageType)this.Page.Master).Arguments %>"
Although it does not look right. You might want to redesign the way you control gets the data. Or atthe very least do the same somewhere in code behind and verify whether a current masterpage is of an expected type.
Update. The final solution that OP used incorporated ideas above, and resulted in having properties like below declared in the control:
public IDictionary<string, object> Arguments
{
get
{
MasterPageType master = this.Page.Master as MasterPageType;
if (master != null)
{
return master.Arguments;
}
else
{
return null;
}
}
}
I am storing name and last name in two labels in main page. I also have those values in a class (class doesnt do much but i am using them for future expansion). I have a user control that will send an email with name and last name as body.
My question is that how can I transfer label or class variable values into user control's body variable?
Create a property on your user control with the datatype of the data you want to pass to it, and populate it in your page on creation of the control.
public class myUserControl : Control
{
...
public int myIntProperty {get; set;}
...
}
Later this in the code behind you can assign the value like
myUserControl cntrl = new myUserControl();
cntrl.myIntProperty = 5;
Instead of this you can pass the value through Markup also like
<uc1:myUserControl ID="uc1" runat="server" myIntProperty="5" />
You need to create properties on your control to hold these values; then from the page code, simply assign the values to the properties in the control.
On your control, you can have something like
public string FirstName
{
get {
if (ViewState["FirstName"] == null)
return string.Empty;
return ViewState["FirstName"].ToString();
}
set {
ViewState["FirstName"] = value;
}
}
You need to define public properties on the control and then when you use control on the page you can pass values to those parameters.
Something like:
<cc:mycustomControl runat="server"
MyProperty1=<%# label1 %>
MyProperty2=<%# label2 %>
/>
Step 1:
You can expost the values as property and than you can make use of that easily.
Step 2: To access your page from the user control you can make use of Parent property or may be some custome login to access the parent page and than write code to consume the property value.
you can do something like this in your user control
string x=((yourparentcontrol)this.parent).label1.text;
and use the string x.
ok, I'm working on a custom control that can contain some javascript, and read this out of the page into a string field.
This is a workaround for dynamic javascript inside an updatepanel.
At the moment, I've got it working, but if I try to put a server tag inside the block:
<custom:control ID="Custom" runat="server">
<%= ControlName.ClientID %>
</custom:control>
The compiler does not like it. I know these are generated at runtime, and so might not be compatible with what I'm doing, but does anyone have any idea how I can get that working?
EDIT
Error message is: Code blocks are not supported in this context
EDIT 2
The control:
[DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ControlValueProperty("Text"), DefaultProperty("Text"), ParseChildren(true, "Text"), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class CustomControl : Control, ITextControl
{
[DefaultValue(""), Bindable(true), Localizable(true)]
public string Text
{
get
{
return (string)(ViewState["Text"] ?? string.Empty);
}
set
{
ViewState["Text"] = value;
}
}
}
The compiler is write, server side code blocks are only supported within the context of an ITemplate.
The "Text" property should be set like this ...
<custom:control ID="Custom" runat="server" Text="YourText">
Using ITemplate you can declare it in the codebehind as ...
public ITemplate Text
{
get;
set;
}
But then you would need to do this ...
<custom:control ID="Custom" runat="server">
<Text><%= ControlName.ClientID %></Text>
</custom:control>
Having said that, if you have a custom control why not just do this in the code behind ...
this.text = ((ITextControl)Page.FindControl(controlName)).Text;
Trouble is, it's not very dynamic.
I would favour the templated option.
Harder to implement though.
My User Control has the following properties:
private String _requestIP;
public String RequestIP
{
get { return _requestIP; }
set { _requestIP = value; }
}
When adding a instance of the Control to an aspx page at design time it's easy to assign the attributes which can be utilized in the codebehind file...
<uc:Item ID="Testing" runat="server" RequestIP="127.0.0.1" />
However, if I try to create the control at runtime in the aspx.cs file, how am I able to assign values to these attributes?
Control ItemX = (Control)Page.LoadControl("/controls/item.ascx");
There is no ItemX.Attributes.Add() method which I would expect to be there, and no ItemX.RequestIP property to set.
Is there a way to set this dynamically in the aspx page using <%= Users_IP_Address %> tags or some other method?
Well, you just need to cast it to the appropriate type (whatever the class name of your user control is).