Compilation error when dynamically setting property of user control inside repeater - c#

In a C# Webforms website (not web application), I have the following set up:
A page, which includes Step.ascx which includes an asp:Repeater.
The repeater in Step.ascx displays all sections (Section.ascx) within this step.
SectionInfoProvider.GetSections(1).ToList() returns a list of SectionInfo, which is a class several properties including DisplayName as string.
I'm getting a compilation error (The type or namespace name could not be found) when trying to dynamically set the SectionInfo property of the Section user control into the repeater in Step.ascx.cs. I've tried using the partial class name of the user control, and UserControl, but neither work.
I'm also trying to create a collection of SectionControls because I want to use that for something else on the page too.
What am I doing wrong here?
Step.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="~/Step.ascx.cs" Inherits="Custom_Step" %>
<%# Register TagPrefix="uc" TagName="Section" Src="~/Section.ascx" %>
<asp:Repeater runat="server" ID="rptSections" OnItemDataBound="rptSections_OnItemDataBound" EnableViewState="True">
<ItemTemplate>
<uc:Section runat="server" ID="Section" EnableViewState="True"/>
</ItemTemplate>
</asp:Repeater>
Step.ascx.cs
public partial class Custom_Step : System.Web.UI.UserControl
{
public IEnumerable<SectionInfo> Sections { get; set; }
private IEnumerable<???> SectionControls { get; set; } <-- What should ??? be?
protected override void OnLoad(EventArgs e)
{
Sections = SectionInfoProvider.GetSections(1).ToList();
rptSections.DataSource = Sections;
rptSections.DataBind();
}
protected void rptSections_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
var info = (SectionInfo)e.Item.DataItem;
var ctrl = (???)e.Item.FindControl("Section"); <-- What should ??? be?
ctrl.SectionInfo = info;
if (SectionControls == null) SectionControls = new List<???>(); <-- What should ??? be?
((List<???>)SectionControls).Add(ctrl); <-- What should ??? be?
}
}
Section.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="~/Section.ascx.cs" Inherits="Custom_Section " %>
<h3><asp:Literal runat="server" ID="litSectionHeading" /></h3>
Section.ascx.cs:
public partial class Custom_Section : System.Web.UI.UserControl
{
public SectionInfo SectionInfo { get; set; }
protected override void OnLoad(EventArgs e)
{
litSectionHeading.Text = SectionInfo.DisplayName;
}
}

I think you just need a typecast and an if.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {`
...
var ctrl = e.Item.FindControl<Custom_Section>("Section");`
or
var ctrl = (Section)e.Item.FindControl("Section");
...
}
Is that something you already tried?
Creating the dynamic controls can be done but that's a separate question.

Related

Access Master Page class in C# .Net 3.5

So... I'm having to get someone's old (2010) .Net 3.5 C# code working... and of course the dev didn't have the project files anymore just the source...
Long story is code is trying to access a property of the master page class and this looks like it should be working according to everything I can find but it's not.
Example of the MasterPage code behind:
public partial class myMasterPage : System.Web.UI.MasterPage
{
private string pageName = "";
public string PageName
{
get { return PageName; }
set { pageName = value; }
}
private int pageID;
public int PageID
{
get { return pageID; }
set { pageID = value; }
}
}
One of the pages code:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" Inherits="Help" Codebehind="Help.aspx.cs" %>
<%# MasterType TypeName="ISLMasterPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
And finally the code behind for that page:
public partial class Help : BasePage
{
protected void Page_PreInit(object sender, EventArgs e)
{
base.PageID = 1;
Master.PageID = 1;
}
}
I've tried changing the MasterType to virtual path... nothing... error I'm getting is:
E'System.Web.UI.MasterPage' does not contain a definition for 'PageID' and no extension method 'PageID' accepting a first argument of type 'System.Web.UI.MasterPage' could be found (are you missing a using directive or an assembly reference?)
So to me it's obvious that it's not picking up the fact that we want the code behind.
I'm guessing this is something obvious but I've never organized my code in such a way.
Your code doesn't know that your master page is a "myMasterPage"
protected void Page_PreInit(object sender, EventArgs e)
{
base.PageID = 1;
(Master as myMasterPage).PageID = 1;
}
It seems your code has inherited a BasePage
public partial class Help : BasePage
It overrides the normal System.Web.UI.Page and enables easy access to properties on every page where BasePage is used.
In the BasePage class there could be something like this:
public class BasePage : System.Web.UI.Page
{
public Site master;
public BasePage()
{
this.Load += new EventHandler(this.Page_Load);
}
protected void Page_Load(object sender, EventArgs e)
{
master = ((Site)(Page.Master));
}
}
In this example the Master Page is always accessible with master on the pages that inherit BasePage

