Inlining a Public Variable in a User Control - c#

I have a user control with a variable that I am trying to display inline in the Aspx page, but it always shows as empty. I've done this in other user controls and I'm not sure why it isn't working.
Aspx...
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyClass.ascx.cs" Inherits="MyClass" %>
<%# sMyPublicString %>
Code Behind...
public partial class MYClass : System.Web.UI.UserControl
{
public string sMyPublicString = "MyValue";
}

In parent webform on Page_Load you have to add Page.DataBind()

Related

User control loaded dynamically with parameters not showing in asp.net

I've search a lot for this issue but nothing came up.
My problem is the following: I have a main view in which I want to load a user control with parameters when I click on a button but the user control won't show. The constructor of the user control is called and the parameters are set, even the page load event from the user control is called. What am i doing wrong?
Main.aspx:
<%# Page Title="Main page" Language="C#" AutoEventWireup="true" CodeBehind="Main.aspx.cs" Inherits="MainApp.Main" MasterPageFile="~/Site.master" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<%-- some other fields which i send to the user control on click --%>
<div class="col-lg-1 col-sm-12 col-md-1">
<asp:Button runat="server" CssClass="btn btn-primary" CausesValidation="false" ID="generateData" OnClick="generateData_Click" Text="View info" />
</div>
<div runat="server" id="contentDiv">
<p>No info available atm.</p>
</div>
</asp:Content>
Main.aspx.cs button click event:
protected void generateData_Click(object sender, EventArgs e)
{
UserControl1 uc = (UserControl1 )this.LoadControl(typeof(UserControl1 ), new object[] { param1, param2, param3});
contentDiv.Controls.Clear();
contentDiv.Controls.Add(uc);
}
UserControl1.aspx.cs:
public partial class UserControl1: System.Web.UI.UserControl
{
public string Param3 { get; set; }
public string Param1 { get; set; }
public string Param2 { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}
public UserControl1(string param1, string param2, string param3)
{
Param1 = param1;
Param2 = param2;
Param3 = param3;
}
}
UserControl1.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs" Inherits="MainApp.UserControls.UserControl1" %>
<div>
<p style="color: red">Under construction</p>
</div>
The user control isn't visible on the main page and I don't know why.
PS: I know that i can send parameters as seen below but I don't understand why I cannot use the method described above.
UserControl1 uc = (UserControl1)this.LoadControl("~/UserControls/UserControl1.ascx");
uc.Param1 = "val1";
...
Here is the full explanation of why second method with LoadControl by type will not work: Asp.net Usercontrol LoadControl Issue.
The reason there is a difference is because in the second instance you
are bypassing the binding mechanism of the the two partial classes.
When you include the ascx control in the page as a normal declaration,
the controls declared within it (the label in your example) are
automatically instantiated by the framework as the class is
instantiated. This is a binding mechnism that reads the "front-end"
ascx file and instantiates objects in the class.
When you use LoadControl(string) - you are doing exactly the same
thing.. it uses a virtual path to get the "front-end" file and ensure
controls within it are instantiated.
However, when you use the new (to 2.0) LoadControl(type, object)
version, there is no virtual path available and it rightly so makes no
assumptions (as it cannot ensure that a type has a front end file).

How to access MasterPage1 method in another ContentPage with MasterPage2 in asp.net?

I have Page1.aspx which has MasterPage1.master as its MasterPage and Page2.aspx which has MasterPage2.master as its MasterPage. I am showing Page2.aspx page in iframe (which is in Page1.aspx) Page2.aspx displays some items in ListView. After adding item to Cart(MyCartUserControl in MasterPage1.master) i want to call the Method say 'MyMethod()' which is in MasterPage1.master. in Page2.aspx
In Page2.aspx:
<%# MasterType VirtualPath="~/Sales/MasterPage1.master" %>
In Page2.aspx.cs
protected void UpdateShoppingCart()
{
Sales_MasterPage1 master = (Sales_MasterPage1)this.Master;
master.BindCart();
}
I know the above code will not work using 'this'. What is replaceable to 'this' keyword?
Help Appreciated!
Try below Code :
protected void UpdateShoppingCart()
{
Sales_MasterPage1 master = new Sales_MasterPage1();
master.BindCart();
}
Problem is the access modifier.
You might need to make it a public method because protected wont let you access it outside of that master class
public static void UpdateShoppingCart()
{
Sales_MasterPage1 master = new Sales_MasterPage1();
master.BindCart();
}
to call it from anywhere you can do:
Sales_MasterPage1.UpdateShoppingCart()

