Passing <%=value%> in user control (.ascx) - c#

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();

Related

How to dynamically load a text into a user control label in asp.net?

I want to load 2 user controls(same but with different text property).
My user control consists of a label with a Function defined for text in ascx.cs
i am loading the control at run time using a panel ..here i want both the user control label to have different texts.
My .ascx file
<asp:Label ID="uclbl" runat="server" />
My .ascx.cs file
public string textforlabel
{
get { return uclbl.Text; }
set { uclbl.Text = value; }
}
My .aspx file
<asp:Panel ID="panelMain" runat="server" >
</asp:Panel>
* i have registered the the control
My .aspx.cs file
Control _dummyUserControl = (Control)Page.LoadControl("~/UserControl/User1.ascx");
_dummyUserControl. ; //can not find the textforlabel here
panelMain.Controls.Add(_dummyUserControl);
because you are making incorrect casting, you should cast to your user control type :
User1 _dummyUserControl = (User1)Page.LoadControl("~/UserControl/User1.ascx");
_dummyUserControl.MyProperty = "SDfsdf" ; //here you can find all your custom properties
panelMain.Controls.Add(_dummyUserControl);
You have to type cast:
User1 _dummyUserControl = (User1)Page.LoadControl("~/UserControl/User1.ascx");

passing an argument from an aspx page to a UserControl

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.

Pass values from page to user control

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.

Simple user control for conditionally rendering nested HTML

What I would like to do, is be able to pass two attributes to a user control, a ListName and a Permission, like so:
<uc:check id="uc" List="Shared Documents" Permission="OpenItems" runat="server">
<!-- have some HTML content here that is rendered if the permission is true -->
</uc:check>
Then in the actual check user control, have something similar to:
<%# Control language="C#" ClassName="check" %>
<%
// determine permission magic placeholder
if (DoesUserHavePermissions(perm))
{
// render nested HTML content
}
else
{
// abort rendering as to not show nested HTML content
}
%>
I have read the page on creating a templated control on MSDN, and while that would work - it really seems to be a bit overkill for what I am trying to do. Is there a control that already renders content based on a boolean expression or a simpler template example?
http://msdn.microsoft.com/en-us/library/36574bf6.aspx
Update:
The following code can be used in the ascx to model a very simple version of this:
<%# Control Language="C#" ClassName="PermissionCheck" %>
<%# Import Namespace="System.ComponentModel" %>
<script runat="server">
void Page_Init()
{
if (Allowed != null)
{
Panel container = new Panel();
Allowed.InstantiateIn(container);
PermissionBasedMessage.Controls.Add(container);
}
}
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate Allowed { get; set; }
</script>
<asp:Placeholder runat="server" ID="PermissionBasedMessage" />
Note: I oversimplified the check in the Page_Init method for this sample code. Additional logic checks can be added as needed.
And reference it in the calling HTML page:
<%# Register src="PermissionCheck.ascx" tagname="PermissionCheck" tagprefix="uc1" %>
<uc1:PermissionCheck ID="PermissionCheck1" runat="server">
<Allowed>Allowed Access</Allowed>
</uc1:PermissionCheck>
You could create a custom control instead of a user control: derive from the asp.net panel, add your two properties, then only render the control if the user has the required permission. E.g. something like this:
The control (put this in App_Code for example):
namespace MyControls
{
public class MyPanel : Panel
{
public string Permission { get; set; }
public string List { get; set; }
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
if (UserHasPermission()) base.Render(writer);
}
}
}
Using the control:
<%# Page ... %>
<%# Register Namespace="MyControls" TagPrefix="mc" %>
<html>
...
<mc:MyPanel runat="server" List="Shared Documents" Permission="OpenItems">
put content and/or other controls here
</mc:MyPanel>
...
Why don't you extend the LiteralControl, add properties for your settings, then render html to the .Value of the LieralControl? Seems pretty simple and a lot less of a headache than using Templated controls
The other answers are good for the generic form of your question, but for checking permissions SPSecurityTrimmedControl might do what you need.
Wrap your content with a place holder control and set the control's visibility to true or false (controls that have .Visible = false won't render any html)
<asp:PlaceHolder id="phWrapper" runat="server">
...
</asp:PlaceHolder>
Then in your code-behind set phWrapper.Visible = DoesUserHavePermissions(perm);
Hope that helps!

Instantiate User Control with Custom Attributes

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).

Categories

Resources