Cannot implicitly convert type 'System.Web.UI.CompiledTemplateBuilder' to custom ITemplate

When you create a ListView, there is a property on the control that allows you to specify the ID of the PlaceHolder in the LayoutTemplate that is used to hold the controls for each item.
<asp:ListView ID="lvTest" runat="server" ItemPlaceholderID="testPlaceholder">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="testPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>Item</li>
</ItemTemplate>
</asp:ListView>
I am trying to create a custom templated server control but would like to have a property on the template itself as I expect to have several different templates. My extremely simplified, trivial example is as follows:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ControlLibrary
{
[ParseChildren(true)]
public class TestContainer : Control, INamingContainer
{
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(TestContainer))]
public TestTemplate TestTemplate { get; set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
DataBind();
}
public override void DataBind()
{
base.DataBind();
CreateChildControls();
}
protected override void CreateChildControls()
{
Controls.Clear();
var phTest = new PlaceHolder();
phTest.ID = "phTest";
Controls.Add(phTest);
if (this.TestTemplate != null)
this.TestTemplate.InstantiateIn(phTest);
this.ChildControlsCreated = true;
}
}
public class TestTemplate : ITemplate
{
public string Name { get; set; }
public void InstantiateIn(Control container)
{
container.Controls.Add(new LiteralControl(this.Name));
}
}
}
However, if I add the template to my .aspx file like below I get an error saying CS0029: Cannot implicitly convert type 'System.Web.UI.CompiledTemplateBuilder' to 'ControlLibrary.TestTemplate'
<CustomControls:TestContainer ID="testContainer" runat="server">
<TestTemplate>
test
</TestTemplate>
</CustomControls:TestContainer>
Is it possible to do this or will I have to put all configurable properties on the parent container itself?
Thanks for any help you can provide!
You did everything correctly except the template property declaration. Any template must be declared as System.Web.UI.ITemplate. Just replace your template declaration with:
public ITemplate TestTemplate { get; set; }
And it should start working. For reference see How to: Create Templated ASP.NET User Controls.
To do a trick with custom attributes for template you need to add one more class inheriting from collection:
public class TestTemplateList : List<TestTemplate> { }
And change declaration of the control as following:
[ParseChildren(true, DefaultProperty = "TestTemplates")]
public class TestContainer : Control, INamingContainer
{
[PersistenceMode(PersistenceMode.InnerProperty)]
public TestTemplateList TestTemplates { get; set; }
// ... OnLoad and DataBind left intact
protected override void CreateChildControls()
{
Controls.Clear();
var phTest = new PlaceHolder();
phTest.ID = "phTest";
Controls.Add(phTest);
if (this.TestTemplates != null)
{
foreach (var testTemplate in TestTemplates)
{
((ITemplate)testTemplate).InstantiateIn(phTest);
}
}
this.ChildControlsCreated = true;
}
}
After that you should be able to declare control in both ways. You can skip specifying TestTemplates because it is declared as a DefaulProperty in ParseChildrenAttribute:
<CustomControls:TestContainer runat="server" ID="testContainer">
<CustomControls:TestTemplate Name="test">
</CustomControls:TestTemplate>
</CustomControls:TestContainer>
<%-- OR --%>
<CustomControls:TestContainer runat="server" ID="wc11">
<TestTemplates>
<CustomControls:TestTemplate Name="test">
</CustomControls:TestTemplate>
</TestTemplates>
</CustomControls:TestContainer>