Advice on how to create a common base class (or functionality) between a page and master?

I created a custom base class that my .aspx pages inherit from.
Since Master page's inherit from MasterPage and not Page, how can I create common functionality that are available in both my Pages and Master pages?
public class SitePage : System.Web.UI.Page
{
public SitePage()
{
}
public bool IsLoggedIn
{
//
}
public string HtmlTitle
{
//
}
}
One approach would be to put all the functionality in the Master page and always call them by going through the Master page. You can strongly type the Master property in SitePage:
public class SitePage : Page
{
public new MyMaster Master { get { return base.Master as MyMaster; } }
}
Then access the values through the Master:
this.Master.IsLoggedIn
Master Page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MyProject.master.cs"
Inherits="MyProject.MasterPages.MyProject" %>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
Base page:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/MyProject.Master"
AutoEventWireup="true" CodeBehind="BasePage.aspx.cs"
Inherits="MyProject.BasePage" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
</asp:Content>
Content Page:
<%# Page Title="MyProject - Home" Language="C#"
MasterPageFile="~/MasterPages/MyProject.Master" AutoEventWireup="true"
CodeFileBaseClass="MyProject.BasePage" CodeFile="Default.aspx.cs"
Inherits="MyProject.Default"
Meta_Description="Code Snippet: Master Page and Base Page"
Meta_Keywords="master, base, content" Theme="Style" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
</asp:Content>

Pass Generic Model to View

I have this in my Page setup;
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ILocationsFormViewModel>" %>
And the interface looks like this;
public interface ILocationsFormViewModel<T>
{
List<TopLocation<T>> Locations { get; set; }
List<TrendingItem<ITrendItem>> TrendingLocations { get; set; }
}
T is either ITopHotel or ITopShop etc etc with no properties
The problem is I get this error when I goto the View;
CS0305: Using the generic type 'ILocationsFormViewModel' requires
'1' type arguments
Does anyone know how I can pass a class that has a Generic type to a view?
You probably need the generic type parameter in there:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ILocationsFormViewModel<T>>" %>

Dynamic user control controls are null on load

I have a base usercontrol in my ASP.Net app, There are HTML markup within this usercontrol that are set to runat="server" and id's. The problem I am having is when the usercontrol is loaded, the HTML markup is being returned as null
ASP.Net C# code:
public partial class webonlinecustombase : System.Web.UI.UserControl
{
public Event Event { get; set; }
public OnlineSystemPageCustom custompage { get; set; }
public OnlineSystemPageCustom.OnlineSystemPageHdr.OnlineSystemPageModule custommodule { get; set; }
public void Page_Load(object sender, EventArgs e)
{
string typeName = custommodule.ModuleInternetFile;
inpagelink.HRef = "#" + custommodule.ModuleName.Replace(" ", "").Replace("/", "");
modtitle.InnerText = custommodule.ModuleName;
Type child = Type.GetType(typeName);
UserControl ctl = Activator.CreateInstance(child) as UserControl;
if (ctl != null)
{
this.modsection.Controls.Add(ctl);
}
}
}
Here is the HTML Markup:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="webonlinecustombase.ascx.cs" Inherits="IPAMIntranet.IPAM_Controls.webtemplatecontrols.webonlinecustombase" %>
<a id="inpagelink" runat="server"></a>
<span id="modtitle" runat="server" style="width:100%;text-align:left">Scientific Overview</span>
<div id="modsection" runat="server" style="width:100%;">
</div>
<p>Back to Top</p>
Why is the inpagelink and modtitle being returned as null?
I have seen this happen in web applications (not web sites) when changes are made to the source (especially setting runat=server on items that were not previously runat=server), but you don't switch to the designer view.
The way that I resolve this issue is to switch to design view and dirty a property in one of the fields. This usually causes VS to update the designer code-behind file.
You can double-check this file to ensure the controls have been added. If you check it prior to doing this, you should see that they are missing.
asp.net does'n have span class, so you cant do anything in code behind with it.
use LinkButton or HyperLink instead of
the other solution is to create span or a in code, something like this
var span = new HtmlGenericControl("span");
span.InnerHtml = "From<br/>Date";
span.Attributes["class"] = "blue";
placeholder.Controls.Add(span);
hope I helped :))

Categories

Resources