UserControl pass Value to MasterPage Content - c#

I am coding in a userControl,
how can I pass text to a control I placed in one of the contents?
It's a literal by the way
Thank you

Create a public property in Master page.
public string WhateverPropertyName
{
get
{
return LiteralControlInMasterPage.Text;
}
set
{
LiteralControlInMasterPage.Text = value;
}
}
and then access it from the usercontrol as
Page.Master.WhateverPropertyName = "Whatever new value";
If you cannot access/update master page use
Literal mpLiteral = (Literal) Master.FindControl("masterPageLiteralControlID");

Related

How to set public bool value in C# code behind, read in .ascx User Control?

I want to slightly modify a user control that is being loaded by various .aspx pages.
If I set a bool value in the .aspx.cs page
public bool showTemplateBGOption;
public bool TemplateBGOption
{
set { showTemplateBGOption = false; }
}
How can I read the bool value showTemplateBGOption in the .ascx control that is rendered on the same .aspx page?
You could cast the Page property to the right type, the page property needs a getter.
(somewhere in your ascx codebehind):
MyPageType page = this.Page as MyPageType;
if(page != null)
{
bool templateBGOption = page.TemplateBGOption;
}
But in this way you are hard-wiring the page with the UserControl. Instead the page should specify the TemplateBGOption to the UserControl.
(somewhere in your page's codebehind):
this.UserControl1.TemplateBGOption = this.TemplateBGOption;
You can inplement an Interface to All Pages that will use this property and then use it in the user control and in this way you can decouple a user control from a specific page but you do it to a "contract"
interface ITemplateBOption
{
bool TemplateBGOption{get;set;}
}
Public MyPage : Page, ITemplateBOption
{
public bool TemplateBGOption
{
get{...}
set{...}
}
}
In your User Control use like this:
ITemplateBOption page = this.Page as ITemplateBOption;
if(page != null)
{
bool templateBGOption = page.TemplateBGOption;
}else
{
//do something, thrown an exception for example.
}

MasterPage Access Controls

I have 2 master page in my web site.
MainMaster
SubMaster (Master Page:MainMaster)
Page (Master Page:SubMaster)
I have hidden fields on SubMasterPage. And i'm proccessing datas and setting hidden field value on SubMasterPage Init event. I want to get hiddenfield's value from Page.aspx
I'm trying this on Page.aspx, getting "Object reference not set to an instance of an object." error
((HiddenField)this.Master.FindControl("hiddenId")).Value
But when i have 1 master page this code works normally.
Have i a solution in this problem? Or should i try transfer datas via session/querystring e.t.c.?
You could add a property to your sub master page to return the value, and use this in the child page.
eg.
Sub Master Page
public string HiddenValue
{
get
{
//return the value of your hidden field
return HiddenID.Value;
}
}
Child page:
//Method to get the hidden value from the master page, if the master page is a sub master page
private string GetHiddenValue()
{
if (this.Master is SubMasterPage)
{
string value = (this.Master as SubMasterPage).HiddenValue;
return value;
}
else
{
return string.Empty;
}
}
if you want to go one step further you could add an extension method to the MasterPage class to call it easily from any page.
eg:
public static class MasterPageExtensions
{
public static string GetHiddenFieldValue(this MasterPage master)
{
if (master is SubMasterPage)
return (master as SubMasterPage).HiddenFieldValue;
else
return string.Empty;
}
}
public class SubMasterPage : MasterPage
{
private HiddenField _hiddenField;
public string HiddenFieldValue
{
get
{
return _hiddenField.Value;
}
}
}
public class ChildPage : Page
{
void TestMethod()
{
string hiddenValue = this.Master.GetHiddenFieldValue();
}
}
this is particularly useful when for example you have a single modal popup message box on the master page, and you want to show it from any child page.

Transfer value to dynamically loaded web user control

I have ASPX page where on preinit i check whitch user control to load.
control = "~/templates/" + which + "/master.ascx";
Then on pageload, i load that control
Control userControl = Page.LoadControl(control);
Page.Controls.Add(userControl);
How i can transfer dynamically loaded user control, from aspx to ascx?
You can create an interface that is implemented by all your custom controls. This way, you can cast to that interface and use it to pass your data through it. Consider this example:
public interface ICustomControl
{
string SomeProperty { get; set; }
}
... and your controls:
public class Control1 : Control, ICustomControl
{
public string SomeProperty
{
get { return someControl.Text; }
set { someControl.Text = value; }
}
// ...
}
Now, you can do something like this:
Control userControl = Page.LoadControl(control);
Page.Controls.Add(userControl);
if (userControl is ICustomControl)
{
ICustomControl customControl = userControl as ICustomControl;
customControl.SomeProperty = "Hello, world!";
}
You could use Page.Findcontrol(...) to get a control in the parent page.
see: http://msdn.microsoft.com/en-us/library/31hxzsdw.aspx
Just make sure you set the ID in the code, so you can access it. Like:
Control userControl = Page.LoadControl(control);
userControl.ID = "ctlName";
Page.Controls.Add(userControl);
cast in your control class type, you need to create public properties for controls (if you want to transfer data to controls)
var userControl = (master)Page.LoadControl(control);
userControl.yourpublicproperty ="some text";
Page.Controls.Add(userControl);

Usercontrol Datasource?

In my current windows application I am looking on how to give a datasource to my usercontrol.
On my page I add my own usercontrol in a flowlayoutpanel, the usercontrol has 3 textboxes in it which I want to fill with data from a datasource.
usercontrol uc = new usercontrol();
flowlayoutpanel.Controls.Add(uc);
uc.DataSource?
I know that in silverlight and ASP.NET you can add a datasource to a usercontrol. In the usercontrol you get the data into the textboxes by using {Binding fieldname} as their content. I can't find any information on this for Windows Forms.
Thanks for the help.
Thomas
There's an article on MSDN which might help you implement this - Walkthrough: Creating a User Control that Supports Simple Data Binding (see also Complex and Lookup Data binding walkthroughs).
I solved this (thanks to the link of Stuart) by putting setters and getters in my usercontrol.
public partial class ucOpleiding : UserControl
{
public string Datum
{
get { return txtDatum.Text; }
set { txtDatum.Text = value; }
}
public string Plaats
{
get { return txtPlaats.Text; }
set { txtPlaats.Text = value; }
}
public string Omschrijving
{
get { return txtOmschrijving.Text; }
set { txtOmschrijving.Text = value; }
}
public ucOpleiding()
{
InitializeComponent();
}
And in my main form I will then call those setters and getters.
foreach (opleiding opl in ChauffeurManagement.getOpleidingen(Int32.Parse(cbbID.SelectedValue.ToString())))
{
ucOpleiding uc = new ucOpleiding();
uc.Datum = opl.datum.ToString();
uc.Plaats = opl.plaats_instantie;
uc.Omschrijving = opl.omschrijving;
flpOpleidingen.Controls.Add(uc);
}

Access Master Page public method from user control/class/page

I am to access a method on my master page. I have an error label which I want to update based on error messages I get from my site.
public string ErrorText
{
get { return this.infoLabel.Text; }
set { this.infoLabel.Text = value; }
}
How can I access this from my user control or classes that I set up?
To access the masterpage:
this.Page.Master
then you might need to cast to the actual type of the master page so that you could get the ErrorText property or make your master page implement an interface containing this property.
Page should contain next markup:
<%# MasterType VirtualPath="~/Site.master" %>
then Page.Master will have not a type of MasterPage but your master page's type, i.e.:
public partial class MySiteMaster : MasterPage
{
public string ErrorText { get; set; }
}
Page code-behind:
this.Master.ErrorText = ...;
Another way:
public interface IMyMasterPage
{
string ErrorText { get; set; }
}
(put it to App_Code or better - into class library)
public partial class MySiteMaster : MasterPage, IMyMasterPage { }
Usage:
((IMyMasterPage )this.Page.Master).ErrorText = ...;

Categories

Resources