How to access user control properties from page?

I am having problem in accessing user control properties from page.
I have usercontrol on master page with some properties,but i am unable to access them from the codebehind of the page which uses that master page.i want to set some properties of usercontrol on page load.Can anyone suggest how can i access them from page.
E.G.
User Control
ucTabSystem.ascx has following properties:
public string TabName
{
get { return _tabName; }
set { _tabName = value; }
}
public string TabUrl
{
get { return _tabUrl; }
set { _tabUrl = value; }
}
Master Page
InHouseTPAMaster.master has this user control in it.
ClaimHomePage.aspx
Uses Master page InHouseTPAMaster.master and i want to set usercontrol properties in page load of this page.
You can use two methods.
The first is by using Page.Master.FindControl('controlID'). Then you can cast it to the type of your user control. The second method is by adding a <%# MasterType VirtualPath="" TypeName=""%> tag to your aspx page. In the VirtualPath add the virtual path to the master page, and the class in the TypeName. You can then access everything with intellisense
You can try this way to set your properties...
<%# Register TagPrefix="Tab" TagName="sys" Src="ucTabSystem.ascx" %>
<tab:sys id="mysys" runat="server" TabName="xxxxx" TabUrl = "yyyy" />
You need to define a public interface with two properties - TabName and TabUrl in separate code file.
public interface IUserControl
{
string TabName{get;set;}
string TabUrl {get;set;}
}
Implements the IUserControl interface to UserControl class. For instance, I've MyUserControl and its code-behind is:
public partial class MyUserControl : System.Web.UI.UserControl , IUserControl
{
public string TabName
{
get { return ViewState["TabName"] == null ? string.Empty : ViewState["TabName"].ToString(); }
set { ViewState["TabName"]= value; }
}
public string TabUrl
{
get { return ViewState["TabUrl"] == null ? string.Empty : ViewState["TabUrl"].ToString(); }
set { ViewState["TabUrl"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Register MyUserControl in MasterPage and it has following markup in it.(master page)
<%# Register src="MyUserControl.ascx" tagname="MyUserControl" tagprefix="uc1" %>
......
<uc1:MyUserControl ID="MyUserControl1" runat="server" />
In Page_Load event (or any other) handler of aspx page (Which is a content page of said master page).
protected void Page_Load(object sender, EventArgs e)
{
IUserControl control = Master.FindControl("MyUserControl1") as IUserControl;
control.TabName = "Something";
control.TabUrl = "http://www.example.com";
}

Web Controls within UserControl null?

I've built a small User Control which is essentially a DropDownList with some preset Values based on what the Target-Property is set on.
Here's the Code:
public partial class Selector : System.Web.UI.UserControl
{
public string SelectedValue { get {return this.ddl.SelectedValue; } }
public int SelectedIndex { get { return this.ddl.SelectedIndex; } }
public ListItem SelectedItem { get { return this.ddl.SelectedItem; } }
private string target;
public string Target { get { return this.target; } set { this.target = value; } }
protected void Page_Load(object sender, EventArgs e)
{
ddl.DataSource = target=="Group"?Util.GetAllGroups(Session["sessionId"].ToString()):Util.GetAllUsers(Session["sessionId"].ToString());
ddl.DataBind();
}
}
ASP-Markup:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Selector.ascx.cs" Inherits="InspireClient.CustomControls.Selector" %>
<asp:DropDownList runat="server" ID="ddl">
</asp:DropDownList>
If I insert my Selector into an aspx-Page it works just fine.
Example:
<SCL:Selector Target="Group" runat="server" />
However, If I programmatically add it like this
ctrl = new Selector();
ctrl.Target = "User";
the DropDownList "ddl" is null and the application (logically) throws an error. Is Page_Load the wrong Method to do such a thing? What am I doing wrong?
I should add, "ctrl" is of type dynamic, not sure if this has anything to do with it.
Thanks in advance!
Dennis
Since you're dynamically adding a user control and not a "simple" web control, you should use the LoadControl() method to instantiate it:
protected void Page_Load(object sender, EventArgs e)
{
Selector yourControl = (Selector) LoadControl("Selector.ascx");
yourControl.Target = "User";
Controls.Add(yourControl);
}

User Control Postback

I am working on rendering a set of cart items using a user control. Each cart item can be removed via a button in the user control. When a cart item is removed I need to visually show it's removal. However, since the cart item existed during the loading of the page it remains until the page is refreshed again. What I am after is a means to refresh the page after the work to remove the cartitem has been completed.
The code behind cart.aspx.cs looks like:
protected void Page_Init(object sender, EventArgs e)
{
CreateCartItemControls();
}
private void CreateCartItemControls()
{
foreach (CartItem ci in Profile.Cart.Items)
{
ASP.CartItemControl cic = new ASP.CartItemControl();
cic.ItemName = ci.Name;
cic.CartID = ci.ID;
cic.Cost = ci.BaseCost.ToString("c");
cic.ItemComponents = ci.Components;
cic.CartItemRemoved += new EventHandler(CartItemRemoved);
Content.Controls.Add(cic);
}
}
void CartItemRemoved(object sender, EventArgs e)
{
Master.UpdateCartItemCount();
}
Markup for CartItemControl.ascx
<%# Control Language="C#" ClassName="CartItemControl" AutoEventWireup="true"
CodeFile="CartItemControl.ascx.cs"
Inherits="UserControls_CartItemControl" %>
<fieldset id="FieldSet" runat="server">
<legend>
<asp:HyperLink ID="ItemLink" runat="server" />
</legend>
<asp:ImageButton ID="RemoveCartItem" AlternateText="Remove Item"
ImageUrl="~/img/buttons/remove_4c.gif" runat="server"
CommandName="Remove" OnCommand="RemoveCartItem_Command" />
<asp:Label ID="TotalItemCost" runat="server" Text="$0.00" />
<ol>
<li runat="server" id="ComponentsLI" visible="false">
<fieldset id="ComponentsFieldSet" runat="server">
<legend>Item Components</legend>
<asp:CheckBoxList ID="ItemComponentsCheckList"
runat="server" />
</fieldset>
</li>
</ol>
</fieldset>
Code behind for the UserControl CartItemControl.ascx.cs
public partial class UserControls_CartItemControl
: System.Web.UI.UserControl
{
public string ItemName { get; set; }
public int CartID { get; set; }
public string Cost { get; set; }
public IDictionary<int, SoftwareComponent> ItemComponents { get; set; }
protected void Page_PreRender(object sender, EventArgs e)
{
SetCartItemControlAttributes();
}
private void SetCartItemControlAttributes()
{
ItemLink.Text = ItemName;
TotalItemCost.Text = Cost;
RemoveCartItem.CommandArgument = CartID.ToString();
if (!ItemComponents.Count.Equals(0))
{
ComponentsLI.Visible = true;
foreach (KeyValuePair<int, ItemComponent> kvp in
ItemComponents)
{
ItemComponentsCheckList.Items.Add(
new ListItem(string.Format("{0} {1}",
kvp.Value.ComponentName,
kvp.Value.ComponentCost.ToString("c")),
kvp.Key.ToString()));
}
}
}
public event EventHandler CartItemRemoved;
protected void RemoveCartItem_Command(object sender, CommandEventArgs e)
{
int itemID;
if (int.TryParse(e.CommandArgument.ToString(), out itemID))
{
Profile.Cart.RemoveCartItem(itemID);
CartItemRemoved(sender, e);
Parent.Controls.Remove(this);
}
}
}
Just as you add CartItemControls to Content's Controls collection on init, you need to remove them on RemoveCartItem_Command. Do so by either exposing your own ItemRemoved event and handling it in the main page or by calling Parent.Controls.Remove(this) inside the RemoveCartItem_Command.
Or am I missing something?
Response.Redirect back to the page after you do the work to remove the cart item.
Try Server.Transfer to the same page.
That said, consider using Ruslan approach, by exposing an ItemRemoved event and handling it on the main page. You can do Content.Controls.Clear and call CreateCartItemControls again.
Just rebind the everything on the main page on every postback.

Categories